mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-04-24 19:32:49 +00:00
refactor: extract update logic to separate method in PetController
This commit is contained in:
parent
101ee08c8e
commit
2764a42b5b
1 changed files with 11 additions and 8 deletions
|
@ -146,23 +146,26 @@ class PetController {
|
|||
return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
|
||||
}
|
||||
|
||||
updatePetDetails(owner, pet);
|
||||
redirectAttributes.addFlashAttribute("message", "Pet details has been edited");
|
||||
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());
|
||||
|
||||
this.owners.save(owner);
|
||||
} else {
|
||||
|
||||
owner.addPet(pet);
|
||||
this.owners.save(owner);
|
||||
}
|
||||
|
||||
redirectAttributes.addFlashAttribute("message", "Pet details has been edited");
|
||||
return "redirect:/owners/{ownerId}";
|
||||
this.owners.save(owner);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue