Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
rudeh2926 committed Oct 30, 2024
2 parents 9b877f1 + d5822d4 commit a8906dc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
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?
)
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 }
}
}

0 comments on commit a8906dc

Please sign in to comment.