diff --git a/src/main/kotlin/dsm/pick2024/domain/weekendmeal/service/QueryWeekendMealApplicationService.kt b/src/main/kotlin/dsm/pick2024/domain/weekendmeal/service/QueryWeekendMealApplicationService.kt index f23c621d..cf07dacc 100644 --- a/src/main/kotlin/dsm/pick2024/domain/weekendmeal/service/QueryWeekendMealApplicationService.kt +++ b/src/main/kotlin/dsm/pick2024/domain/weekendmeal/service/QueryWeekendMealApplicationService.kt @@ -1,10 +1,12 @@ package dsm.pick2024.domain.weekendmeal.service +import dsm.pick2024.domain.weekendmeal.domain.WeekendMealPeriod import dsm.pick2024.domain.weekendmeal.port.`in`.QueryWeekendMealApplicationUseCase import dsm.pick2024.domain.weekendmeal.port.out.QueryWeekendMealPeriodPort import dsm.pick2024.domain.weekendmeal.presentation.dto.response.QueryWeekendMealStatusResponse import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional +import java.time.LocalDate import java.time.LocalDate.* import java.time.ZoneId @@ -19,16 +21,27 @@ class QueryWeekendMealApplicationService( val periods = queryWeekendMealPeriodPort.queryAllWeekendMeal() - val period = periods.find { - (today.isEqual(it.start) || today.isAfter(it.start)) && today.isBefore(it.end.plusDays(1)) - } + val currentPeriod = findCurrentPeriod(today, periods) + + val nextPeriod = currentPeriod ?: findNextPeriod(today, periods) - val status = period != null - val month = period?.month?.value + val status = currentPeriod != null + val month = nextPeriod?.month?.value return QueryWeekendMealStatusResponse( status, month ) } + + private fun findCurrentPeriod(today: LocalDate, periods: List): WeekendMealPeriod? { + return periods.find { + (today.isEqual(it.start) || today.isAfter(it.start)) && today.isBefore(it.end.plusDays(1)) + } + } + + private fun findNextPeriod(today: LocalDate, periods: List): WeekendMealPeriod? { + return periods.firstOrNull { it.start.isAfter(today) } + ?: periods.lastOrNull { it.end.isBefore(today) } + } }