Skip to content

Commit

Permalink
feat: 좋아요 TopN 모두르이 답변 조회 기능 구현 #32
Browse files Browse the repository at this point in the history
  • Loading branch information
PgmJun committed Mar 28, 2024
1 parent ef37903 commit 9f3d0cb
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.th.plu.api.controller.answer.dto.response.AnswerInfoResponse
import com.th.plu.api.controller.answer.dto.response.EveryAnswerInfoResponse
import com.th.plu.api.service.answer.AnswerService
import com.th.plu.common.dto.response.ApiResponse
import com.th.plu.domain.domain.answer.dto.EveryAnswerRetrievePageResponses
import com.th.plu.domain.domain.answer.dto.EveryAnswerRetrieveResponses
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.web.bind.annotation.*
Expand Down Expand Up @@ -49,12 +49,21 @@ class AnswerController(
}

@Auth
@Operation(summary = "[인증] 모두의 답변 조회")
@Operation(summary = "[인증] 모두의 답변 조회(무한 스크롤)")
@GetMapping("/v1/answers")
fun paginateAnswersByCursor(
@RequestParam(defaultValue = Long.MAX_VALUE.toString()) lastAnswerId: Long,
@RequestParam(defaultValue = "10") pageSize: Long,
): ApiResponse<EveryAnswerRetrievePageResponses> {
return ApiResponse.success(answerService.retrieveEveryAnswersWithCursor(lastAnswerId, pageSize))
): ApiResponse<EveryAnswerRetrieveResponses> {
return ApiResponse.success(answerService.findEveryAnswersWithCursor(lastAnswerId, pageSize))
}

