mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-17 21:35:50 +00:00
Compare commits
1 commit
3dc92bb2f1
...
92899cfe0b
Author | SHA1 | Date | |
---|---|---|---|
![]() |
92899cfe0b |
1 changed files with 3 additions and 21 deletions
|
@ -129,7 +129,7 @@ class PetController {
|
||||||
|
|
||||||
String petName = pet.getName();
|
String petName = pet.getName();
|
||||||
|
|
||||||
// checking if the pet name already exists for the owner
|
// checking if the pet name already exist for the owner
|
||||||
if (StringUtils.hasText(petName)) {
|
if (StringUtils.hasText(petName)) {
|
||||||
Pet existingPet = owner.getPet(petName, false);
|
Pet existingPet = owner.getPet(petName, false);
|
||||||
if (existingPet != null && !existingPet.getId().equals(pet.getId())) {
|
if (existingPet != null && !existingPet.getId().equals(pet.getId())) {
|
||||||
|
@ -146,28 +146,10 @@ class PetController {
|
||||||
return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
|
return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
|
||||||
}
|
}
|
||||||
|
|
||||||
updatePetDetails(owner, pet);
|
owner.addPet(pet);
|
||||||
|
this.owners.save(owner);
|
||||||
redirectAttributes.addFlashAttribute("message", "Pet details has been edited");
|
redirectAttributes.addFlashAttribute("message", "Pet details has been edited");
|
||||||
return "redirect:/owners/{ownerId}";
|
return "redirect:/owners/{ownerId}";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Updates the pet details if it exists or adds a new pet to the owner.
|
|
||||||
* @param owner The owner of the pet
|
|
||||||
* @param pet The pet with updated details
|
|
||||||
*/
|
|
||||||
private void updatePetDetails(Owner owner, Pet pet) {
|
|
||||||
Pet existingPet = owner.getPet(pet.getId());
|
|
||||||
if (existingPet != null) {
|
|
||||||
// Update existing pet's properties
|
|
||||||
existingPet.setName(pet.getName());
|
|
||||||
existingPet.setBirthDate(pet.getBirthDate());
|
|
||||||
existingPet.setType(pet.getType());
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
owner.addPet(pet);
|
|
||||||
}
|
|
||||||
this.owners.save(owner);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue