Skip to content

Commit

Permalink
fix: 랭크 로직 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
rlaisqls authored Oct 30, 2023
2 parents 17ec635 + 0d5a6e5 commit fbeb42c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 34 deletions.
28 changes: 16 additions & 12 deletions src/main/kotlin/kr/hs/dsm/inq/common/util/PageUtil.kt
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
package kr.hs.dsm.inq.common.util

import com.querydsl.jpa.impl.JPAQuery
import kr.hs.dsm.inq.common.util.PageUtil.getLimit
import kr.hs.dsm.inq.common.util.PageUtil.getOffset
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Pageable

val defaultPage: Pageable = PageRequest.of(0, 15)

object PageUtil {
const val pageSize = 5L
private const val pageSize = 5L

private fun <T> List<T>.safeSlice(startIndex: Int, endIndex: Int): List<T> {
val start = startIndex.coerceAtLeast(0)
val end = endIndex.coerceAtMost(size - 1)
return slice(start..end)
}

fun getOffset(page: Long) = page * pageSize
fun getLimit() = pageSize + 1
fun <T> hasNext(list: List<T>) = list.size > pageSize
fun <T> toPageResponse(page: Long, list: List<T>): PageResponse<T> {
val offset = getOffset(page).toInt()
val sliceList = list.safeSlice(offset, offset + pageSize.toInt() - 1)
return PageResponse(
list = sliceList,
hasNext = list.safeSlice(offset, offset + pageSize.toInt()).size > sliceList.size
)
}
}

/**
* JPAQuery에 주어진 page에 대한 offset과 limit을 지정하는 extension function
*/
fun <T> JPAQuery<T>.offsetAndLimit(page: Long): JPAQuery<T> =
offset(getOffset(page))
.limit(getLimit())

