From 22065acfef9dcc4983ab73fb9f15cab2607f1a4e Mon Sep 17 00:00:00 2001 From: JackKaif Date: Mon, 19 Feb 2024 11:55:28 +0300 Subject: [PATCH] Complete entity validation task --- .../exercise/controller/GuestsController.java | 8 ++++- .../java/exercise/dto/GuestCreateDTO.java | 32 +++++++++++++++++++ .../src/main/java/exercise/model/Guest.java | 23 ++++++++----- 3 files changed, 54 insertions(+), 9 deletions(-) diff --git a/java-spring-ru/entity-validation/src/main/java/exercise/controller/GuestsController.java b/java-spring-ru/entity-validation/src/main/java/exercise/controller/GuestsController.java index a487bba..67cd9c3 100644 --- a/java-spring-ru/entity-validation/src/main/java/exercise/controller/GuestsController.java +++ b/java-spring-ru/entity-validation/src/main/java/exercise/controller/GuestsController.java @@ -46,6 +46,12 @@ public GuestDTO show(@PathVariable long id) { } // BEGIN - + @PostMapping("") + @ResponseStatus(HttpStatus.CREATED) + public GuestDTO create(@Valid @RequestBody GuestCreateDTO newGuest) { + var guest = guestMapper.map(newGuest); + guestRepository.save(guest); + return guestMapper.map(guest); + } // END } diff --git a/java-spring-ru/entity-validation/src/main/java/exercise/dto/GuestCreateDTO.java b/java-spring-ru/entity-validation/src/main/java/exercise/dto/GuestCreateDTO.java index 2cb9e09..b0027dd 100644 --- a/java-spring-ru/entity-validation/src/main/java/exercise/dto/GuestCreateDTO.java +++ b/java-spring-ru/entity-validation/src/main/java/exercise/dto/GuestCreateDTO.java @@ -2,4 +2,36 @@ // BEGIN +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.validation.constraints.Email; +import jakarta.validation.constraints.FutureOrPresent; +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Pattern; +import lombok.Getter; +import lombok.Setter; +import org.springframework.data.annotation.CreatedDate; + +import java.time.LocalDate; + +import static jakarta.persistence.GenerationType.IDENTITY; + +@Getter +@Setter +public class GuestCreateDTO { + @NotNull + private String name; + + @Email + private String email; + + @Pattern(regexp = "\\+\\d{11,13}") + private String phoneNumber; + + @Pattern(regexp = "\\d{4}") + private String clubCard; + + @FutureOrPresent + private LocalDate cardValidUntil; +} // END diff --git a/java-spring-ru/entity-validation/src/main/java/exercise/model/Guest.java b/java-spring-ru/entity-validation/src/main/java/exercise/model/Guest.java index 912942a..5c82d2c 100644 --- a/java-spring-ru/entity-validation/src/main/java/exercise/model/Guest.java +++ b/java-spring-ru/entity-validation/src/main/java/exercise/model/Guest.java @@ -8,10 +8,7 @@ import jakarta.persistence.GeneratedValue; import static jakarta.persistence.GenerationType.IDENTITY; -import jakarta.validation.constraints.Email; -import jakarta.validation.constraints.Future; -import jakarta.validation.constraints.NotBlank; -import jakarta.validation.constraints.Pattern; +import jakarta.validation.constraints.*; import org.springframework.data.annotation.CreatedDate; import org.springframework.data.jpa.domain.support.AuditingEntityListener; @@ -19,9 +16,6 @@ import lombok.Setter; import java.time.LocalDate; - - - @Entity @Table(name = "guests") @EntityListeners(AuditingEntityListener.class) @@ -34,7 +28,20 @@ public class Guest { private long id; // BEGIN - + @NotNull + private String name; + + @Email + private String email; + + @Pattern(regexp = "^\\+\\d{11,13}$") + private String phoneNumber; + + @Pattern(regexp = "^\\d{4}$") + private String clubCard; + + @FutureOrPresent + private LocalDate cardValidUntil; // END @CreatedDate