refactor: extract update logic to separate method in PetController

This commit is contained in:
VishantOwO 2025-01-02 13:56:19 +05:30
parent 101ee08c8e
commit 2764a42b5b

View file

@ -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);
}
}