/**
* repository 계층에서 page 결과를 반환하기 위한 class
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import com.querydsl.jpa.impl.JPAQuery
import com.querydsl.jpa.impl.JPAQueryFactory
import kr.hs.dsm.inq.common.util.PageResponse
import kr.hs.dsm.inq.common.util.PageUtil
import kr.hs.dsm.inq.common.util.offsetAndLimit
import kr.hs.dsm.inq.domain.question.persistence.*
import kr.hs.dsm.inq.domain.question.persistence.QQuestionSets.questionSets
import kr.hs.dsm.inq.domain.question.persistence.QQuestionSolvingHistory.questionSolvingHistory
import kr.hs.dsm.inq.domain.question.persistence.QQuestionTags.questionTags
import kr.hs.dsm.inq.domain.question.persistence.QTags.tags
import kr.hs.dsm.inq.domain.question.persistence.dto.QQuestionSetDetailDto
import kr.hs.dsm.inq.domain.question.persistence.dto.QQuestionSetDto
import kr.hs.dsm.inq.domain.question.persistence.dto.QuestionSetDetailDto
Expand Down Expand Up @@ -46,19 +47,18 @@ class CustomQuestionSetsRepositoryImpl(
category: Category?,
keyword: String?,
tags: List<String>?,
page: Long)
: PageResponse<QuestionSetDto> {
page: Long
): PageResponse<QuestionSetDto> {
val questionSetResponse = queryFactory
.selectFrom(questionSets)
.where(
questionSets.name.contains(keyword ?: "")
.and(category?.let { questionSets.category.eq(it)})
)
.offsetAndLimit(page)
.getQuestionSetDto(user)

return PageResponse<QuestionSetDto>(
hasNext = PageUtil.hasNext(questionSetResponse),
return PageUtil.toPageResponse(
page = page,
list = questionSetResponse
)
}
Expand All @@ -84,8 +84,8 @@ class CustomQuestionSetsRepositoryImpl(

private fun <T> JPAQuery<T>.getQuestionSetDto(user: User): MutableList<QuestionSetDto> {
val author = QUser("writer")
return leftJoin(QQuestionTags.questionTags).on(QQuestionTags.questionTags.problems.eq(questionSets.problemId))
.leftJoin(QTags.tags).on(QTags.tags.id.eq(QQuestionTags.questionTags.id.tagId))
return leftJoin(questionTags).on(questionTags.problems.eq(questionSets.problemId))
.leftJoin(tags).on(tags.id.eq(questionTags.id.tagId))
.leftJoin(questionSolvingHistory)
.on(questionSolvingHistory.userId.id.eq(user.id)).on(questionSolvingHistory.problem.eq(questionSets.problemId))
.innerJoin(author).on(author.id.eq(questionSets.authorId.id))
Expand All @@ -102,7 +102,7 @@ class CustomQuestionSetsRepositoryImpl(
/* username = */ author.username,
/* job = */ author.job,
/* jobDuration = */ author.jobDuration,
/* tagList = */ GroupBy.list(QTags.tags),
/* tagList = */ GroupBy.list(tags),
/* isAnswered = */ questionSolvingHistory.isNotNull,
/* likeCount = */ questionSets.likeCount,
/* viewCount = */ questionSets.viewCount,
Expand All @@ -116,8 +116,8 @@ class CustomQuestionSetsRepositoryImpl(
val author = QUser("writer")
val liked = QLike("liked")
val favorite = QFavorite("favorite")
return@run leftJoin(QQuestionTags.questionTags).on(QQuestionTags.questionTags.problems.eq(questionSets.problemId))
.leftJoin(QTags.tags).on(QTags.tags.id.eq(QQuestionTags.questionTags.id.tagId))
return@run leftJoin(questionTags).on(questionTags.problems.eq(questionSets.problemId))
.leftJoin(tags).on(tags.id.eq(questionTags.id.tagId))
.innerJoin(author).on(author.id.eq(questionSets.authorId.id))
.leftJoin(liked).on(liked.id.userId.eq(user.id)).on(liked.post.eq(questionSets.postId))
.leftJoin(favorite).on(favorite.id.userId.eq(user.id)).on(favorite.problemId.eq(questionSets.problemId))
Expand All @@ -137,7 +137,7 @@ class CustomQuestionSetsRepositoryImpl(
/* isLiked = */ liked.isLiked.isTrue,
/* isDisliked = */ liked.isLiked.isFalse,
/* isFavorite = */ favorite.isNotNull,
/* tagList = */ GroupBy.list(QTags.tags),
/* tagList = */ GroupBy.list(tags),
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.querydsl.jpa.impl.JPAQuery
import com.querydsl.jpa.impl.JPAQueryFactory
import kr.hs.dsm.inq.common.util.PageResponse
import kr.hs.dsm.inq.common.util.PageUtil
import kr.hs.dsm.inq.common.util.offsetAndLimit
import kr.hs.dsm.inq.domain.question.persistence.Category
import kr.hs.dsm.inq.domain.question.persistence.QQuestionTags.questionTags
import kr.hs.dsm.inq.domain.question.persistence.QQuestions.questions
Expand Down Expand Up @@ -50,17 +49,18 @@ class CustomQuestionRepositoryImpl(
keyword: String?,
tagList: List<String>
): PageResponse<QuestionDto> {
val keyword = keyword ?: ""
val questionList = queryFactory
.selectFrom(questions)
.where(
category?.let { questions.category.eq(it)
.and((keyword ?: "").let { questions.question.contains(keyword) }) }
questions.question.contains(keyword)
.and(category?.let { questions.category.eq(it)})
)
.orderBy(questions.likeCount.asc())
.offsetAndLimit(page)
.getQuestionDto(user)
return PageResponse(
hasNext = PageUtil.hasNext(questionList),

return PageUtil.toPageResponse(
page = page,
list = questionList.filter { it.tagList.map { it.tag }.containsAll(tagList) }
)
}
Expand All @@ -69,10 +69,10 @@ class CustomQuestionRepositoryImpl(
val questionList = queryFactory
.selectFrom(questions)
.orderBy(questions.answerCount.asc())
.offsetAndLimit(page)
.getQuestionDto(user)
return PageResponse(
hasNext = PageUtil.hasNext(questionList),

return PageUtil.toPageResponse(
page = page,
list = questionList
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ data class QuestionRankResponse(
QuestionListResponse(
hasNext = hasNext,
questionList = list.mapIndexed { idx, it ->
QuestionResponse.of(it, PageUtil.getOffset(page) + idx)
QuestionResponse.of(it, PageUtil.getOffset(page) + idx + 1)
}
)
}
Expand Down

0 comments on commit fbeb42c

Please sign in to comment.