mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-04-26 04:12:47 +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;
|
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());
|
Pet existingPet = owner.getPet(pet.getId());
|
||||||
if (existingPet != null) {
|
if (existingPet != null) {
|
||||||
// Update existing pet's properties
|
// Update existing pet's properties
|
||||||
existingPet.setName(pet.getName());
|
existingPet.setName(pet.getName());
|
||||||
existingPet.setBirthDate(pet.getBirthDate());
|
existingPet.setBirthDate(pet.getBirthDate());
|
||||||
existingPet.setType(pet.getType());
|
existingPet.setType(pet.getType());
|
||||||
|
|
||||||
this.owners.save(owner);
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
owner.addPet(pet);
|
owner.addPet(pet);
|
||||||
|
}
|
||||||
this.owners.save(owner);
|
this.owners.save(owner);
|
||||||
}
|
}
|
||||||
|
|
||||||
redirectAttributes.addFlashAttribute("message", "Pet details has been edited");
|
|
||||||
return "redirect:/owners/{ownerId}";
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue