Skip to content

Commit

Permalink
feat: 즐겨찾기 api
Browse files Browse the repository at this point in the history
  • Loading branch information
rlaisqls committed Nov 6, 2023
1 parent d26ac16 commit 55609c5
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,48 @@
package kr.hs.dsm.inq.domain.question.persistence.repository

import com.querydsl.jpa.impl.JPAQueryFactory
import kr.hs.dsm.inq.common.util.PageResponse
import kr.hs.dsm.inq.domain.question.persistence.Category
import kr.hs.dsm.inq.domain.question.persistence.Problem
import kr.hs.dsm.inq.domain.question.persistence.ProblemType
import kr.hs.dsm.inq.domain.question.persistence.QFavorite
import kr.hs.dsm.inq.domain.question.persistence.QFavorite.favorite
import kr.hs.dsm.inq.domain.question.persistence.QProblem
import kr.hs.dsm.inq.domain.question.persistence.QProblem.problem
import kr.hs.dsm.inq.domain.question.persistence.QuestionSets
import kr.hs.dsm.inq.domain.question.persistence.dto.QuestionSetDetailDto
import kr.hs.dsm.inq.domain.question.persistence.dto.QuestionSetDto
import kr.hs.dsm.inq.domain.user.persistence.User
import org.springframework.data.repository.CrudRepository
import org.springframework.stereotype.Repository

interface ProblemRepository : CrudRepository<Problem, Long>, CustomProblemRepository {
}

interface CustomProblemRepository {
fun queryFavoriteProblem(userId: Long): List<Problem>
fun queryFavoriteProblemSet(userId: Long): List<Problem>
}

