From cca4d45e002f8231b43a27ac1f7c3a8e3085d9d9 Mon Sep 17 00:00:00 2001 From: PEDSF Date: Sat, 17 Oct 2020 17:11:39 +0200 Subject: [PATCH] Fix spetialty empty bug --- .../petclinic/controller/OwnerController.java | 1 - .../samples/petclinic/dto/OwnerDTO.java | 3 ++- .../samples/petclinic/dto/VetDTO.java | 12 +++++----- .../samples/petclinic/model/Owner.java | 2 +- .../petclinic/service/OwnerService.java | 10 ++++++++- .../samples/petclinic/service/VetService.java | 22 +++++++++++++++---- .../controller/VetControllerTests.java | 3 ++- .../petclinic/service/VetServiceTest.java | 5 ++++- 8 files changed, 42 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/springframework/samples/petclinic/controller/OwnerController.java b/src/main/java/org/springframework/samples/petclinic/controller/OwnerController.java index 6cfcec4ef..dc9535e47 100644 --- a/src/main/java/org/springframework/samples/petclinic/controller/OwnerController.java +++ b/src/main/java/org/springframework/samples/petclinic/controller/OwnerController.java @@ -44,7 +44,6 @@ import java.util.Map; class OwnerController { private final OwnerService ownerService; - private final VisitService visitService; OwnerController(OwnerService ownerService, VisitService visitService) { diff --git a/src/main/java/org/springframework/samples/petclinic/dto/OwnerDTO.java b/src/main/java/org/springframework/samples/petclinic/dto/OwnerDTO.java index 83d2f8bac..0a05b6518 100644 --- a/src/main/java/org/springframework/samples/petclinic/dto/OwnerDTO.java +++ b/src/main/java/org/springframework/samples/petclinic/dto/OwnerDTO.java @@ -85,7 +85,8 @@ public class OwnerDTO extends PersonDTO { } public void addPet(PetDTO pet) { - if (pet.isNew()) { + + if(!this.getPets().contains(pet)) { getPetsInternal().add(pet); } pet.setOwner(this); diff --git a/src/main/java/org/springframework/samples/petclinic/dto/VetDTO.java b/src/main/java/org/springframework/samples/petclinic/dto/VetDTO.java index e41f87859..0b1520b56 100644 --- a/src/main/java/org/springframework/samples/petclinic/dto/VetDTO.java +++ b/src/main/java/org/springframework/samples/petclinic/dto/VetDTO.java @@ -29,22 +29,22 @@ import java.util.*; */ public class VetDTO extends PersonDTO { - private Set specialties; + private Set specialties; - protected Set getSpecialtiesInternal() { + protected Set getSpecialtiesInternal() { if (this.specialties == null) { this.specialties = new HashSet<>(); } return this.specialties; } - protected void setSpecialtiesInternal(Set specialties) { + protected void setSpecialtiesInternal(Set specialties) { this.specialties = specialties; } @XmlElement - public List getSpecialties() { - List sortedSpecs = new ArrayList<>(getSpecialtiesInternal()); + public List getSpecialties() { + List sortedSpecs = new ArrayList<>(getSpecialtiesInternal()); PropertyComparator.sort(sortedSpecs, new MutableSortDefinition("name", true, true)); return Collections.unmodifiableList(sortedSpecs); } @@ -53,7 +53,7 @@ public class VetDTO extends PersonDTO { return getSpecialtiesInternal().size(); } - public void addSpecialty(Specialty specialty) { + public void addSpecialty(SpecialtyDTO specialty) { getSpecialtiesInternal().add(specialty); } diff --git a/src/main/java/org/springframework/samples/petclinic/model/Owner.java b/src/main/java/org/springframework/samples/petclinic/model/Owner.java index e5a4be5d5..bc5f2df92 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/Owner.java +++ b/src/main/java/org/springframework/samples/petclinic/model/Owner.java @@ -104,7 +104,7 @@ public class Owner extends Person { } public void addPet(Pet pet) { - if (pet.isNew()) { + if (!this.getPets().contains(pet)) { getPetsInternal().add(pet); } pet.setOwner(this); diff --git a/src/main/java/org/springframework/samples/petclinic/service/OwnerService.java b/src/main/java/org/springframework/samples/petclinic/service/OwnerService.java index 64b375f40..fcb239be7 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/OwnerService.java +++ b/src/main/java/org/springframework/samples/petclinic/service/OwnerService.java @@ -1,9 +1,12 @@ package org.springframework.samples.petclinic.service; +import net.bytebuddy.matcher.FilterableList; import org.modelmapper.ModelMapper; import org.modelmapper.internal.util.Lists; import org.springframework.samples.petclinic.dto.OwnerDTO; +import org.springframework.samples.petclinic.dto.PetDTO; import org.springframework.samples.petclinic.model.Owner; +import org.springframework.samples.petclinic.model.Pet; import org.springframework.samples.petclinic.repository.OwnerRepository; import org.springframework.samples.petclinic.repository.PetRepository; import org.springframework.samples.petclinic.repository.PetTypeRepository; @@ -52,7 +55,12 @@ public class OwnerService implements BaseService { public OwnerDTO entityToDTO(Owner entity) { if (entity != null) { OwnerDTO ownerDTO = modelMapper.map(entity, OwnerDTO.class); - entity.getPets().forEach(pet -> ownerDTO.addPet(petService.entityToDTO(pet))); + + for( Pet pet : entity.getPets()) { + PetDTO petDTO = petService.entityToDTO(pet); + ownerDTO.addPet(petDTO); + } + return ownerDTO; } diff --git a/src/main/java/org/springframework/samples/petclinic/service/VetService.java b/src/main/java/org/springframework/samples/petclinic/service/VetService.java index 5f4775bd3..1e0071d93 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/VetService.java +++ b/src/main/java/org/springframework/samples/petclinic/service/VetService.java @@ -2,7 +2,10 @@ package org.springframework.samples.petclinic.service; import org.modelmapper.ModelMapper; import org.modelmapper.internal.util.Lists; +import org.springframework.samples.petclinic.dto.SpecialtyDTO; import org.springframework.samples.petclinic.dto.VetDTO; +import org.springframework.samples.petclinic.model.Specialty; +import org.springframework.samples.petclinic.repository.SpecialtyRepository; import org.springframework.samples.petclinic.repository.VetRepository; import org.springframework.samples.petclinic.model.Vet; import org.springframework.stereotype.Service; @@ -21,17 +24,23 @@ import java.util.List; public class VetService implements BaseService { private final VetRepository vetRepository; - + private final SpecialtyService specialtyService; private final ModelMapper modelMapper = new ModelMapper(); - public VetService(VetRepository vetRepository) { + public VetService(VetRepository vetRepository, SpecialtyRepository specialtyRepository) { this.vetRepository = vetRepository; + this.specialtyService = new SpecialtyService(specialtyRepository); } @Override public Vet dtoToEntity(VetDTO dto) { if (dto != null) { - return modelMapper.map(dto, Vet.class); + Vet vet = modelMapper.map(dto, Vet.class); + dto.getSpecialties().forEach(specialtyDTO -> { + Specialty specialty = specialtyService.dtoToEntity(specialtyDTO); + vet.addSpecialty(specialty); + }); + return vet; } return new Vet(); @@ -40,7 +49,12 @@ public class VetService implements BaseService { @Override public VetDTO entityToDTO(Vet entity) { if (entity != null) { - return modelMapper.map(entity, VetDTO.class); + VetDTO vetDTO = modelMapper.map(entity, VetDTO.class); + entity.getSpecialties().forEach(specialty -> { + SpecialtyDTO specialtyDTO = specialtyService.entityToDTO(specialty); + vetDTO.addSpecialty(specialtyDTO); + }); + return vetDTO; } return new VetDTO(); } diff --git a/src/test/java/org/springframework/samples/petclinic/controller/VetControllerTests.java b/src/test/java/org/springframework/samples/petclinic/controller/VetControllerTests.java index 003daa26e..c3eef01b1 100644 --- a/src/test/java/org/springframework/samples/petclinic/controller/VetControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/controller/VetControllerTests.java @@ -35,6 +35,7 @@ import org.springframework.http.MediaType; import org.springframework.samples.petclinic.common.CommonAttribute; import org.springframework.samples.petclinic.common.CommonEndPoint; import org.springframework.samples.petclinic.common.CommonView; +import org.springframework.samples.petclinic.dto.SpecialtyDTO; import org.springframework.samples.petclinic.dto.VetDTO; import org.springframework.samples.petclinic.service.VetService; import org.springframework.samples.petclinic.model.Specialty; @@ -65,7 +66,7 @@ class VetControllerTests { helen.setFirstName("Helen"); helen.setLastName("Leary"); helen.setId(2); - Specialty radiology = new Specialty(); + SpecialtyDTO radiology = new SpecialtyDTO(); radiology.setId(1); radiology.setName("radiology"); helen.addSpecialty(radiology); diff --git a/src/test/java/org/springframework/samples/petclinic/service/VetServiceTest.java b/src/test/java/org/springframework/samples/petclinic/service/VetServiceTest.java index 289a8c501..3f11bf551 100644 --- a/src/test/java/org/springframework/samples/petclinic/service/VetServiceTest.java +++ b/src/test/java/org/springframework/samples/petclinic/service/VetServiceTest.java @@ -10,6 +10,7 @@ import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.context.annotation.ComponentScan; import org.springframework.samples.petclinic.dto.VetDTO; import org.springframework.samples.petclinic.model.Vet; +import org.springframework.samples.petclinic.repository.SpecialtyRepository; import org.springframework.samples.petclinic.repository.VetRepository; import org.springframework.stereotype.Service; @@ -31,6 +32,8 @@ class VetServiceTest { @Autowired private VetRepository vetRepository; + @Autowired + private SpecialtyRepository specialtyRepository; private VetService vetService; @@ -40,7 +43,7 @@ class VetServiceTest { @BeforeEach void beforeEach() { - vetService = new VetService(vetRepository); + vetService = new VetService(vetRepository, specialtyRepository); vet = new Vet(); vet.setId(VET_ID); vet.setFirstName(VET_FIRST_NAME);