From f429502898f56ff13188727101fae838cbcddf96 Mon Sep 17 00:00:00 2001 From: viru Date: Sun, 13 Oct 2024 14:42:21 +0530 Subject: [PATCH] Improved Validation for Birth Dates AND Duplicate Pet Name Check Enhancement: --- .../petclinic/owner/PetController.java | 209 ++++++++---------- 1 file changed, 90 insertions(+), 119 deletions(-) diff --git a/src/main/java/org/springframework/samples/petclinic/owner/PetController.java b/src/main/java/org/springframework/samples/petclinic/owner/PetController.java index 781fb5805..d8ef2e38d 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/PetController.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/PetController.java @@ -1,18 +1,3 @@ -/* - * Copyright 2012-2019 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ package org.springframework.samples.petclinic.owner; import java.time.LocalDate; @@ -33,130 +18,116 @@ import org.springframework.web.bind.annotation.RequestMapping; import jakarta.validation.Valid; import org.springframework.web.servlet.mvc.support.RedirectAttributes; -/** - * @author Juergen Hoeller - * @author Ken Krebs - * @author Arjen Poutsma - */ @Controller @RequestMapping("/owners/{ownerId}") class PetController { - private static final String VIEWS_PETS_CREATE_OR_UPDATE_FORM = "pets/createOrUpdatePetForm"; + private static final String VIEWS_PETS_CREATE_OR_UPDATE_FORM = "pets/createOrUpdatePetForm"; - private final OwnerRepository owners; + private final OwnerRepository owners; - public PetController(OwnerRepository owners) { - this.owners = owners; - } + public PetController(OwnerRepository owners) { + this.owners = owners; + } - @ModelAttribute("types") - public Collection populatePetTypes() { - return this.owners.findPetTypes(); - } + @ModelAttribute("types") + public Collection populatePetTypes() { + return this.owners.findPetTypes(); + } - @ModelAttribute("owner") - public Owner findOwner(@PathVariable("ownerId") int ownerId) { + @ModelAttribute("owner") + public Owner findOwner(@PathVariable("ownerId") int ownerId) { + Owner owner = this.owners.findById(ownerId); + if (owner == null) { + throw new IllegalArgumentException("Owner ID not found: " + ownerId); + } + return owner; + } - Owner owner = this.owners.findById(ownerId); - if (owner == null) { - throw new IllegalArgumentException("Owner ID not found: " + ownerId); - } - return owner; - } + @ModelAttribute("pet") + public Pet findPet(@PathVariable("ownerId") int ownerId, + @PathVariable(name = "petId", required = false) Integer petId) { + if (petId == null) { + return new Pet(); + } + Owner owner = this.owners.findById(ownerId); + if (owner == null) { + throw new IllegalArgumentException("Owner ID not found: " + ownerId); + } + return owner.getPet(petId); + } - @ModelAttribute("pet") - public Pet findPet(@PathVariable("ownerId") int ownerId, - @PathVariable(name = "petId", required = false) Integer petId) { + @InitBinder("owner") + public void initOwnerBinder(WebDataBinder dataBinder) { + dataBinder.setDisallowedFields("id"); + } - if (petId == null) { - return new Pet(); - } + @InitBinder("pet") + public void initPetBinder(WebDataBinder dataBinder) { + dataBinder.setValidator(new PetValidator()); + } - Owner owner = this.owners.findById(ownerId); - if (owner == null) { - throw new IllegalArgumentException("Owner ID not found: " + ownerId); - } - return owner.getPet(petId); - } + @GetMapping("/pets/new") + public String initCreationForm(Owner owner, ModelMap model) { + Pet pet = new Pet(); + owner.addPet(pet); + model.put("pet", pet); + return VIEWS_PETS_CREATE_OR_UPDATE_FORM; + } - @InitBinder("owner") - public void initOwnerBinder(WebDataBinder dataBinder) { - dataBinder.setDisallowedFields("id"); - } + @PostMapping("/pets/new") + public String processCreationForm(Owner owner, @Valid Pet pet, BindingResult result, ModelMap model, + RedirectAttributes redirectAttributes) { + validatePetNameAndBirthDate(owner, pet, result); - @InitBinder("pet") - public void initPetBinder(WebDataBinder dataBinder) { - dataBinder.setValidator(new PetValidator()); - } + if (result.hasErrors()) { + model.put("pet", pet); + return VIEWS_PETS_CREATE_OR_UPDATE_FORM; + } - @GetMapping("/pets/new") - public String initCreationForm(Owner owner, ModelMap model) { - Pet pet = new Pet(); - owner.addPet(pet); - model.put("pet", pet); - return VIEWS_PETS_CREATE_OR_UPDATE_FORM; - } + owner.addPet(pet); + this.owners.save(owner); + redirectAttributes.addFlashAttribute("message", "New Pet has been Added"); + return "redirect:/owners/{ownerId}"; + } - @PostMapping("/pets/new") - public String processCreationForm(Owner owner, @Valid Pet pet, BindingResult result, ModelMap model, - RedirectAttributes redirectAttributes) { - if (StringUtils.hasText(pet.getName()) && pet.isNew() && owner.getPet(pet.getName(), true) != null) { - result.rejectValue("name", "duplicate", "already exists"); - } + @GetMapping("/pets/{petId}/edit") + public String initUpdateForm(Owner owner, @PathVariable("petId") int petId, ModelMap model) { + Pet pet = owner.getPet(petId); + if (pet == null) { + throw new IllegalArgumentException("Pet ID not found: " + petId); + } + model.put("pet", pet); + return VIEWS_PETS_CREATE_OR_UPDATE_FORM; + } - LocalDate currentDate = LocalDate.now(); - if (pet.getBirthDate() != null && pet.getBirthDate().isAfter(currentDate)) { - result.rejectValue("birthDate", "typeMismatch.birthDate"); - } + @PostMapping("/pets/{petId}/edit") + public String processUpdateForm(@Valid Pet pet, BindingResult result, Owner owner, ModelMap model, + RedirectAttributes redirectAttributes) { + validatePetNameAndBirthDate(owner, pet, result); - owner.addPet(pet); - if (result.hasErrors()) { - model.put("pet", pet); - return VIEWS_PETS_CREATE_OR_UPDATE_FORM; - } + if (result.hasErrors()) { + model.put("pet", pet); + return VIEWS_PETS_CREATE_OR_UPDATE_FORM; + } - this.owners.save(owner); - redirectAttributes.addFlashAttribute("message", "New Pet has been Added"); - return "redirect:/owners/{ownerId}"; - } + owner.addPet(pet); + this.owners.save(owner); + redirectAttributes.addFlashAttribute("message", "Pet details have been edited"); + return "redirect:/owners/{ownerId}"; + } - @GetMapping("/pets/{petId}/edit") - public String initUpdateForm(Owner owner, @PathVariable("petId") int petId, ModelMap model, - RedirectAttributes redirectAttributes) { - Pet pet = owner.getPet(petId); - model.put("pet", pet); - return VIEWS_PETS_CREATE_OR_UPDATE_FORM; - } + private void validatePetNameAndBirthDate(Owner owner, Pet pet, BindingResult result) { + String petName = pet.getName(); - @PostMapping("/pets/{petId}/edit") - public String processUpdateForm(@Valid Pet pet, BindingResult result, Owner owner, ModelMap model, - RedirectAttributes redirectAttributes) { - - String petName = pet.getName(); - - // checking if the pet name already exist for the owner - if (StringUtils.hasText(petName)) { - Pet existingPet = owner.getPet(petName.toLowerCase(), false); - if (existingPet != null && existingPet.getId() != pet.getId()) { - result.rejectValue("name", "duplicate", "already exists"); - } - } - - LocalDate currentDate = LocalDate.now(); - if (pet.getBirthDate() != null && pet.getBirthDate().isAfter(currentDate)) { - result.rejectValue("birthDate", "typeMismatch.birthDate"); - } - - if (result.hasErrors()) { - model.put("pet", pet); - return VIEWS_PETS_CREATE_OR_UPDATE_FORM; - } - - owner.addPet(pet); - this.owners.save(owner); - redirectAttributes.addFlashAttribute("message", "Pet details has been edited"); - return "redirect:/owners/{ownerId}"; - } + // Checking if the pet name already exists for the owner + if (StringUtils.hasText(petName) && owner.getPet(petName, true) != null) { + result.rejectValue("name", "duplicate", "already exists"); + } + LocalDate currentDate = LocalDate.now(); + if (pet.getBirthDate() == null || pet.getBirthDate().isAfter(currentDate)) { + result.rejectValue("birthDate", "typeMismatch.birthDate"); + } + } }