Skip to content

Commit

Permalink
⚡️: fix findTodayQuestion, LocalDateTime to LocaDate로 수정 #38
Browse files Browse the repository at this point in the history
  • Loading branch information
sookyungg committed Apr 6, 2024
1 parent 0515645 commit 644c4cd
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class QuestionService(
) {
@Transactional(readOnly = true)
fun getQuestionToday(memberId: Long): QuestionResultDto {
val today = LocalDateTime.now()
val today = LocalDateTime.now().toLocalDate()

return questionExplorer.findQuestionDate(today).let { todayQuestion ->
val answered = answerExplorer.hasAnswered(memberId, todayQuestion.id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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.LocalDate
import java.time.YearMonth

@Component
Expand All @@ -17,8 +17,8 @@ class QuestionExplorer(
questionRepository.findById(id).orElse(null)
?: throw NotFoundException(ErrorCode.NOT_FOUND_QUESTION_EXCEPTION, "존재하지 않는 질문 $id 입니다")

fun findQuestionDate(date: LocalDateTime): Question =
questionRepository.findByExposedAtOrNull(date) ?: throw InternalServerException(
fun findQuestionDate(date: LocalDate): Question =
questionRepository.findByExposedAtDateOrNull(date) ?: throw InternalServerException(
ErrorCode.DATA_NOT_READY_EXCEPTION,
"($date) 날짜의 질문데이터가 준비되지 않았습니다. "
)
Expand All @@ -32,7 +32,7 @@ class QuestionExplorer(
.toSet() // application 에서 중복 처리중, 500 넘는 warn log 발생시 월별 1건 조회하도록 쿼리 개선 필요!

fun findTodayQuestion(): Question {
return findQuestionDate(LocalDateTime.now())
return findQuestionDate(LocalDate.now())
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.th.plu.domain.domain.question.repository

import com.th.plu.domain.domain.question.Question
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.YearMonth
import java.util.*


interface QuestionRepositoryCustom {
Expand All @@ -15,6 +15,6 @@ interface QuestionRepositoryCustom {

fun findAllExposedAtInAnsweredMonth(memberId: Long): List<LocalDateTime>

fun findTodayQuestion(): Question?
fun findByExposedAtDateOrNull(exposedAt: LocalDate): Question?

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

Expand All @@ -27,6 +28,16 @@ class QuestionRepositoryImpl(private val queryFactory: JPAQueryFactory) : Questi
.fetchOne()
}

override fun findByExposedAtDateOrNull(exposedAtDate: LocalDate): Question? {
val startOfDay = exposedAtDate.atStartOfDay() // 해당 날짜의 00:00시
val endOfDay = exposedAtDate.plusDays(1).atStartOfDay().minusSeconds(1) // 다음 날짜의 00:00시에서 1초를 빼서 현재 날짜의 23:59:59로 설정

return queryFactory
.selectFrom(question)
.where(question.exposedAt.between(startOfDay, endOfDay))
.fetchFirst() // 해당 날짜의 첫 번째 질문 결과만 반환하거나, 결과가 없을 경우 null 반환
}

override fun findAllByExposedMonthIn(memberId: Long, yearMonth: YearMonth): List<Question> {
return queryFactory
.selectFrom(question)
Expand Down Expand Up @@ -71,11 +82,4 @@ class QuestionRepositoryImpl(private val queryFactory: JPAQueryFactory) : Questi
}
}

override fun findTodayQuestion(): Question? {
val today = LocalDateTime.now()
return queryFactory
.selectFrom(question)
.where(question.questionDate.eq(today))
.fetchOne()
}
}

0 comments on commit 644c4cd

Please sign in to comment.