Skip to content

Commit

Permalink
Validation of unique pet name moved to Clinic Service
Browse files Browse the repository at this point in the history
  • Loading branch information
japarejo committed Feb 12, 2020
1 parent 417b44e commit f3b5294
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
import org.springframework.samples.petclinic.repository.PetRepository;
import org.springframework.samples.petclinic.repository.VetRepository;
import org.springframework.samples.petclinic.repository.VisitRepository;
import org.springframework.samples.petclinic.service.exceptions.DuplicatedPetNameException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
* Mostly used as a facade for all Petclinic controllers Also a placeholder
Expand Down Expand Up @@ -100,8 +102,11 @@ public Pet findPetById(int id) throws DataAccessException {
}

@Transactional
public void savePet(Pet pet) throws DataAccessException {
petRepository.save(pet);
public void savePet(Pet pet) throws DataAccessException, DuplicatedPetNameException {
if (StringUtils.hasLength(pet.getName()) && pet.isNew() && pet.getOwner().getPet(pet.getName(), true) != null) {
throw new DuplicatedPetNameException();
}
petRepository.save(pet);
}

@Transactional(readOnly = true)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.springframework.samples.petclinic.service.exceptions;

/**
*
* @author japarejo
*/
public class DuplicatedPetNameException extends RuntimeException{

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@
import javax.validation.Valid;

import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.samples.petclinic.model.Visit;
import org.springframework.samples.petclinic.service.exceptions.DuplicatedPetNameException;

/**
* @author Juergen Hoeller
Expand Down Expand Up @@ -90,18 +94,20 @@ public String initCreationForm(Owner owner, ModelMap model) {
}

@PostMapping(value = "/pets/new")
public String processCreationForm(Owner owner, @Valid Pet pet, BindingResult result, ModelMap model) {
if (StringUtils.hasLength(pet.getName()) && pet.isNew() && owner.getPet(pet.getName(), true) != null) {
result.rejectValue("name", "duplicate", "already exists");
}
public String processCreationForm(Owner owner, @Valid Pet pet, BindingResult result, ModelMap model) {
if (result.hasErrors()) {
model.put("pet", pet);
return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
}
else {
try{
owner.addPet(pet);
this.clinicService.savePet(pet);
return "redirect:/owners/{ownerId}";
}catch(DuplicatedPetNameException ex){
result.rejectValue("name", "duplicate", "already exists");
return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
}
return "redirect:/owners/{ownerId}";
}
}

Expand Down Expand Up @@ -130,8 +136,8 @@ public String processUpdateForm(@Valid Pet pet, BindingResult result, Owner owne
}
else {
Pet petToUpdate=this.clinicService.findPetById(petId);
BeanUtils.copyProperties(pet, petToUpdate, "id","owner","visits");
this.clinicService.savePet(petToUpdate);
BeanUtils.copyProperties(pet, petToUpdate, "id","owner","visits");
this.clinicService.savePet(petToUpdate);
return "redirect:/owners/{ownerId}";
}
}
Expand Down

0 comments on commit f3b5294

Please sign in to comment.