From 907adad9b5f852b08b05c69b0c65b275bfbeec5f Mon Sep 17 00:00:00 2001 From: sookyung kang Date: Sat, 6 Apr 2024 18:44:07 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F:=20fix=20findTodayQuestion,?= =?UTF-8?q?=20LocalDateTime=20to=20LocaDate=EB=A1=9C=20=EC=88=98=EC=A0=95?= =?UTF-8?q?=20=20#38?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/service/question/QuestionService.kt | 2 +- .../question/explorer/QuestionExplorer.kt | 8 ++++---- .../repository/QuestionRepositoryCustom.kt | 6 +++--- .../repository/QuestionRepositoryImpl.kt | 18 +++++++++++------- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/plu-api/src/main/kotlin/com/th/plu/api/service/question/QuestionService.kt b/plu-api/src/main/kotlin/com/th/plu/api/service/question/QuestionService.kt index ad795ad..9644c06 100644 --- a/plu-api/src/main/kotlin/com/th/plu/api/service/question/QuestionService.kt +++ b/plu-api/src/main/kotlin/com/th/plu/api/service/question/QuestionService.kt @@ -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) diff --git a/plu-domain/src/main/kotlin/com/th/plu/domain/domain/question/explorer/QuestionExplorer.kt b/plu-domain/src/main/kotlin/com/th/plu/domain/domain/question/explorer/QuestionExplorer.kt index 55dd188..75ab84e 100644 --- a/plu-domain/src/main/kotlin/com/th/plu/domain/domain/question/explorer/QuestionExplorer.kt +++ b/plu-domain/src/main/kotlin/com/th/plu/domain/domain/question/explorer/QuestionExplorer.kt @@ -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 @@ -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) 날짜의 질문데이터가 준비되지 않았습니다. " ) @@ -32,7 +32,7 @@ class QuestionExplorer( .toSet() // application 에서 중복 처리중, 500 넘는 warn log 발생시 월별 1건 조회하도록 쿼리 개선 필요! fun findTodayQuestion(): Question { - return findQuestionDate(LocalDateTime.now()) + return findQuestionDate(LocalDate.now()) } } \ No newline at end of file diff --git a/plu-domain/src/main/kotlin/com/th/plu/domain/domain/question/repository/QuestionRepositoryCustom.kt b/plu-domain/src/main/kotlin/com/th/plu/domain/domain/question/repository/QuestionRepositoryCustom.kt index eb94531..844a484 100644 --- a/plu-domain/src/main/kotlin/com/th/plu/domain/domain/question/repository/QuestionRepositoryCustom.kt +++ b/plu-domain/src/main/kotlin/com/th/plu/domain/domain/question/repository/QuestionRepositoryCustom.kt @@ -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 { @@ -15,6 +15,6 @@ interface QuestionRepositoryCustom { fun findAllExposedAtInAnsweredMonth(memberId: Long): List - fun findTodayQuestion(): Question? - + fun findByExposedAtDateOrNull(exposedAt: LocalDate): Question? + } diff --git a/plu-domain/src/main/kotlin/com/th/plu/domain/domain/question/repository/QuestionRepositoryImpl.kt b/plu-domain/src/main/kotlin/com/th/plu/domain/domain/question/repository/QuestionRepositoryImpl.kt index b822288..cd3c7f5 100644 --- a/plu-domain/src/main/kotlin/com/th/plu/domain/domain/question/repository/QuestionRepositoryImpl.kt +++ b/plu-domain/src/main/kotlin/com/th/plu/domain/domain/question/repository/QuestionRepositoryImpl.kt @@ -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 @@ -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 { return queryFactory .selectFrom(question) @@ -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() - } } \ No newline at end of file