Skip to content

Commit

Permalink
refactor: 단일 조회 방식으로 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
belljun3395 committed Jan 13, 2025
1 parent c60df7b commit 1fae88b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ class ArticleDao(
selectArticleContentsQuery(articleIds)
.fetchInto(SelectArticleContentsRecord::class.java)

fun selectArticleContent(articleId: Long): SelectArticleContentsRecord? = dslContext.select(
ArticleIfo.ARTICLE_IFO.ARTICLE_MST_ID.`as`(SelectArticleContentsRecord::articleId.name),
ArticleIfo.ARTICLE_IFO.CONTENT.`as`(SelectArticleContentsRecord::content.name)
).from(ArticleIfo.ARTICLE_IFO)
.where(ArticleIfo.ARTICLE_IFO.ARTICLE_MST_ID.eq(articleId))
.and(ArticleIfo.ARTICLE_IFO.DELETED_AT.isNull)
.fetchOneInto(SelectArticleContentsRecord::class.java)

fun selectArticleContentsQuery(articleIds: Set<Long>) = dslContext.select(
ArticleIfo.ARTICLE_IFO.ARTICLE_MST_ID.`as`(SelectArticleContentsRecord::articleId.name),
ArticleIfo.ARTICLE_IFO.CONTENT.`as`(SelectArticleContentsRecord::content.name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,29 @@ class ArticleMainCardDao(
.toSet()
}

fun selectArticleMainCardRecord(articleId: Long): ArticleMainCardRecord? =
dslContext.select(
ARTICLE_MAIN_CARD.ID.`as`(ArticleMainCardRecord::articleId.name),
ARTICLE_MAIN_CARD.TITLE.`as`(ArticleMainCardRecord::articleTitle.name),
ARTICLE_MAIN_CARD.MAIN_IMAGE_URL.`as`(ArticleMainCardRecord::mainImageUrl.name),
ARTICLE_MAIN_CARD.CATEGORY_CD.`as`(ArticleMainCardRecord::categoryCd.name),
ARTICLE_MAIN_CARD.CREATED_AT.`as`(ArticleMainCardRecord::createdAt.name),
ARTICLE_MAIN_CARD.WRITER_ID.`as`(ArticleMainCardRecord::writerId.name),
ARTICLE_MAIN_CARD.WRITER_EMAIL.`as`(ArticleMainCardRecord::writerEmail.name),
jsonGetAttributeAsText(
ARTICLE_MAIN_CARD.WRITER_DESCRIPTION,
"name"
).`as`(ArticleMainCardRecord::writerName.name),
jsonGetAttribute(ARTICLE_MAIN_CARD.WRITER_DESCRIPTION, "url").`as`(ArticleMainCardRecord::writerUrl.name),
jsonGetAttribute(
ARTICLE_MAIN_CARD.WRITER_DESCRIPTION,
"imageUrl"
).`as`(ArticleMainCardRecord::writerImgUrl.name),
ARTICLE_MAIN_CARD.WORKBOOKS.`as`(ArticleMainCardRecord::workbooks.name)
).from(ARTICLE_MAIN_CARD)
.where(ARTICLE_MAIN_CARD.ID.eq(articleId))
.fetchOne(articleMainCardMapper)

fun selectArticleMainCardsRecordQuery(articleIds: Set<Long>) = dslContext.select(
ARTICLE_MAIN_CARD.ID.`as`(ArticleMainCardRecord::articleId.name),
ARTICLE_MAIN_CARD.TITLE.`as`(ArticleMainCardRecord::articleTitle.name),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import com.few.data.common.code.CategoryType
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional
import java.util.*
import kotlin.Comparator

@Component
class BrowseArticlesUseCase(
Expand Down Expand Up @@ -57,25 +56,22 @@ class BrowseArticlesUseCase(
true
}

val articleViewRecordIds = articleViewsRecords.map { it.articleId }

/**
* ARTICLE_MAIN_CARD 테이블에서 이번 스크롤에서 보여줄 10개 아티클 조회 (TODO: 캐싱 적용)
*/
val articleMainCardRecords: Set<ArticleMainCardRecord> =
articleMainCardDao.selectArticleMainCardsRecord(articleViewsRecords.map { it.articleId }.toSet())
val articleMainCardRecords: Set<ArticleMainCardRecord> = articleViewRecordIds.map { articleMainCardDao.selectArticleMainCardRecord(it)!! }.toSet()

/**
* 아티클 컨텐츠는 ARTICLE_MAIN_CARD가 아닌 ARTICLE_IFO에서 조회 (TODO: 캐싱 적용)
*/
val selectArticleContentsRecords: List<SelectArticleContentsRecord> =
articleDao.selectArticleContents(articleMainCardRecords.map { it.articleId }.toSet())
setContentsToRecords(selectArticleContentsRecords, articleMainCardRecords)

/**
* 아티클 조회수 순, 조회수가 같을 경우 최신 아티클이 우선순위를 가지도록 정렬 (TODO: 삭제시 양향도 파악 필요)
*/
val sortedArticles = updateAndSortArticleViews(articleMainCardRecords, articleViewsRecords)
val selectArticleContentsRecords: List<SelectArticleContentsRecord> = articleViewRecordIds.map { articleDao.selectArticleContent(it)!! }.toList()
articleMainCardRecords.withIndex().forEach { (index, articleMainCardRecord) ->
articleMainCardRecord.content = selectArticleContentsRecords[index].content
}

val articleUseCaseOuts: List<ReadArticleUseCaseOut> = sortedArticles.map { a ->
val articleUseCaseOuts: List<ReadArticleUseCaseOut> = articleMainCardRecords.map { a ->
ReadArticleUseCaseOut(
id = a.articleId,
writer = WriterDetail(
Expand All @@ -99,51 +95,4 @@ class BrowseArticlesUseCase(

return ReadArticlesUseCaseOut(articleUseCaseOuts, isLast)
}

private fun updateAndSortArticleViews(
articleRecords: Set<ArticleMainCardRecord>,
articleViewsRecords: List<SelectArticleViewsRecord>,
): Set<ArticleMainCardRecord> {
val sortedSet = TreeSet(
Comparator<ArticleMainCardRecord> { a1, a2 ->
// views 값이 null일 경우 0으로 간주
val views1 = a1.views ?: 0
val views2 = a2.views ?: 0

// views 내림차순 정렬
val viewComparison = views2.compareTo(views1)

if (viewComparison != 0) {
viewComparison
} else {
// views가 같을 경우 articleId 내림차순 정렬(최신글)
val articleId1 = a1.articleId
val articleId2 = a2.articleId
articleId2.compareTo(articleId1)
}
}
)

val viewsMap = articleViewsRecords.associateBy({ it.articleId }, { it.views })

articleRecords.forEach { article ->
val updatedViews = viewsMap[article.articleId] ?: 0
article.views = updatedViews
sortedSet.add(article)
}

return sortedSet
}

private fun setContentsToRecords(
articleContentsRecords: List<SelectArticleContentsRecord>,
articleMainCardRecords: Set<ArticleMainCardRecord>,
) {
val articleMainCardRecordsMap: Map<Long, ArticleMainCardRecord> =
articleMainCardRecords.associateBy { it.articleId }

articleContentsRecords.map { articleContentRecord ->
articleMainCardRecordsMap[articleContentRecord.articleId]?.content = articleContentRecord.content
}
}
}

0 comments on commit 1fae88b

Please sign in to comment.