Skip to content

Commit

Permalink
Merge pull request #41 from Leets-Official/#40/feat/시간-validator-구현
Browse files Browse the repository at this point in the history
#41 Feat: 시간 Validator 구현
  • Loading branch information
KoungQ authored Aug 7, 2024
2 parents fcd11e8 + 73667f4 commit e3819e5
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -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<? extends Payload>[] payload() default {};

String matchStartTime() default "matchStartTime";

String matchEndTime() default "matchEndTime";

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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(
Expand All @@ -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
) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
) {}
Expand All @@ -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
) {}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
) {}
}
Original file line number Diff line number Diff line change
@@ -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<ScheduleTimeCheck, Time> {

@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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ 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;
}

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;
}
}

0 comments on commit e3819e5

Please sign in to comment.