From 4546ad624353058b3897d3103c894a15384d07f7 Mon Sep 17 00:00:00 2001 From: koungq Date: Wed, 7 Aug 2024 14:36:00 +0900 Subject: [PATCH 1/2] =?UTF-8?q?Hotfix:=20=EC=8B=9C=EA=B0=84=20Validator=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../annotation/ScheduleTimeCheck.java | 28 +++++++++++++++++++ .../schedule/application/dto/EventDTO.java | 7 ++--- .../schedule/application/dto/MeetingDTO.java | 9 +++--- .../schedule/application/dto/ScheduleDTO.java | 8 ++++++ .../validator/ScheduleTimeCheckValidator.java | 19 +++++++++++++ .../schedule/domain/entity/Schedule.java | 8 +++--- 6 files changed, 67 insertions(+), 12 deletions(-) create mode 100644 src/main/java/leets/weeth/domain/schedule/application/annotation/ScheduleTimeCheck.java create mode 100644 src/main/java/leets/weeth/domain/schedule/application/validator/ScheduleTimeCheckValidator.java 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..79d7c624 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,6 +2,7 @@ import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; +import leets.weeth.domain.schedule.application.annotation.ScheduleTimeCheck; import org.springframework.format.annotation.DateTimeFormat; import java.time.LocalDateTime; @@ -28,8 +29,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 +38,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..b0556932 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,13 @@ import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; +import leets.weeth.domain.schedule.application.annotation.ScheduleTimeCheck; import org.springframework.format.annotation.DateTimeFormat; import java.time.LocalDateTime; +import static leets.weeth.domain.schedule.application.dto.ScheduleDTO.*; + public class MeetingDTO { public record Response( @@ -28,8 +31,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 +40,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; } } From 73667f4d0abc22a3685f3536a1eb669b19bc3637 Mon Sep 17 00:00:00 2001 From: koungq Date: Wed, 7 Aug 2024 14:36:16 +0900 Subject: [PATCH 2/2] =?UTF-8?q?Hotfix:=20=EC=8B=9C=EA=B0=84=20Validator=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leets/weeth/domain/schedule/application/dto/EventDTO.java | 1 - .../weeth/domain/schedule/application/dto/MeetingDTO.java | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) 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 79d7c624..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 @@ -3,7 +3,6 @@ import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; import leets.weeth.domain.schedule.application.annotation.ScheduleTimeCheck; -import org.springframework.format.annotation.DateTimeFormat; import java.time.LocalDateTime; 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 b0556932..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 @@ -3,11 +3,10 @@ import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; import leets.weeth.domain.schedule.application.annotation.ScheduleTimeCheck; -import org.springframework.format.annotation.DateTimeFormat; import java.time.LocalDateTime; -import static leets.weeth.domain.schedule.application.dto.ScheduleDTO.*; +import static leets.weeth.domain.schedule.application.dto.ScheduleDTO.Time; public class MeetingDTO {