mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-22 07:15:49 +00:00
PetDTO fix loop in equal testing for OwnerDTO attribute
This commit is contained in:
parent
c6b54da5df
commit
f7f07f42dd
7 changed files with 25 additions and 17 deletions
|
@ -110,7 +110,7 @@ class PetController {
|
|||
|
||||
@PostMapping(CommonEndPoint.PETS_ID_EDIT)
|
||||
public String processUpdateForm(@ModelAttribute(CommonAttribute.PET) @Valid PetDTO pet, BindingResult result,
|
||||
OwnerDTO owner, ModelMap model) {
|
||||
@ModelAttribute(CommonAttribute.OWNER) OwnerDTO owner, ModelMap model) {
|
||||
if (result.hasErrors()) {
|
||||
pet.setOwner(owner);
|
||||
model.put(CommonAttribute.PET, pet);
|
||||
|
|
|
@ -80,7 +80,7 @@ class VisitController {
|
|||
// Spring MVC calls method loadPetWithVisit(...) before initNewVisitForm is called
|
||||
@GetMapping(CommonEndPoint.VISITS_NEW)
|
||||
public String initNewVisitForm(@PathVariable("petId") int petId, Map<String, Object> model) {
|
||||
return CommonView.PET_CREATE_OR_UPDATE;
|
||||
return CommonView.VISIT_CREATE_OR_UPDATE;
|
||||
}
|
||||
|
||||
// Spring MVC calls method loadPetWithVisit(...) before processNewVisitForm is called
|
||||
|
|
|
@ -86,7 +86,7 @@ public class OwnerDTO extends PersonDTO {
|
|||
|
||||
public void addPet(PetDTO pet) {
|
||||
|
||||
if (!this.getPets().contains(pet)) {
|
||||
if (pet.isNew() || this.getPets() == null || !this.getPets().contains(pet)) {
|
||||
getPetsInternal().add(pet);
|
||||
}
|
||||
pet.setOwner(this);
|
||||
|
|
|
@ -90,6 +90,8 @@ public class PetDTO extends NamedDTO {
|
|||
return true;
|
||||
if (!(o instanceof PetDTO))
|
||||
return false;
|
||||
if (!super.equals(o))
|
||||
return false;
|
||||
|
||||
PetDTO petDTO = (PetDTO) o;
|
||||
|
||||
|
@ -97,7 +99,7 @@ public class PetDTO extends NamedDTO {
|
|||
return false;
|
||||
if (!getType().equals(petDTO.getType()))
|
||||
return false;
|
||||
if (!getOwner().getId().equals(petDTO.getOwner().getId()))
|
||||
if (getOwner() != null ? !getOwner().getId().equals(petDTO.getOwner().getId()) : petDTO.getOwner() != null)
|
||||
return false;
|
||||
return getVisits() != null ? getVisits().equals(petDTO.getVisits()) : petDTO.getVisits() == null;
|
||||
}
|
||||
|
|
|
@ -15,7 +15,6 @@
|
|||
*/
|
||||
package org.springframework.samples.petclinic.repository;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
|
@ -53,6 +52,13 @@ public interface PetRepository extends Repository<Pet, Integer> {
|
|||
*/
|
||||
Pet findById(Integer id);
|
||||
|
||||
/**
|
||||
* Retrieve all {@link Pet}d from the data store by owner id.
|
||||
* @param id the id of owner to search for
|
||||
* @return a Collection of {@link Pet}s
|
||||
*/
|
||||
List<Pet> findByOwnerId(Integer id);
|
||||
|
||||
/**
|
||||
* Retrieve all {@link Pet}s from the data store
|
||||
* @return a Collection of {@link Pet}s (or an empty Collection if none
|
||||
|
|
|
@ -50,7 +50,7 @@ public class PetService implements BaseService<Pet, PetDTO> {
|
|||
dto.getVisits().forEach(visitDTO -> pet.addVisit(visitService.dtoToEntity(visitDTO)));
|
||||
|
||||
dto.getOwner().getPets().forEach(petDTO -> {
|
||||
if (petDTO.getId().equals(dto.getId())) {
|
||||
if (dto.getId() == null || petDTO.getId().equals(dto.getId())) {
|
||||
owner.addPet(pet);
|
||||
}
|
||||
else {
|
||||
|
@ -72,18 +72,14 @@ public class PetService implements BaseService<Pet, PetDTO> {
|
|||
PetDTO petDTO = modelMapper.map(entity, PetDTO.class);
|
||||
OwnerDTO ownerDTO = modelMapper.map(entity.getOwner(), OwnerDTO.class);
|
||||
|
||||
petRepository.findByOwnerId(ownerDTO.getId()).forEach(pet -> {
|
||||
PetDTO otherPetDTO = modelMapper.map(pet, PetDTO.class);
|
||||
otherPetDTO.setOwner(ownerDTO);
|
||||
ownerDTO.addPet(otherPetDTO);
|
||||
});
|
||||
|
||||
entity.getVisits().forEach(visit -> petDTO.addVisit(visitService.entityToDTO(visit)));
|
||||
|
||||
entity.getOwner().getPets().forEach(pet -> {
|
||||
if (pet.getId().equals(entity.getId())) {
|
||||
ownerDTO.addPet(petDTO);
|
||||
}
|
||||
else {
|
||||
PetDTO otherPetDTO = modelMapper.map(pet, PetDTO.class);
|
||||
otherPetDTO.setOwner(ownerDTO);
|
||||
ownerDTO.addPet(otherPetDTO);
|
||||
}
|
||||
});
|
||||
petDTO.setOwner(ownerDTO);
|
||||
return petDTO;
|
||||
}
|
||||
|
@ -125,6 +121,10 @@ public class PetService implements BaseService<Pet, PetDTO> {
|
|||
petRepository.save(dtoToEntity(petDTO));
|
||||
}
|
||||
|
||||
public List<PetDTO> findByOwnerId(int id) {
|
||||
return entitiesToDTOS(petRepository.findByOwnerId(id));
|
||||
}
|
||||
|
||||
public List<PetTypeDTO> findPetTypes() {
|
||||
List<PetType> petTypes = petRepository.findPetTypes();
|
||||
|
||||
|
|
|
@ -65,7 +65,7 @@ class VisitControllerTests {
|
|||
@Tag("initNewVisitForm")
|
||||
void testInitNewVisitForm() throws Exception {
|
||||
mockMvc.perform(get(CommonEndPoint.VISITS_NEW, TEST_PET_ID)).andExpect(status().isOk())
|
||||
.andExpect(view().name(CommonView.PET_CREATE_OR_UPDATE));
|
||||
.andExpect(view().name(CommonView.VISIT_CREATE_OR_UPDATE));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in a new issue