From 1fc1153968990d6905eff89d10deb04b434b87f3 Mon Sep 17 00:00:00 2001 From: wesleylcarraway Date: Mon, 3 Jan 2022 02:06:09 -0300 Subject: [PATCH] pet feature added --- pom.xml | 5 ++++ .../personapi/dto/request/PersonDTO.java | 6 ++++ .../personapi/dto/request/PetDTO.java | 27 ++++++++++++++++++ .../personapi/entities/Person.java | 13 ++++----- .../personapi/entities/Pet.java | 28 +++++++++++++++++++ .../personapi/enums/Species.java | 16 +++++++++++ 6 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 src/main/java/one/digitalinnovation/personapi/dto/request/PetDTO.java create mode 100644 src/main/java/one/digitalinnovation/personapi/entities/Pet.java create mode 100644 src/main/java/one/digitalinnovation/personapi/enums/Species.java diff --git a/pom.xml b/pom.xml index 8de3f9e..cd6edf6 100644 --- a/pom.xml +++ b/pom.xml @@ -63,6 +63,11 @@ + + org.hibernate + hibernate-validator + 7.0.2.Final + diff --git a/src/main/java/one/digitalinnovation/personapi/dto/request/PersonDTO.java b/src/main/java/one/digitalinnovation/personapi/dto/request/PersonDTO.java index 9ba2a7f..585b98c 100644 --- a/src/main/java/one/digitalinnovation/personapi/dto/request/PersonDTO.java +++ b/src/main/java/one/digitalinnovation/personapi/dto/request/PersonDTO.java @@ -38,4 +38,10 @@ public class PersonDTO { @Valid @NotEmpty private List phones; + + @Valid + @NotEmpty + private List pets; + + } diff --git a/src/main/java/one/digitalinnovation/personapi/dto/request/PetDTO.java b/src/main/java/one/digitalinnovation/personapi/dto/request/PetDTO.java new file mode 100644 index 0000000..3e3e44b --- /dev/null +++ b/src/main/java/one/digitalinnovation/personapi/dto/request/PetDTO.java @@ -0,0 +1,27 @@ +package one.digitalinnovation.personapi.dto.request; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import one.digitalinnovation.personapi.enums.Species; + +import javax.persistence.EnumType; +import javax.persistence.Enumerated; +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.Size; + +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class PetDTO { + private Long id; + + @Enumerated(EnumType.STRING) + private Species species; + + @NotEmpty + @Size(min = 13, max = 14) + private String name; +} diff --git a/src/main/java/one/digitalinnovation/personapi/entities/Person.java b/src/main/java/one/digitalinnovation/personapi/entities/Person.java index 3db1d29..35772b4 100644 --- a/src/main/java/one/digitalinnovation/personapi/entities/Person.java +++ b/src/main/java/one/digitalinnovation/personapi/entities/Person.java @@ -5,14 +5,7 @@ import lombok.Data; import lombok.NoArgsConstructor; -import javax.persistence.CascadeType; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.FetchType; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.OneToMany; +import javax.persistence.*; import java.time.LocalDate; import java.util.ArrayList; import java.util.List; @@ -41,4 +34,8 @@ public class Person { @OneToMany(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE}) private List phones = new ArrayList<>(); + + @OneToMany(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE}) + private List pets = new ArrayList<>(); + } diff --git a/src/main/java/one/digitalinnovation/personapi/entities/Pet.java b/src/main/java/one/digitalinnovation/personapi/entities/Pet.java new file mode 100644 index 0000000..bd31def --- /dev/null +++ b/src/main/java/one/digitalinnovation/personapi/entities/Pet.java @@ -0,0 +1,28 @@ +package one.digitalinnovation.personapi.entities; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import one.digitalinnovation.personapi.enums.Species; + +import javax.persistence.*; + +@Entity +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class Pet { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Enumerated(EnumType.STRING) + @Column(nullable = false) + private Species species; + + @Column(nullable = false) + private String name; +} diff --git a/src/main/java/one/digitalinnovation/personapi/enums/Species.java b/src/main/java/one/digitalinnovation/personapi/enums/Species.java new file mode 100644 index 0000000..f775fe9 --- /dev/null +++ b/src/main/java/one/digitalinnovation/personapi/enums/Species.java @@ -0,0 +1,16 @@ +package one.digitalinnovation.personapi.enums; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public enum Species { + + DOG("Dog"), + CAT("Cat"), + HAMSTER("Hamster"), + REPTILE("Reptile"); + + private final String description; +} \ No newline at end of file