Skip to content

Commit

Permalink
[Fix/#281] 아티클 조회수 순 정렬시 옵션 추가(article최신순) (#282)
Browse files Browse the repository at this point in the history
* fix: 아티클 조회수 순 정렬시 옵션 추가(article최신순)

* fix: ARTICLE_VIEW_COUNT 조회시 DELETED_AT is null 옵션 추가
  • Loading branch information
hun-ca authored Aug 3, 2024
1 parent 4e31047 commit bf06924
Showing 1 changed file with 23 additions and 28 deletions.
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)
.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)
.query
}

0 comments on commit bf06924

Please sign in to comment.