diff --git a/src/main/kotlin/roomescape/schedule/business/ScheduleValidator.kt b/src/main/kotlin/roomescape/schedule/business/ScheduleValidator.kt index 4baf7790..e81739f4 100644 --- a/src/main/kotlin/roomescape/schedule/business/ScheduleValidator.kt +++ b/src/main/kotlin/roomescape/schedule/business/ScheduleValidator.kt @@ -33,16 +33,17 @@ class ScheduleValidator( val date: LocalDate = schedule.date val time: LocalTime = request.time ?: schedule.time - validateDateTime(date, time) + validateNotInPast(date, time) } - fun validateCanCreate(request: ScheduleCreateRequest) { + fun validateCanCreate(storeId: Long, request: ScheduleCreateRequest) { val date: LocalDate = request.date val time: LocalTime = request.time val themeId: Long = request.themeId validateAlreadyExists(date, themeId, time) - validateDateTime(date, time) + validateNotInPast(date, time) + validateTimeNotConflict(storeId, request.date, request.themeId, request.time) } private fun validateAlreadyExists(date: LocalDate, themeId: Long, time: LocalTime) { @@ -54,7 +55,7 @@ class ScheduleValidator( } } - private fun validateDateTime(date: LocalDate, time: LocalTime) { + private fun validateNotInPast(date: LocalDate, time: LocalTime) { val dateTime = LocalDateTime.of(date, time) if (dateTime.isBefore(LocalDateTime.now())) { @@ -64,4 +65,13 @@ class ScheduleValidator( throw ScheduleException(ScheduleErrorCode.PAST_DATE_TIME) } } + + private fun validateTimeNotConflict(storeId: Long, date: LocalDate, themeId: Long, time: LocalTime) { + scheduleRepository.findStoreSchedulesWithThemeByDate(storeId, date, themeId) + .firstOrNull { it.containsTime(time) } + ?.let { + log.info { "[ScheduleValidator.validateTimeNotConflict] 시간이 겹치는 일정 존재: conflictSchedule(Id=${it.id}, time=${it.time}~${it.getEndAt()})" } + throw ScheduleException(ScheduleErrorCode.SCHEDULE_TIME_CONFLICT) + } + } } diff --git a/src/main/kotlin/roomescape/schedule/exception/ScheduleErrorCode.kt b/src/main/kotlin/roomescape/schedule/exception/ScheduleErrorCode.kt index 096fce94..696d6630 100644 --- a/src/main/kotlin/roomescape/schedule/exception/ScheduleErrorCode.kt +++ b/src/main/kotlin/roomescape/schedule/exception/ScheduleErrorCode.kt @@ -12,5 +12,6 @@ enum class ScheduleErrorCode( SCHEDULE_ALREADY_EXISTS(HttpStatus.CONFLICT, "S002", "이미 동일한 일정이 있어요."), PAST_DATE_TIME(HttpStatus.BAD_REQUEST, "S003", "과거 날짜와 시간은 선택할 수 없어요."), SCHEDULE_IN_USE(HttpStatus.CONFLICT, "S004", "예약이 진행중이거나 완료된 일정은 삭제할 수 없어요."), - SCHEDULE_NOT_AVAILABLE(HttpStatus.CONFLICT, "S005", "예약이 완료되었거나 예약할 수 없는 일정이에요.") + SCHEDULE_NOT_AVAILABLE(HttpStatus.CONFLICT, "S005", "예약이 완료되었거나 예약할 수 없는 일정이에요."), + SCHEDULE_TIME_CONFLICT(HttpStatus.CONFLICT, "S006", "시간이 겹치는 다른 일정이 있어요.") }