mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-22 15:25: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 {
|
class OwnerController {
|
||||||
|
|
||||||
private final OwnerService ownerService;
|
private final OwnerService ownerService;
|
||||||
|
|
||||||
private final VisitService visitService;
|
private final VisitService visitService;
|
||||||
|
|
||||||
OwnerController(OwnerService ownerService, VisitService visitService) {
|
OwnerController(OwnerService ownerService, VisitService visitService) {
|
||||||
|
|
|
@ -85,7 +85,8 @@ public class OwnerDTO extends PersonDTO {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addPet(PetDTO pet) {
|
public void addPet(PetDTO pet) {
|
||||||
if (pet.isNew()) {
|
|
||||||
|
if(!this.getPets().contains(pet)) {
|
||||||
getPetsInternal().add(pet);
|
getPetsInternal().add(pet);
|
||||||
}
|
}
|
||||||
pet.setOwner(this);
|
pet.setOwner(this);
|
||||||
|
|
|
@ -29,22 +29,22 @@ import java.util.*;
|
||||||
*/
|
*/
|
||||||
public class VetDTO extends PersonDTO {
|
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) {
|
if (this.specialties == null) {
|
||||||
this.specialties = new HashSet<>();
|
this.specialties = new HashSet<>();
|
||||||
}
|
}
|
||||||
return this.specialties;
|
return this.specialties;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void setSpecialtiesInternal(Set<Specialty> specialties) {
|
protected void setSpecialtiesInternal(Set<SpecialtyDTO> specialties) {
|
||||||
this.specialties = specialties;
|
this.specialties = specialties;
|
||||||
}
|
}
|
||||||
|
|
||||||
@XmlElement
|
@XmlElement
|
||||||
public List<Specialty> getSpecialties() {
|
public List<SpecialtyDTO> getSpecialties() {
|
||||||
List<Specialty> sortedSpecs = new ArrayList<>(getSpecialtiesInternal());
|
List<SpecialtyDTO> sortedSpecs = new ArrayList<>(getSpecialtiesInternal());
|
||||||
PropertyComparator.sort(sortedSpecs, new MutableSortDefinition("name", true, true));
|
PropertyComparator.sort(sortedSpecs, new MutableSortDefinition("name", true, true));
|
||||||
return Collections.unmodifiableList(sortedSpecs);
|
return Collections.unmodifiableList(sortedSpecs);
|
||||||
}
|
}
|
||||||
|
@ -53,7 +53,7 @@ public class VetDTO extends PersonDTO {
|
||||||
return getSpecialtiesInternal().size();
|
return getSpecialtiesInternal().size();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addSpecialty(Specialty specialty) {
|
public void addSpecialty(SpecialtyDTO specialty) {
|
||||||
getSpecialtiesInternal().add(specialty);
|
getSpecialtiesInternal().add(specialty);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -104,7 +104,7 @@ public class Owner extends Person {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addPet(Pet pet) {
|
public void addPet(Pet pet) {
|
||||||
if (pet.isNew()) {
|
if (!this.getPets().contains(pet)) {
|
||||||
getPetsInternal().add(pet);
|
getPetsInternal().add(pet);
|
||||||
}
|
}
|
||||||
pet.setOwner(this);
|
pet.setOwner(this);
|
||||||
|
|
|
@ -1,9 +1,12 @@
|
||||||
package org.springframework.samples.petclinic.service;
|
package org.springframework.samples.petclinic.service;
|
||||||
|
|
||||||
|
import net.bytebuddy.matcher.FilterableList;
|
||||||
import org.modelmapper.ModelMapper;
|
import org.modelmapper.ModelMapper;
|
||||||
import org.modelmapper.internal.util.Lists;
|
import org.modelmapper.internal.util.Lists;
|
||||||
import org.springframework.samples.petclinic.dto.OwnerDTO;
|
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.Owner;
|
||||||
|
import org.springframework.samples.petclinic.model.Pet;
|
||||||
import org.springframework.samples.petclinic.repository.OwnerRepository;
|
import org.springframework.samples.petclinic.repository.OwnerRepository;
|
||||||
import org.springframework.samples.petclinic.repository.PetRepository;
|
import org.springframework.samples.petclinic.repository.PetRepository;
|
||||||
import org.springframework.samples.petclinic.repository.PetTypeRepository;
|
import org.springframework.samples.petclinic.repository.PetTypeRepository;
|
||||||
|
@ -52,7 +55,12 @@ public class OwnerService implements BaseService<Owner, OwnerDTO> {
|
||||||
public OwnerDTO entityToDTO(Owner entity) {
|
public OwnerDTO entityToDTO(Owner entity) {
|
||||||
if (entity != null) {
|
if (entity != null) {
|
||||||
OwnerDTO ownerDTO = modelMapper.map(entity, OwnerDTO.class);
|
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;
|
return ownerDTO;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,10 @@ package org.springframework.samples.petclinic.service;
|
||||||
|
|
||||||
import org.modelmapper.ModelMapper;
|
import org.modelmapper.ModelMapper;
|
||||||
import org.modelmapper.internal.util.Lists;
|
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.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.repository.VetRepository;
|
||||||
import org.springframework.samples.petclinic.model.Vet;
|
import org.springframework.samples.petclinic.model.Vet;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
@ -21,17 +24,23 @@ import java.util.List;
|
||||||
public class VetService implements BaseService<Vet, VetDTO> {
|
public class VetService implements BaseService<Vet, VetDTO> {
|
||||||
|
|
||||||
private final VetRepository vetRepository;
|
private final VetRepository vetRepository;
|
||||||
|
private final SpecialtyService specialtyService;
|
||||||
private final ModelMapper modelMapper = new ModelMapper();
|
private final ModelMapper modelMapper = new ModelMapper();
|
||||||
|
|
||||||
public VetService(VetRepository vetRepository) {
|
public VetService(VetRepository vetRepository, SpecialtyRepository specialtyRepository) {
|
||||||
this.vetRepository = vetRepository;
|
this.vetRepository = vetRepository;
|
||||||
|
this.specialtyService = new SpecialtyService(specialtyRepository);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Vet dtoToEntity(VetDTO dto) {
|
public Vet dtoToEntity(VetDTO dto) {
|
||||||
if (dto != null) {
|
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();
|
return new Vet();
|
||||||
|
@ -40,7 +49,12 @@ public class VetService implements BaseService<Vet, VetDTO> {
|
||||||
@Override
|
@Override
|
||||||
public VetDTO entityToDTO(Vet entity) {
|
public VetDTO entityToDTO(Vet entity) {
|
||||||
if (entity != null) {
|
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();
|
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.CommonAttribute;
|
||||||
import org.springframework.samples.petclinic.common.CommonEndPoint;
|
import org.springframework.samples.petclinic.common.CommonEndPoint;
|
||||||
import org.springframework.samples.petclinic.common.CommonView;
|
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.dto.VetDTO;
|
||||||
import org.springframework.samples.petclinic.service.VetService;
|
import org.springframework.samples.petclinic.service.VetService;
|
||||||
import org.springframework.samples.petclinic.model.Specialty;
|
import org.springframework.samples.petclinic.model.Specialty;
|
||||||
|
@ -65,7 +66,7 @@ class VetControllerTests {
|
||||||
helen.setFirstName("Helen");
|
helen.setFirstName("Helen");
|
||||||
helen.setLastName("Leary");
|
helen.setLastName("Leary");
|
||||||
helen.setId(2);
|
helen.setId(2);
|
||||||
Specialty radiology = new Specialty();
|
SpecialtyDTO radiology = new SpecialtyDTO();
|
||||||
radiology.setId(1);
|
radiology.setId(1);
|
||||||
radiology.setName("radiology");
|
radiology.setName("radiology");
|
||||||
helen.addSpecialty(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.context.annotation.ComponentScan;
|
||||||
import org.springframework.samples.petclinic.dto.VetDTO;
|
import org.springframework.samples.petclinic.dto.VetDTO;
|
||||||
import org.springframework.samples.petclinic.model.Vet;
|
import org.springframework.samples.petclinic.model.Vet;
|
||||||
|
import org.springframework.samples.petclinic.repository.SpecialtyRepository;
|
||||||
import org.springframework.samples.petclinic.repository.VetRepository;
|
import org.springframework.samples.petclinic.repository.VetRepository;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@ -31,6 +32,8 @@ class VetServiceTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private VetRepository vetRepository;
|
private VetRepository vetRepository;
|
||||||
|
@Autowired
|
||||||
|
private SpecialtyRepository specialtyRepository;
|
||||||
|
|
||||||
private VetService vetService;
|
private VetService vetService;
|
||||||
|
|
||||||
|
@ -40,7 +43,7 @@ class VetServiceTest {
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void beforeEach() {
|
void beforeEach() {
|
||||||
vetService = new VetService(vetRepository);
|
vetService = new VetService(vetRepository, specialtyRepository);
|
||||||
vet = new Vet();
|
vet = new Vet();
|
||||||
vet.setId(VET_ID);
|
vet.setId(VET_ID);
|
||||||
vet.setFirstName(VET_FIRST_NAME);
|
vet.setFirstName(VET_FIRST_NAME);
|
||||||
|
|
Loading…
Reference in a new issue