Skip to content
Snippets Groups Projects
Commit a994785f authored by Mic's avatar Mic
Browse files

made sure the ClinicService facade is used by all Controllers

parent 4d6496e1
No related branches found
No related tags found
No related merge requests found
...@@ -19,6 +19,7 @@ import org.springframework.samples.petclinic.Visit; ...@@ -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 { ...@@ -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;
} }
...@@ -41,11 +41,25 @@ public class ClinicServiceImpl implements ClinicService { ...@@ -41,11 +41,25 @@ public class ClinicServiceImpl implements ClinicService {
public Owner findOwnerById(int id) throws DataAccessException { public Owner findOwnerById(int id) throws DataAccessException {
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 {
......
...@@ -7,7 +7,7 @@ import javax.validation.Valid; ...@@ -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; ...@@ -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 { ...@@ -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 { ...@@ -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 { ...@@ -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 { ...@@ -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 { ...@@ -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;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment