-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
24 additions
and
10 deletions.
There are no files selected for viewing
6 changes: 4 additions & 2 deletions
6
.../dsm/pick2024/domain/weekendmeal/presentation/dto/response/QueryIsPeriodStatusResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
package dsm.pick2024.domain.weekendmeal.presentation.dto.response | ||
|
||
import java.time.LocalDate | ||
|
||
data class QueryIsPeriodStatusResponse( | ||
val status: Boolean, | ||
val start: String, | ||
val end: String | ||
val start: LocalDate?, | ||
val end: LocalDate? | ||
) |
28 changes: 20 additions & 8 deletions
28
src/main/kotlin/dsm/pick2024/domain/weekendmeal/service/QueryIsWeekendMealPeriodService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,43 @@ | ||
package dsm.pick2024.domain.weekendmeal.service | ||
|
||
import dsm.pick2024.domain.weekendmeal.domain.WeekendMealPeriod | ||
import dsm.pick2024.domain.weekendmeal.port.`in`.QueryIsWeekendMealPeriodUseCase | ||
import dsm.pick2024.domain.weekendmeal.port.out.QueryWeekendMealPeriodPort | ||
import dsm.pick2024.domain.weekendmeal.presentation.dto.response.QueryIsPeriodStatusResponse | ||
import org.springframework.stereotype.Service | ||
import org.springframework.transaction.annotation.Transactional | ||
import java.time.LocalDate | ||
import java.time.LocalDate.now | ||
|
||
@Service | ||
class QueryIsWeekendMealPeriodService( | ||
private val queryWeekendMealPeriodPort: QueryWeekendMealPeriodPort | ||
) : QueryIsWeekendMealPeriodUseCase { | ||
|
||
@Transactional | ||
@Transactional(readOnly = true) | ||
override fun isWeekendMealPeriod(): QueryIsPeriodStatusResponse { | ||
val today = now() | ||
|
||
val periods = queryWeekendMealPeriodPort.queryAllWeekendMeal() | ||
|
||
val period = periods.find { | ||
today.isAfter(it.start.minusDays(1)) && today.isBefore(it.end.plusDays(1)) | ||
} | ||
val currentPeriod = findCurrentPeriod(today, periods) | ||
|
||
val status = period != null | ||
val start = period?.start?.toString() ?: "" | ||
val end = period?.end?.toString() ?: "" | ||
val nextPeriod = currentPeriod ?: findNextPeriod(today, periods) | ||
|
||
val status = currentPeriod != null | ||
val start = nextPeriod?.start | ||
val end = nextPeriod?.end | ||
|
||
return QueryIsPeriodStatusResponse(status, start, end) | ||
} | ||
|
||
private fun findCurrentPeriod(today: LocalDate, periods: List<WeekendMealPeriod>): WeekendMealPeriod? { | ||
return periods.find { | ||
today.isAfter(it.start.minusDays(1)) && today.isBefore(it.end.plusDays(1)) | ||
} | ||
} | ||
|
||
private fun findNextPeriod(today: LocalDate, periods: List<WeekendMealPeriod>): WeekendMealPeriod? { | ||
return periods.filter { it.start.isAfter(today) } | ||
.minByOrNull { it.start } | ||
} | ||
} |