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

[Fix/#281] 아티클 조회수 순 정렬시 옵션 추가(article최신순) #282

Merged
merged 2 commits into from
Aug 3, 2024
Merged
Changes from all commits
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 @@ -7,8 +7,6 @@ import com.few.api.repo.dao.article.query.SelectRankByViewsQuery
import com.few.api.repo.dao.article.record.SelectArticleViewsRecord
import jooq.jooq_dsl.tables.ArticleViewCount.ARTICLE_VIEW_COUNT
import org.jooq.DSLContext
import org.jooq.Record2
import org.jooq.SelectLimitPercentStep
import org.jooq.impl.DSL.*
import org.springframework.stereotype.Repository

Expand Down Expand Up @@ -56,12 +54,13 @@ class ArticleViewCountDao(
fun selectRankByViewsQuery(query: SelectRankByViewsQuery) = dslContext
.select(field("row_rank_tb.offset", Long::class.java))
.from(
dslContext
.select(
ARTICLE_VIEW_COUNT.ARTICLE_ID,
rowNumber().over(orderBy(ARTICLE_VIEW_COUNT.VIEW_COUNT.desc())).`as`("offset")
)
.from(ARTICLE_VIEW_COUNT)
dslContext.select(
ARTICLE_VIEW_COUNT.ARTICLE_ID,
rowNumber().over(
orderBy(ARTICLE_VIEW_COUNT.VIEW_COUNT.desc(), ARTICLE_VIEW_COUNT.ARTICLE_ID.desc())
).`as`("offset")
).from(ARTICLE_VIEW_COUNT)
.where(ARTICLE_VIEW_COUNT.DELETED_AT.isNull)
Comment on lines -59 to +63
Copy link
Member Author

Choose a reason for hiding this comment

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

  • as-is: VIEW_COUNT 로만 내림 차순 하여 10개 가져옴
  • to-be: VIEW_COUNT가 같은 경우 ARTICLE_ID를 내림 차순하는 조건 추가(최신 글 우선)

Copy link
Member Author

Choose a reason for hiding this comment

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

CREATE TABLE ARTICLE_VIEW_COUNT
(
    article_id  BIGINT  NOT NULL,
    view_count  BIGINT  NOT NULL,
    category_cd TINYINT NOT NULL,
    deleted_at  TIMESTAMP NULL DEFAULT NULL,
    CONSTRAINT article_view_count_pk PRIMARY KEY (article_id)
);

위와 같이 article_id에 인덱싱이 되어 있어 인덱스 타고 갑니다

.asTable("row_rank_tb")
)
.where(field("row_rank_tb.${ARTICLE_VIEW_COUNT.ARTICLE_ID.name}")!!.eq(query.articleId))
Expand All @@ -73,26 +72,22 @@ class ArticleViewCountDao(
.toSet()
}

fun selectArticlesOrderByViewsQuery(query: SelectArticlesOrderByViewsQuery): SelectLimitPercentStep<Record2<Any, Any>> {
val articleViewCountOffsetTb = select()
.from(ARTICLE_VIEW_COUNT)
.where(ARTICLE_VIEW_COUNT.DELETED_AT.isNull)
.orderBy(ARTICLE_VIEW_COUNT.VIEW_COUNT.desc())
.limit(query.offset, Long.MAX_VALUE)
.asTable("article_view_count_offset_tb")

val baseQuery = dslContext.select(
fun selectArticlesOrderByViewsQuery(query: SelectArticlesOrderByViewsQuery) = dslContext
.select(
field("article_view_count_offset_tb.article_id").`as`(SelectArticleViewsRecord::articleId.name),
field("article_view_count_offset_tb.view_count").`as`(SelectArticleViewsRecord::views.name)
)
.from(articleViewCountOffsetTb)

return if (query.category != null) {
baseQuery.where(field("article_view_count_offset_tb.category_cd").eq(query.category.code))
.limit(10)
} else {
baseQuery
.limit(10)
} // TODO: .query로 리턴하면 리턴 타입이 달라져 못받는 문제
}
).from(
select()
.from(ARTICLE_VIEW_COUNT)
.where(ARTICLE_VIEW_COUNT.DELETED_AT.isNull)
.orderBy(ARTICLE_VIEW_COUNT.VIEW_COUNT.desc(), ARTICLE_VIEW_COUNT.ARTICLE_ID.desc())
.limit(query.offset, Long.MAX_VALUE)
.asTable("article_view_count_offset_tb")
).where(
when (query.category) {
(null) -> noCondition()
else -> field("article_view_count_offset_tb.category_cd").eq(query.category.code)
}
).limit(10)
Comment on lines +86 to +91
Copy link
Member Author

Choose a reason for hiding this comment

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

카테고리 옵션이 추가로 온 경우에 대해 동적 쿼리 적용했습니다

.query
}
Loading