11package dsm.pick2024.domain.weekendmeal.service
22
3+ import dsm.pick2024.domain.weekendmeal.domain.WeekendMealPeriod
34import dsm.pick2024.domain.weekendmeal.port.`in`.QueryWeekendMealApplicationUseCase
45import dsm.pick2024.domain.weekendmeal.port.out.QueryWeekendMealPeriodPort
56import dsm.pick2024.domain.weekendmeal.presentation.dto.response.QueryWeekendMealStatusResponse
67import org.springframework.stereotype.Service
78import org.springframework.transaction.annotation.Transactional
9+ import java.time.LocalDate
810import java.time.LocalDate.*
911import java.time.ZoneId
1012
@@ -19,16 +21,27 @@ class QueryWeekendMealApplicationService(
1921
2022 val periods = queryWeekendMealPeriodPort.queryAllWeekendMeal()
2123
22- val period = periods.find {
23- (today.isEqual(it.start) || today.isAfter(it.start)) && today.isBefore(it.end.plusDays( 1 ))
24- }
24+ val currentPeriod = findCurrentPeriod(today, periods)
25+
26+ val nextPeriod = currentPeriod ? : findNextPeriod(today, periods)
2527
26- val status = period != null
27- val month = period ?.month?.value
28+ val status = currentPeriod != null
29+ val month = nextPeriod ?.month?.value
2830
2931 return QueryWeekendMealStatusResponse (
3032 status,
3133 month
3234 )
3335 }
36+
37+ private fun findCurrentPeriod (today : LocalDate , periods : List <WeekendMealPeriod >): WeekendMealPeriod ? {
38+ return periods.find {
39+ (today.isEqual(it.start) || today.isAfter(it.start)) && today.isBefore(it.end.plusDays(1 ))
40+ }
41+ }
42+
43+ private fun findNextPeriod (today : LocalDate , periods : List <WeekendMealPeriod >): WeekendMealPeriod ? {
44+ return periods.firstOrNull { it.start.isAfter(today) }
45+ ? : periods.lastOrNull { it.end.isBefore(today) }
46+ }
3447}
0 commit comments