@Auth
@Operation(summary = "[인증] 모두의 답변 조회(좋아요 TopN)")
@GetMapping("/v1/answers/popular")
fun getAnswersAboutLikeTopN(
@RequestParam(defaultValue = "10") getCount: Long,
): ApiResponse<EveryAnswerRetrieveResponses> {
return ApiResponse.success(answerService.findEveryAnswersLikeTopN(getCount))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.th.plu.api.service.answer
import com.th.plu.api.controller.answer.dto.response.AnswerInfoResponse
import com.th.plu.api.controller.answer.dto.response.EveryAnswerInfoResponse
import com.th.plu.api.service.like.LikeValidator
import com.th.plu.domain.domain.answer.dto.EveryAnswerRetrievePageResponses
import com.th.plu.domain.domain.answer.dto.EveryAnswerRetrieveResponses
import com.th.plu.domain.domain.answer.explorer.AnswerExplorer
import com.th.plu.domain.domain.answer.explorer.QuestionExplorer
import com.th.plu.domain.domain.answer.repository.AnswerRepository
Expand Down Expand Up @@ -55,17 +55,24 @@ class AnswerService(
}

@Transactional(readOnly = true)
fun retrieveEveryAnswersWithCursor(lastAnswerId: Long, pageSize: Long): EveryAnswerRetrievePageResponses {
fun findEveryAnswersWithCursor(lastAnswerId: Long, pageSize: Long): EveryAnswerRetrieveResponses {
val todayQuestionId = questionExplorer.findTodayQuestion().id
val answerInfos = answerRepository.findEveryAnswersWithCursorAndPageSize(todayQuestionId!!, lastAnswerId, pageSize)
return EveryAnswerRetrievePageResponses(answerInfos)
val answers = answerRepository.findEveryAnswersWithCursorAndPageSize(todayQuestionId!!, lastAnswerId, pageSize)
return EveryAnswerRetrieveResponses(answers)
}

@Transactional(readOnly = true)
fun findEveryAnswerInfo(): EveryAnswerInfoResponse {
val todayQuestion = questionExplorer.findTodayQuestion()
val answerCount = answerRepository.findPublicAnswerCountByQuestionId(todayQuestion.id!!)
val answerCount = answerRepository.findPublicAnswersCountByQuestionId(todayQuestion.id!!)

return EveryAnswerInfoResponse.of(todayQuestion, answerCount)
}

fun findEveryAnswersLikeTopN(getCount: Long): EveryAnswerRetrieveResponses {
val todayQuestion = questionExplorer.findTodayQuestion()
val answers = answerRepository.findPublicAnswersLikeTopN(todayQuestion.id!!, getCount)

return EveryAnswerRetrieveResponses(answers)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package com.th.plu.domain.domain.answer.dto

import com.querydsl.core.annotations.QueryProjection

data class EveryAnswerRetrievePageResponse @QueryProjection constructor(
data class EveryAnswerRetrieveResponse @QueryProjection constructor(
val answerId: Long,
val likeCount: Long,
val answerContent: String
)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.th.plu.domain.domain.answer.dto

data class EveryAnswerRetrieveResponses(val answerInfos: List<EveryAnswerRetrieveResponse>)
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.th.plu.domain.domain.answer.repository

import com.th.plu.domain.domain.answer.Answer
import com.th.plu.domain.domain.answer.dto.EveryAnswerRetrievePageResponse
import com.th.plu.domain.domain.answer.dto.EveryAnswerRetrieveResponse

interface AnswerRepositoryCustom {
fun findAnswerById(id: Long): Answer?

fun findEveryAnswersWithCursorAndPageSize(questionId: Long, lastAnswerId: Long, pageSize: Long): List<EveryAnswerRetrievePageResponse>
fun findEveryAnswersWithCursorAndPageSize(questionId: Long, lastAnswerId: Long, pageSize: Long): List<EveryAnswerRetrieveResponse>

fun findPublicAnswerCountByQuestionId(questionId: Long): Long
fun findPublicAnswersCountByQuestionId(questionId: Long): Long

fun findPublicAnswersLikeTopN(questionId: Long, getCount: Long): List<EveryAnswerRetrieveResponse>
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package com.th.plu.domain.domain.answer.repository
import com.querydsl.jpa.impl.JPAQueryFactory
import com.th.plu.domain.domain.answer.Answer
import com.th.plu.domain.domain.answer.QAnswer.answer
import com.th.plu.domain.domain.answer.dto.EveryAnswerRetrievePageResponse
import com.th.plu.domain.domain.answer.dto.QEveryAnswerRetrievePageResponse
import com.th.plu.domain.domain.answer.dto.EveryAnswerRetrieveResponse
import com.th.plu.domain.domain.answer.dto.QEveryAnswerRetrieveResponse
import com.th.plu.domain.domain.like.QLike.like
import org.springframework.stereotype.Repository

Expand All @@ -18,9 +18,9 @@ class AnswerRepositoryImpl(private val queryFactory: JPAQueryFactory) : AnswerRe
.fetchOne()
}

override fun findEveryAnswersWithCursorAndPageSize(questionId: Long, lastAnswerId: Long, pageSize: Long): List<EveryAnswerRetrievePageResponse> {
override fun findEveryAnswersWithCursorAndPageSize(questionId: Long, lastAnswerId: Long, pageSize: Long): List<EveryAnswerRetrieveResponse> {
return queryFactory
.select(QEveryAnswerRetrievePageResponse(answer.id, like.answer.id.count(), answer.content))
.select(QEveryAnswerRetrieveResponse(answer.id, like.answer.id.count(), answer.content))
.from(answer)
.leftJoin(like).on(like.answer.id.eq(answer.id))
.where(
Expand All @@ -34,11 +34,26 @@ class AnswerRepositoryImpl(private val queryFactory: JPAQueryFactory) : AnswerRe
.fetch()
}

override fun findPublicAnswerCountByQuestionId(questionId: Long): Long {
override fun findPublicAnswersCountByQuestionId(questionId: Long): Long {
return queryFactory
.select(answer.id.count())
.from(answer)
.where(answer.question.id.eq(questionId))
.fetchOne()!!
}

override fun findPublicAnswersLikeTopN(questionId: Long, getCount: Long): List<EveryAnswerRetrieveResponse> {
return queryFactory
.select(QEveryAnswerRetrieveResponse(answer.id, like.answer.id.count(), answer.content))
.from(answer)
.leftJoin(like).on(like.answer.id.eq(answer.id))
.where(
answer.isPublic.eq(true),
answer.question.id.eq(questionId)
)
.groupBy(answer.id)
.orderBy(like.answer.id.count().desc())
.limit(getCount)
.fetch()
}
}

0 comments on commit 9f3d0cb

Please sign in to comment.