Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat/#221] 아티클 조회수 저장 방식 및 조회 방식 개선 #223

Merged
merged 13 commits into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ArticleViewCountDao(
dslContext.insertInto(ARTICLE_VIEW_COUNT)
.set(ARTICLE_VIEW_COUNT.ARTICLE_ID, query.articleId)
.set(ARTICLE_VIEW_COUNT.VIEW_COUNT, 1)
.set(ARTICLE_VIEW_COUNT.CATEGORY_CD, query.categoryType?.code)
.set(ARTICLE_VIEW_COUNT.CATEGORY_CD, query.categoryType.code)
.onDuplicateKeyUpdate()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요거는 뭔가요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

article_view_count 테이블 로우는 아티클 당 한개로 구성되는데, 첫번째로 조회될 땐 insert가 가고, 이미 row가 있는 경우(이미 1번 이상 조회됨) Update(view_count + 1) 쿼리가 날라가는 upsert 쿼리입니다

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아하 신기하다..!!!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SQL 변환

INSERT INTO ARTICLE_VIEW_COUNT (ARTICLE_ID, VIEW_COUNT, CATEGORY_CD)
VALUES ($articleId, 1, $category)
ON DUPLICATE KEY UPDATE
VIEW_COUNT = VIEW_COUNT + 1;

.set(ARTICLE_VIEW_COUNT.VIEW_COUNT, ARTICLE_VIEW_COUNT.VIEW_COUNT.plus(1))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import com.few.data.common.code.CategoryType

data class ArticleViewCountQuery(
val articleId: Long,
val categoryType: CategoryType?,
val categoryType: CategoryType,
)
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ArticleViewHisAsyncHandler(

@Async(value = DATABASE_ACCESS_POOL)
@Transactional
fun addArticleViewHis(articleId: Long, memberId: Long, categoryType: CategoryType?) {
fun addArticleViewHis(articleId: Long, memberId: Long, categoryType: CategoryType) {
try {
articleViewHisDao.insertArticleViewHis(ArticleViewHisCommand(articleId, memberId))
log.debug { "Successfully inserted article view history for articleId: $articleId and memberId: $memberId" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class ReadArticleUseCase(
articleViewHisAsyncHandler.addArticleViewHis(
useCaseIn.articleId,
useCaseIn.memberId,
CategoryType.fromCode(articleRecord.category)
CategoryType.fromCode(articleRecord.category) ?: throw RuntimeException("invalid article")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

모든 아티클은 카테고리가 필수인데, 카테고리에 정의되지 않은 경우가 발생하는거 자체가 말이 일단 안되는 상황이라.. 예외코드를 정의하고 던지는게 맞을까 싶어요

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 그렇긴 한데 말이 안되는 상황도 대비하는 것은 나쁘지 않은 것 같아요

)

return ReadArticleUseCaseOut(
Expand Down
Loading