mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-20 14:55:50 +00:00
Improved Validation for Birth Dates AND Duplicate Pet Name Check Enhancement:
This commit is contained in:
parent
62dbfa8e9a
commit
f429502898
1 changed files with 90 additions and 119 deletions
|
@ -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;
|
package org.springframework.samples.petclinic.owner;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
|
@ -33,11 +18,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Juergen Hoeller
|
|
||||||
* @author Ken Krebs
|
|
||||||
* @author Arjen Poutsma
|
|
||||||
*/
|
|
||||||
@Controller
|
@Controller
|
||||||
@RequestMapping("/owners/{ownerId}")
|
@RequestMapping("/owners/{ownerId}")
|
||||||
class PetController {
|
class PetController {
|
||||||
|
@ -57,7 +37,6 @@ class PetController {
|
||||||
|
|
||||||
@ModelAttribute("owner")
|
@ModelAttribute("owner")
|
||||||
public Owner findOwner(@PathVariable("ownerId") int ownerId) {
|
public Owner findOwner(@PathVariable("ownerId") int ownerId) {
|
||||||
|
|
||||||
Owner owner = this.owners.findById(ownerId);
|
Owner owner = this.owners.findById(ownerId);
|
||||||
if (owner == null) {
|
if (owner == null) {
|
||||||
throw new IllegalArgumentException("Owner ID not found: " + ownerId);
|
throw new IllegalArgumentException("Owner ID not found: " + ownerId);
|
||||||
|
@ -68,11 +47,9 @@ class PetController {
|
||||||
@ModelAttribute("pet")
|
@ModelAttribute("pet")
|
||||||
public Pet findPet(@PathVariable("ownerId") int ownerId,
|
public Pet findPet(@PathVariable("ownerId") int ownerId,
|
||||||
@PathVariable(name = "petId", required = false) Integer petId) {
|
@PathVariable(name = "petId", required = false) Integer petId) {
|
||||||
|
|
||||||
if (petId == null) {
|
if (petId == null) {
|
||||||
return new Pet();
|
return new Pet();
|
||||||
}
|
}
|
||||||
|
|
||||||
Owner owner = this.owners.findById(ownerId);
|
Owner owner = this.owners.findById(ownerId);
|
||||||
if (owner == null) {
|
if (owner == null) {
|
||||||
throw new IllegalArgumentException("Owner ID not found: " + ownerId);
|
throw new IllegalArgumentException("Owner ID not found: " + ownerId);
|
||||||
|
@ -101,30 +78,25 @@ class PetController {
|
||||||
@PostMapping("/pets/new")
|
@PostMapping("/pets/new")
|
||||||
public String processCreationForm(Owner owner, @Valid Pet pet, BindingResult result, ModelMap model,
|
public String processCreationForm(Owner owner, @Valid Pet pet, BindingResult result, ModelMap model,
|
||||||
RedirectAttributes redirectAttributes) {
|
RedirectAttributes redirectAttributes) {
|
||||||
if (StringUtils.hasText(pet.getName()) && pet.isNew() && owner.getPet(pet.getName(), true) != null) {
|
validatePetNameAndBirthDate(owner, pet, result);
|
||||||
result.rejectValue("name", "duplicate", "already exists");
|
|
||||||
}
|
|
||||||
|
|
||||||
LocalDate currentDate = LocalDate.now();
|
|
||||||
if (pet.getBirthDate() != null && pet.getBirthDate().isAfter(currentDate)) {
|
|
||||||
result.rejectValue("birthDate", "typeMismatch.birthDate");
|
|
||||||
}
|
|
||||||
|
|
||||||
owner.addPet(pet);
|
|
||||||
if (result.hasErrors()) {
|
if (result.hasErrors()) {
|
||||||
model.put("pet", pet);
|
model.put("pet", pet);
|
||||||
return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
|
return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
owner.addPet(pet);
|
||||||
this.owners.save(owner);
|
this.owners.save(owner);
|
||||||
redirectAttributes.addFlashAttribute("message", "New Pet has been Added");
|
redirectAttributes.addFlashAttribute("message", "New Pet has been Added");
|
||||||
return "redirect:/owners/{ownerId}";
|
return "redirect:/owners/{ownerId}";
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/pets/{petId}/edit")
|
@GetMapping("/pets/{petId}/edit")
|
||||||
public String initUpdateForm(Owner owner, @PathVariable("petId") int petId, ModelMap model,
|
public String initUpdateForm(Owner owner, @PathVariable("petId") int petId, ModelMap model) {
|
||||||
RedirectAttributes redirectAttributes) {
|
|
||||||
Pet pet = owner.getPet(petId);
|
Pet pet = owner.getPet(petId);
|
||||||
|
if (pet == null) {
|
||||||
|
throw new IllegalArgumentException("Pet ID not found: " + petId);
|
||||||
|
}
|
||||||
model.put("pet", pet);
|
model.put("pet", pet);
|
||||||
return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
|
return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
|
||||||
}
|
}
|
||||||
|
@ -132,21 +104,7 @@ class PetController {
|
||||||
@PostMapping("/pets/{petId}/edit")
|
@PostMapping("/pets/{petId}/edit")
|
||||||
public String processUpdateForm(@Valid Pet pet, BindingResult result, Owner owner, ModelMap model,
|
public String processUpdateForm(@Valid Pet pet, BindingResult result, Owner owner, ModelMap model,
|
||||||
RedirectAttributes redirectAttributes) {
|
RedirectAttributes redirectAttributes) {
|
||||||
|
validatePetNameAndBirthDate(owner, pet, result);
|
||||||
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()) {
|
if (result.hasErrors()) {
|
||||||
model.put("pet", pet);
|
model.put("pet", pet);
|
||||||
|
@ -155,8 +113,21 @@ class PetController {
|
||||||
|
|
||||||
owner.addPet(pet);
|
owner.addPet(pet);
|
||||||
this.owners.save(owner);
|
this.owners.save(owner);
|
||||||
redirectAttributes.addFlashAttribute("message", "Pet details has been edited");
|
redirectAttributes.addFlashAttribute("message", "Pet details have been edited");
|
||||||
return "redirect:/owners/{ownerId}";
|
return "redirect:/owners/{ownerId}";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void validatePetNameAndBirthDate(Owner owner, Pet pet, BindingResult result) {
|
||||||
|
String petName = pet.getName();
|
||||||
|
|
||||||
|
// 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");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue