Skip to content

Commit

Permalink
⚡️: fix Answer, Question 관련 오류 수정 #38
Browse files Browse the repository at this point in the history
  • Loading branch information
sookyungg committed Apr 6, 2024
1 parent 8f708e5 commit 8316dc2
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import com.th.plu.domain.domain.like.Like
import com.th.plu.domain.domain.like.explorer.LikeExplorer
import com.th.plu.domain.domain.like.repository.LikeRepository
import com.th.plu.domain.domain.member.explorer.MemberExplorer
import com.th.plu.domain.domain.question.QuestionExplorer
import com.th.plu.domain.domain.question.explorer.QuestionExplorer
import com.th.plu.domain.isUniqueError
import org.springframework.dao.DataIntegrityViolationException
import org.springframework.stereotype.Service
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.th.plu.api.service.question

import com.th.plu.domain.domain.answer.AnswerExplorer
import com.th.plu.domain.domain.question.QuestionExplorer
import com.th.plu.domain.domain.answer.explorer.AnswerExplorer
import com.th.plu.domain.domain.question.QuestionResultDto
import com.th.plu.domain.domain.question.explorer.QuestionExplorer
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.time.LocalDateTime
Expand All @@ -17,7 +17,7 @@ class QuestionService(
fun getQuestionToday(memberId: Long): QuestionResultDto {
val today = LocalDateTime.now()

return questionExplorer.findQuestion(today).let { todayQuestion ->
return questionExplorer.findQuestionDate(today).let { todayQuestion ->
val answered = answerExplorer.hasAnswered(memberId, todayQuestion.id)

QuestionResultDto(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ class Answer(
private var _id: Long? = null,

@ManyToOne(fetch = FetchType.LAZY, cascade = [CascadeType.ALL])
@JoinColumn(name = "member_id", nullable = false)
private var member: Member,
@JoinColumn(name = "member_id", nullable = false) var member: Member,

@ManyToOne(fetch = FetchType.LAZY, cascade = [CascadeType.ALL])
@JoinColumn(name = "question_id", nullable = false)
Expand All @@ -53,6 +52,10 @@ class Answer(
private set

val questionId: Long = question.id

fun getLikeCount(): Int {
return likes.size
}
}

fun newAnswerInstance(member: Member, question: Question, content: String, isPublic: Boolean) = Answer(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ import org.springframework.stereotype.Component

@Component
class AnswerExplorer(
private val answerRepository: AnswerRepository
private val answerRepository: AnswerRepository
) {
fun findAnswerById(id: Long): Answer {
return answerRepository.findAnswerById(id)
?: throw NotFoundException(ErrorCode.NOT_FOUND_ANSWER_EXCEPTION, "존재하지 않는 답변(ID: $id) 입니다")
?: throw NotFoundException(ErrorCode.NOT_FOUND_ANSWER_EXCEPTION, "존재하지 않는 답변(ID: $id) 입니다")
}

fun hasAnswered(memberId: Long, questionId: Long): Boolean {
return answerRepository.existsByMemberIdAndQuestionId(memberId, questionId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,48 +13,48 @@ import org.springframework.stereotype.Repository
class AnswerRepositoryImpl(private val queryFactory: JPAQueryFactory) : AnswerRepositoryCustom {
override fun findAnswerById(id: Long): Answer? {
return queryFactory
.selectFrom(answer)
.where(answer.id.eq(id))
.fetchOne()
.selectFrom(answer)
.where(answer._id.eq(id))
.fetchOne()
}

override fun findEveryAnswersWithCursorAndPageSize(questionId: Long, lastAnswerId: Long, pageSize: 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),
answer.id.lt(lastAnswerId),
)
.groupBy(answer.id)
.orderBy(answer.id.desc())
.limit(pageSize)
.fetch()
.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),
answer._id.lt(lastAnswerId),
)
.groupBy(answer._id)
.orderBy(answer._id.desc())
.limit(pageSize)
.fetch()
}

override fun findPublicAnswersCountByQuestionId(questionId: Long): Long {
return queryFactory
.select(answer.id.count())
.from(answer)
.where(answer.question.id.eq(questionId))
.fetchOne()!!
.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()
.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()
}

override fun existsByMemberIdAndQuestionId(memberId: Long, questionId: Long): Boolean {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
package com.th.plu.domain.domain.answer.explorer
package com.th.plu.domain.domain.question.explorer

import com.th.plu.common.exception.code.ErrorCode
import com.th.plu.common.exception.model.InternalServerException
import com.th.plu.common.exception.model.NotFoundException
import com.th.plu.domain.domain.question.Question
import com.th.plu.domain.domain.question.repository.QuestionRepository
import org.springframework.stereotype.Component
import java.time.LocalDateTime
import java.time.YearMonth

@Component
class QuestionExplorer(
private val questionRepository: QuestionRepository
private val questionRepository: QuestionRepository,
) {
fun findQuestionById(id: Long): Question {
return questionRepository.findQuestionById(id)
?: throw NotFoundException(ErrorCode.NOT_FOUND_QUESTION_EXCEPTION, "존재하지 않는 질문(ID: $id) 입니다")
}
fun findQuestion(id: Long): Question =
questionRepository.findById(id).orElse(null)
?: throw NotFoundException(ErrorCode.NOT_FOUND_QUESTION_EXCEPTION, "존재하지 않는 질문 $id 입니다")

fun findQuestionDate(date: LocalDateTime): Question =
questionRepository.findByExposedAtOrNull(date) ?: throw InternalServerException(
ErrorCode.DATA_NOT_READY_EXCEPTION,
"($date) 날짜의 질문데이터가 준비되지 않았습니다. "
)

fun findMyQuestionsMonthly(memberId: Long, yearMonth: YearMonth): List<Question> =
questionRepository.findAllByExposedMonthIn(memberId, yearMonth)

fun findAnsweredYearMonth(memberId: Long): Set<YearMonth> =
questionRepository.findAllExposedAtInAnsweredMonth(memberId)
.map { YearMonth.of(it.year, it.monthValue) }
.toSet() // application 에서 중복 처리중, 500 넘는 warn log 발생시 월별 1건 조회하도록 쿼리 개선 필요!

fun findTodayQuestion(): Question {
return questionRepository.findTodayQuestion()
?: throw NotFoundException(ErrorCode.NOT_FOUND_QUESTION_EXCEPTION, "오늘의 질문이 존재하지 않습니다")
return findQuestionDate(LocalDateTime.now())
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.th.plu.domain.domain.question.repository
import com.th.plu.domain.domain.question.Question
import java.time.LocalDateTime
import java.time.YearMonth
import java.util.*


interface QuestionRepositoryCustom {
Expand All @@ -15,4 +16,5 @@ interface QuestionRepositoryCustom {
fun findAllExposedAtInAnsweredMonth(memberId: Long): List<LocalDateTime>

fun findTodayQuestion(): Question?

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.th.plu.domain.domain.question.Question
import org.springframework.stereotype.Repository
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.YearMonth

@Repository
Expand Down Expand Up @@ -74,10 +75,14 @@ class QuestionRepositoryImpl(private val queryFactory: JPAQueryFactory) : Questi

override fun findTodayQuestion(): Question? {
val today = LocalDate.now()

// 오늘 날짜의 시작 시간
val startOfDay = today.atStartOfDay()
// 오늘 날짜의 마지막 순간
val endOfDay = today.atTime(LocalTime.MAX)
return queryFactory
.selectFrom(question)
.where(question.questionDate.eq(today))
// questionDate가 하루의 시작과 끝 사이에 있는지 확인
.where(question.questionDate.between(startOfDay, endOfDay))
.fetchOne()
}
}

0 comments on commit 8316dc2

Please sign in to comment.