From bed236444e78f9fec3c83eb1d3bc6e3a8eee18fd Mon Sep 17 00:00:00 2001 From: "J. A. Parejo" Date: Wed, 12 Feb 2020 22:50:12 +0100 Subject: [PATCH] Validation of unique pet name moved to Clinic Service --- .../service/exceptions/DuplicatedPetNameException.java | 2 +- .../springframework/samples/petclinic/web/PetController.java | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/springframework/samples/petclinic/service/exceptions/DuplicatedPetNameException.java b/src/main/java/org/springframework/samples/petclinic/service/exceptions/DuplicatedPetNameException.java index 7309b1aa4f5..b10347ea519 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/exceptions/DuplicatedPetNameException.java +++ b/src/main/java/org/springframework/samples/petclinic/service/exceptions/DuplicatedPetNameException.java @@ -9,6 +9,6 @@ * * @author japarejo */ -public class DuplicatedPetNameException extends RuntimeException{ +public class DuplicatedPetNameException extends Exception{ } diff --git a/src/main/java/org/springframework/samples/petclinic/web/PetController.java b/src/main/java/org/springframework/samples/petclinic/web/PetController.java index bf1b47b10e1..53de47800c3 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/PetController.java +++ b/src/main/java/org/springframework/samples/petclinic/web/PetController.java @@ -137,7 +137,12 @@ public String processUpdateForm(@Valid Pet pet, BindingResult result, Owner owne else { Pet petToUpdate=this.clinicService.findPetById(petId); BeanUtils.copyProperties(pet, petToUpdate, "id","owner","visits"); + try { this.clinicService.savePet(petToUpdate); + } catch (DuplicatedPetNameException ex) { + result.rejectValue("name", "duplicate", "already exists"); + return VIEWS_PETS_CREATE_OR_UPDATE_FORM; + } return "redirect:/owners/{ownerId}"; } }