mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-04-26 12:22:47 +00:00
made sure the ClinicService facade is used by all Controllers
This commit is contained in:
parent
4d6496e18e
commit
a994785f73
3 changed files with 28 additions and 9 deletions
|
@ -19,6 +19,7 @@ import org.springframework.samples.petclinic.Visit;
|
||||||
* @author Ken Krebs
|
* @author Ken Krebs
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
* @author Sam Brannen
|
* @author Sam Brannen
|
||||||
|
* @author Michael Isvy
|
||||||
*/
|
*/
|
||||||
public interface ClinicService {
|
public interface ClinicService {
|
||||||
|
|
||||||
|
@ -34,4 +35,8 @@ public interface ClinicService {
|
||||||
|
|
||||||
public Collection<Vet> findVets() throws DataAccessException;
|
public Collection<Vet> findVets() throws DataAccessException;
|
||||||
|
|
||||||
|
public void saveOwner(Owner owner) throws DataAccessException;
|
||||||
|
|
||||||
|
Collection<Owner> findOwnerByLastName(String lastName) throws DataAccessException;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,11 +42,25 @@ public class ClinicServiceImpl implements ClinicService {
|
||||||
return ownerRepository.findById(id);
|
return ownerRepository.findById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Transactional(readOnly=true)
|
||||||
|
public Collection<Owner> findOwnerByLastName(String lastName) throws DataAccessException {
|
||||||
|
return ownerRepository.findByLastName(lastName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Transactional
|
||||||
|
public void saveOwner(Owner owner) throws DataAccessException {
|
||||||
|
ownerRepository.save(owner);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public void saveVisit(Visit visit) throws DataAccessException {
|
public void saveVisit(Visit visit) throws DataAccessException {
|
||||||
visitRepository.save(visit);
|
visitRepository.save(visit);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Transactional(readOnly=true)
|
@Transactional(readOnly=true)
|
||||||
public Pet findPetById(int id) throws DataAccessException {
|
public Pet findPetById(int id) throws DataAccessException {
|
||||||
return petRepository.findById(id);
|
return petRepository.findById(id);
|
||||||
|
|
|
@ -7,7 +7,7 @@ import javax.validation.Valid;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.samples.petclinic.Owner;
|
import org.springframework.samples.petclinic.Owner;
|
||||||
import org.springframework.samples.petclinic.repository.OwnerRepository;
|
import org.springframework.samples.petclinic.service.ClinicService;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.validation.BindingResult;
|
import org.springframework.validation.BindingResult;
|
||||||
|
@ -32,12 +32,12 @@ import org.springframework.web.servlet.ModelAndView;
|
||||||
@SessionAttributes(types = Owner.class)
|
@SessionAttributes(types = Owner.class)
|
||||||
public class OwnerController {
|
public class OwnerController {
|
||||||
|
|
||||||
private final OwnerRepository ownerRepository;
|
private final ClinicService clinicService;
|
||||||
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public OwnerController(OwnerRepository ownerRepository) {
|
public OwnerController(ClinicService clinicService) {
|
||||||
this.ownerRepository = ownerRepository;
|
this.clinicService = clinicService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@InitBinder
|
@InitBinder
|
||||||
|
@ -58,7 +58,7 @@ public class OwnerController {
|
||||||
return "owners/createOrUpdateOwnerForm";
|
return "owners/createOrUpdateOwnerForm";
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this.ownerRepository.save(owner);
|
this.clinicService.saveOwner(owner);
|
||||||
status.setComplete();
|
status.setComplete();
|
||||||
return "redirect:/owners/" + owner.getId();
|
return "redirect:/owners/" + owner.getId();
|
||||||
}
|
}
|
||||||
|
@ -79,7 +79,7 @@ public class OwnerController {
|
||||||
}
|
}
|
||||||
|
|
||||||
// find owners by last name
|
// find owners by last name
|
||||||
Collection<Owner> results = this.ownerRepository.findByLastName(owner.getLastName());
|
Collection<Owner> results = this.clinicService.findOwnerByLastName(owner.getLastName());
|
||||||
if (results.size() < 1) {
|
if (results.size() < 1) {
|
||||||
// no owners found
|
// no owners found
|
||||||
result.rejectValue("lastName", "notFound", "not found");
|
result.rejectValue("lastName", "notFound", "not found");
|
||||||
|
@ -99,7 +99,7 @@ public class OwnerController {
|
||||||
|
|
||||||
@RequestMapping(value="/owners/{ownerId}/edit", method = RequestMethod.GET)
|
@RequestMapping(value="/owners/{ownerId}/edit", method = RequestMethod.GET)
|
||||||
public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) {
|
public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) {
|
||||||
Owner owner = this.ownerRepository.findById(ownerId);
|
Owner owner = this.clinicService.findOwnerById(ownerId);
|
||||||
model.addAttribute(owner);
|
model.addAttribute(owner);
|
||||||
return "owners/createOrUpdateOwnerForm";
|
return "owners/createOrUpdateOwnerForm";
|
||||||
}
|
}
|
||||||
|
@ -110,7 +110,7 @@ public class OwnerController {
|
||||||
return "owners/createOrUpdateOwnerForm";
|
return "owners/createOrUpdateOwnerForm";
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this.ownerRepository.save(owner);
|
this.clinicService.saveOwner(owner);
|
||||||
status.setComplete();
|
status.setComplete();
|
||||||
return "redirect:/owners/{ownerId}";
|
return "redirect:/owners/{ownerId}";
|
||||||
}
|
}
|
||||||
|
@ -125,7 +125,7 @@ public class OwnerController {
|
||||||
@RequestMapping("/owners/{ownerId}")
|
@RequestMapping("/owners/{ownerId}")
|
||||||
public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) {
|
public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) {
|
||||||
ModelAndView mav = new ModelAndView("owners/ownerDetails");
|
ModelAndView mav = new ModelAndView("owners/ownerDetails");
|
||||||
mav.addObject(this.ownerRepository.findById(ownerId));
|
mav.addObject(this.clinicService.findOwnerById(ownerId));
|
||||||
return mav;
|
return mav;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue