mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-22 07:15:49 +00:00
Fix spetialty empty bug
This commit is contained in:
parent
f3e9c39681
commit
cca4d45e00
8 changed files with 42 additions and 16 deletions
|
@ -44,7 +44,6 @@ import java.util.Map;
|
|||
class OwnerController {
|
||||
|
||||
private final OwnerService ownerService;
|
||||
|
||||
private final VisitService visitService;
|
||||
|
||||
OwnerController(OwnerService ownerService, VisitService visitService) {
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -29,22 +29,22 @@ import java.util.*;
|
|||
*/
|
||||
public class VetDTO extends PersonDTO {
|
||||
|
||||
private Set<Specialty> specialties;
|
||||
private Set<SpecialtyDTO> specialties;
|
||||
|
||||
protected Set<Specialty> getSpecialtiesInternal() {
|
||||
protected Set<SpecialtyDTO> getSpecialtiesInternal() {
|
||||
if (this.specialties == null) {
|
||||
this.specialties = new HashSet<>();
|
||||
}
|
||||
return this.specialties;
|
||||
}
|
||||
|
||||
protected void setSpecialtiesInternal(Set<Specialty> specialties) {
|
||||
protected void setSpecialtiesInternal(Set<SpecialtyDTO> specialties) {
|
||||
this.specialties = specialties;
|
||||
}
|
||||
|
||||
@XmlElement
|
||||
public List<Specialty> getSpecialties() {
|
||||
List<Specialty> sortedSpecs = new ArrayList<>(getSpecialtiesInternal());
|
||||
public List<SpecialtyDTO> getSpecialties() {
|
||||
List<SpecialtyDTO> 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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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<Owner, OwnerDTO> {
|
|||
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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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<Vet, VetDTO> {
|
||||
|
||||
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<Vet, VetDTO> {
|
|||
@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();
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue