diff --git a/src/main/java/leets/weeth/domain/schedule/application/annotation/ScheduleTimeCheck.java b/src/main/java/leets/weeth/domain/schedule/application/annotation/ScheduleTimeCheck.java new file mode 100644 index 00000000..c5f12a1a --- /dev/null +++ b/src/main/java/leets/weeth/domain/schedule/application/annotation/ScheduleTimeCheck.java @@ -0,0 +1,28 @@ +package leets.weeth.domain.schedule.application.annotation; + +import jakarta.validation.Constraint; +import jakarta.validation.Payload; +import leets.weeth.domain.schedule.application.validator.ScheduleTimeCheckValidator; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +@Target({FIELD}) +@Retention(RUNTIME) +@Constraint(validatedBy = ScheduleTimeCheckValidator.class) +public @interface ScheduleTimeCheck { + + String message() default "마감 시간이 시작 시간보다 빠를 수 없습니다."; + + Class[] groups() default {}; + + Class[] payload() default {}; + + String matchStartTime() default "matchStartTime"; + + String matchEndTime() default "matchEndTime"; + +} diff --git a/src/main/java/leets/weeth/domain/schedule/application/dto/EventDTO.java b/src/main/java/leets/weeth/domain/schedule/application/dto/EventDTO.java index f1ad4a53..c528fcfa 100644 --- a/src/main/java/leets/weeth/domain/schedule/application/dto/EventDTO.java +++ b/src/main/java/leets/weeth/domain/schedule/application/dto/EventDTO.java @@ -2,7 +2,7 @@ import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; -import org.springframework.format.annotation.DateTimeFormat; +import leets.weeth.domain.schedule.application.annotation.ScheduleTimeCheck; import java.time.LocalDateTime; @@ -28,8 +28,7 @@ public record Save( @NotBlank String location, @NotBlank String requiredItem, @NotNull String memberCount, - @NotNull @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime start, - @NotNull @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime end + @ScheduleTimeCheck ScheduleDTO.Time time ) {} public record Update( @@ -38,7 +37,6 @@ public record Update( @NotBlank String location, @NotBlank String requiredItem, @NotNull String memberCount, - @NotNull @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime start, - @NotNull @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime end + @ScheduleTimeCheck ScheduleDTO.Time time ) {} } diff --git a/src/main/java/leets/weeth/domain/schedule/application/dto/MeetingDTO.java b/src/main/java/leets/weeth/domain/schedule/application/dto/MeetingDTO.java index 739e6db8..a9b7d212 100644 --- a/src/main/java/leets/weeth/domain/schedule/application/dto/MeetingDTO.java +++ b/src/main/java/leets/weeth/domain/schedule/application/dto/MeetingDTO.java @@ -2,10 +2,12 @@ import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; -import org.springframework.format.annotation.DateTimeFormat; +import leets.weeth.domain.schedule.application.annotation.ScheduleTimeCheck; import java.time.LocalDateTime; +import static leets.weeth.domain.schedule.application.dto.ScheduleDTO.Time; + public class MeetingDTO { public record Response( @@ -28,8 +30,7 @@ public record Save( @NotBlank String title, @NotBlank String content, @NotBlank String location, - @NotNull @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime start, - @NotNull @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime end, + @ScheduleTimeCheck Time time, @NotNull Integer weekNumber, @NotNull Integer cardinal ) {} @@ -38,8 +39,7 @@ public record Update( @NotBlank String title, @NotBlank String content, @NotBlank String location, - @NotNull @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime start, - @NotNull @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime end, + @ScheduleTimeCheck Time time, @NotNull Integer weekNumber, @NotNull Integer cardinal ) {} diff --git a/src/main/java/leets/weeth/domain/schedule/application/dto/ScheduleDTO.java b/src/main/java/leets/weeth/domain/schedule/application/dto/ScheduleDTO.java index 4d10a284..53932620 100644 --- a/src/main/java/leets/weeth/domain/schedule/application/dto/ScheduleDTO.java +++ b/src/main/java/leets/weeth/domain/schedule/application/dto/ScheduleDTO.java @@ -1,5 +1,8 @@ package leets.weeth.domain.schedule.application.dto; +import jakarta.validation.constraints.NotNull; +import org.springframework.format.annotation.DateTimeFormat; + import java.time.LocalDateTime; public class ScheduleDTO { @@ -11,4 +14,9 @@ public record Response( LocalDateTime end, Boolean isMeeting ) {} + + public record Time( + @NotNull @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime start, + @NotNull @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime end + ) {} } diff --git a/src/main/java/leets/weeth/domain/schedule/application/validator/ScheduleTimeCheckValidator.java b/src/main/java/leets/weeth/domain/schedule/application/validator/ScheduleTimeCheckValidator.java new file mode 100644 index 00000000..2d8357ef --- /dev/null +++ b/src/main/java/leets/weeth/domain/schedule/application/validator/ScheduleTimeCheckValidator.java @@ -0,0 +1,19 @@ +package leets.weeth.domain.schedule.application.validator; + +import jakarta.validation.ConstraintValidator; +import jakarta.validation.ConstraintValidatorContext; +import leets.weeth.domain.schedule.application.annotation.ScheduleTimeCheck; +import leets.weeth.domain.schedule.application.dto.ScheduleDTO.Time; + +public class ScheduleTimeCheckValidator implements ConstraintValidator { + + @Override + public void initialize(ScheduleTimeCheck constraintAnnotation) { + ConstraintValidator.super.initialize(constraintAnnotation); + } + + @Override + public boolean isValid(Time time, ConstraintValidatorContext context) { + return time.start().isBefore(time.end().plusMinutes(1)); + } +} \ No newline at end of file diff --git a/src/main/java/leets/weeth/domain/schedule/domain/entity/Schedule.java b/src/main/java/leets/weeth/domain/schedule/domain/entity/Schedule.java index e9c85c22..ffa0af3e 100644 --- a/src/main/java/leets/weeth/domain/schedule/domain/entity/Schedule.java +++ b/src/main/java/leets/weeth/domain/schedule/domain/entity/Schedule.java @@ -44,8 +44,8 @@ public void updateUpperClass(EventDTO.Update dto, User user) { this.title = dto.title(); this.content = dto.content(); this.location = dto.location(); - this.start = dto.start(); - this.end = dto.end(); + this.start = dto.time().start(); + this.end = dto.time().end(); this.user = user; } @@ -53,8 +53,8 @@ public void updateUpperClass(MeetingDTO.Update dto, User user) { this.title = dto.title(); this.content = dto.content(); this.location = dto.location(); - this.start = dto.start(); - this.end = dto.end(); + this.start = dto.time().start(); + this.end = dto.time().end(); this.user = user; } }