@Repository
class CustomProblemRepositoryImpl(
private val queryFactory: JPAQueryFactory
): CustomProblemRepository {

override fun queryFavoriteProblem(userId: Long): List<Problem> {
return queryFactory
.selectFrom(problem)
.innerJoin(favorite).on(favorite.user.id.eq(userId))
.where(problem.type.eq(ProblemType.QUESTION))
.fetch()
}

override fun queryFavoriteProblemSet(userId: Long): List<Problem> {
return queryFactory
.selectFrom(problem)
.innerJoin(favorite).on(favorite.user.id.eq(userId))
.where(problem.type.eq(ProblemType.SET))
.fetch()
}

interface ProblemRepository : CrudRepository<Problem, Long> {
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package kr.hs.dsm.inq.domain.question.persistence.repository

import com.querydsl.core.ResultTransformer
import com.querydsl.core.group.GroupBy
import com.querydsl.core.types.Expression
import com.querydsl.core.types.Ops
import com.querydsl.core.types.dsl.Expressions
import com.querydsl.jpa.impl.JPAQuery
import com.querydsl.jpa.impl.JPAQueryFactory
import kr.hs.dsm.inq.common.util.PageResponse
Expand All @@ -15,7 +11,6 @@ import kr.hs.dsm.inq.domain.question.persistence.QPost.post
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.QQuestions.questions
import kr.hs.dsm.inq.domain.question.persistence.QTags.tags
import kr.hs.dsm.inq.domain.question.persistence.dto.*
import kr.hs.dsm.inq.domain.user.persistence.QUser
Expand All @@ -40,6 +35,8 @@ interface CustomQuestionSetsRepository {
user: User,
id: Long
): QuestionSetDetailDto?

fun queryQuestionSetDtoByProblemIdIn(user: User, problemIds: List<Long>): PageResponse<QuestionSetDto>
}

@Repository
Expand Down Expand Up @@ -88,6 +85,21 @@ class CustomQuestionSetsRepositoryImpl(
return if (questionSetDetailResponse.isEmpty()) null else questionSetDetailResponse[0]
}

override fun queryQuestionSetDtoByProblemIdIn(user: User, problemIds: List<Long>): PageResponse<QuestionSetDto> {
val questionSetList = queryFactory
.selectFrom(questionSets)
.where(
questionSets.problem.id.`in`(problemIds)
)
.orderBy(questionSets.post.likeCount.asc())
.getQuestionSetDto(user)

return PageUtil.toPageResponse(
page = 0,
list = questionSetList
)
}

private fun <T> JPAQuery<T>.getQuestionSetDto(user: User): MutableList<QuestionSetDto> {
val author = QUser("writer")
return leftJoin(questionTags).on(questionTags.problems.eq(questionSets.problem))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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.domain.question.persistence.Category
import kr.hs.dsm.inq.domain.question.persistence.QQuestionSets
import kr.hs.dsm.inq.domain.question.persistence.QQuestionTags.questionTags
import kr.hs.dsm.inq.domain.question.persistence.QQuestions.questions
import kr.hs.dsm.inq.domain.question.persistence.QTags.tags
Expand Down Expand Up @@ -36,6 +37,7 @@ interface CustomQuestionRepository {
fun queryQuestionDtoById(id: Long, user: User): QuestionDto?
fun queryQuestionDetailDtoById(user: User, questionId: Long): QuestionDetailDto?
fun queryQuestionDto(user: User): List<QuestionDto>
fun queryQuestionDtoByProblemIdIn(user: User, problemIds: List<Long>): PageResponse<QuestionDto>
}

@Repository
Expand Down Expand Up @@ -75,6 +77,20 @@ class CustomQuestionRepositoryImpl(
.getQuestionDto(user)
}

override fun queryQuestionDtoByProblemIdIn(user: User, problemIds: List<Long>): PageResponse<QuestionDto> {
val questionList = queryFactory
.selectFrom(QQuestionSets.questionSets)
.where(
questions.problem.id.`in`(problemIds)
)
.getQuestionDto(user)

return PageUtil.toPageResponse(
page = 0,
list = questionList
)
}

override fun queryQuestionDtoOrderByAnswerCount(user: User, page: Long): PageResponse<QuestionDto> {
val questionList = queryFactory
.selectFrom(questions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,21 @@ class QuestionController(
return questionService.getPopularQuestion()
}

@GetMapping("/popular/set")
fun getPopularQuestionSet(): QuestionSetResponse {
@GetMapping("/set/popular")
fun getPopularQuestionSet(): QuestionSetListResponse {
return questionService.getPopularQuestionSet()
}

@GetMapping("/favorite")
fun getFavoriteQuestion(): QuestionListResponse {
return questionService.getFavoriteQuestion()
}

@GetMapping("/favorite/set")
fun getFavoriteQuestionSet(): QuestionSetListResponse {
return questionService.getFavoriteQuestionSet()
}

@GetMapping("/{question-id}")
fun getQuestionDetail(@PathVariable("question-id") questionId: Long): QuestionDetailResponse {
return questionService.getQuestionDetail(questionId)
Expand Down Expand Up @@ -104,7 +114,7 @@ class QuestionController(
}

@GetMapping("/set")
fun getQuestionSets(@Valid @ModelAttribute request: GetQuestionSetsRequest): QuestionSetResponse{
fun getQuestionSets(@Valid @ModelAttribute request: GetQuestionSetsRequest): QuestionSetListResponse{
return questionService.getQuestionSet(request)
}

Expand Down Expand Up @@ -140,7 +150,7 @@ class QuestionController(
}

@GetMapping("/set/rank")
fun getQuestionSetRank(@Valid @ModelAttribute request: GetQuestionSetRankRequest): QuestionSetResponse {
fun getQuestionSetRank(@Valid @ModelAttribute request: GetQuestionSetRankRequest): QuestionSetListResponse {
return questionService.getQuestionSetRank(request)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,13 @@ data class RegisterQuestionSetsResponse(
val isFavorite: Boolean
)

data class QuestionSetResponse(
data class QuestionSetListResponse(
val hasNext: Boolean,
val questionSetsList: List<QuestionSetsResponse>,
) {
companion object {
fun of(pageResponse: PageResponse<QuestionSetDto>) = pageResponse.run {
QuestionSetResponse(
QuestionSetListResponse(
hasNext = hasNext,
questionSetsList = list.map { QuestionSetsResponse.of(it) }
)
Expand All @@ -209,7 +209,7 @@ data class QuestionSetRankResponse(
) {
companion object {
fun of(page: Long, pageResponse: PageResponse<QuestionSetDto>) = pageResponse.run {
QuestionSetResponse(
QuestionSetListResponse(
hasNext = hasNext,
questionSetsList = list.mapIndexed { idx, it ->
QuestionSetsResponse.of(it, PageUtil.getOffset(page) + idx + 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,23 +153,38 @@ class QuestionService(
}

fun getPopularQuestion(): QuestionListResponse {

val user = SecurityUtil.getCurrentUser()
val questionList = questionsRepository.queryQuestionDtoOrderByAnswerCount(
user = user,
page = 1L
)

return QuestionListResponse.of(questionList)
}

fun getPopularQuestionSet(): QuestionSetResponse {
fun getPopularQuestionSet(): QuestionSetListResponse {
val user = SecurityUtil.getCurrentUser()
val questionSetList = questionSetsRepository.queryQuestionSetDtoOrderByLike(
user = user,
page = 1L
)
return QuestionSetResponse.of(questionSetList)
return QuestionSetListResponse.of(questionSetList)
}

fun getFavoriteQuestion(): QuestionListResponse {
val user = SecurityUtil.getCurrentUser()
val problems = problemRepository.queryFavoriteProblemSet(user.id)
val questionList = questionsRepository.queryQuestionDtoByProblemIdIn(user, problems.map { it.id })
return QuestionListResponse.of(questionList)
}

fun getFavoriteQuestionSet(): QuestionSetListResponse {
val user = SecurityUtil.getCurrentUser()
val problems = problemRepository.queryFavoriteProblem(user.id)
val questionSetList = questionSetsRepository.queryQuestionSetDtoByProblemIdIn(
user = user,
problemIds = problems.map { it.id }
)
return QuestionSetListResponse.of(questionSetList)
}

fun getQuestionDetail(questionId: Long): QuestionDetailResponse {
Expand Down Expand Up @@ -395,7 +410,7 @@ class QuestionService(
)
}

fun getQuestionSet(request: GetQuestionSetsRequest): QuestionSetResponse {
fun getQuestionSet(request: GetQuestionSetsRequest): QuestionSetListResponse {
val user = SecurityUtil.getCurrentUser()

val questionSetList = request.run{
Expand All @@ -408,7 +423,7 @@ class QuestionService(
)
}

return QuestionSetResponse.of(questionSetList)
return QuestionSetListResponse.of(questionSetList)
}

fun getQuestionSetDetail(questionSetId: Long): GetQuestionSetDetailResponse {
Expand Down Expand Up @@ -481,7 +496,7 @@ class QuestionService(
)
}

fun getQuestionSetRank(request: GetQuestionSetRankRequest): QuestionSetResponse {
fun getQuestionSetRank(request: GetQuestionSetRankRequest): QuestionSetListResponse {
val user = SecurityUtil.getCurrentUser()

val questionSetList = questionSetsRepository.queryQuestionSetDtoOrderByLike(
Expand Down

0 comments on commit 55609c5

Please sign in to comment.