/** * Custom handler for displaying an owner. * * @param ownerId the ID of the owner to display * @return a ModelMap with the model attributes for the view */ @GetMapping("/owners/{ownerId}") public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) { ModelAndView mav = new ModelAndView("owners/ownerDetails"); mav.addObject(this.owners.findById(ownerId)); return mav; }
@GetMapping("/owners") public String processFindForm(Owner owner, BindingResult result, Map<String, Object> model) { // allow parameterless GET request for /owners to return all records if (owner.getLastName() == null) { owner.setLastName(""); // empty string signifies broadest possible search } // find owners by last name Collection<Owner> results = this.owners.findByLastName(owner.getLastName()); if (results.isEmpty()) { // no owners found result.rejectValue("lastName", "notFound", "not found"); return "owners/findOwners"; } else if (results.size() == 1) { // 1 owner found owner = results.iterator().next(); return "redirect:/owners/" + owner.getId(); } else { // multiple owners found model.put("selections", results); return "owners/ownersList"; } }
@PostMapping("/owners/new") public String processCreationForm(@Valid Owner owner, BindingResult result) { if (result.hasErrors()) { return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; } else { this.owners.save(owner); return "redirect:/owners/" + owner.getId(); } }
@ModelAttribute("owner") public Owner findOwner(@PathVariable("ownerId") int ownerId) { return this.owners.findById(ownerId); }
@PostMapping("/owners/{ownerId}/edit") public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result, @PathVariable("ownerId") int ownerId) { if (result.hasErrors()) { return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; } else { owner.setId(ownerId); this.owners.save(owner); return "redirect:/owners/{ownerId}"; } }
@RequestMapping(value = "/owners", method = RequestMethod.GET) public String processFindForm(Owner owner, BindingResult result, Map<String, Object> model) { // allow parameterless GET request for /owners to return all records if (owner.getLastName() == null) { owner.setLastName(""); // empty string signifies broadest possible search } // find owners by last name Collection<Owner> results = this.owners.findByLastName(owner.getLastName()); if (results.isEmpty()) { // no owners found result.rejectValue("lastName", "notFound", "not found"); return "owners/findOwners"; } else if (results.size() == 1) { // 1 owner found owner = results.iterator().next(); return "redirect:/owners/" + owner.getId(); } else { // multiple owners found model.put("selections", results); return "owners/ownersList"; } }
@GetMapping("/owners/{ownerId}/edit") public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) { Owner owner = this.owners.findById(ownerId); model.addAttribute(owner); return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; }
@RequestMapping(value = "/owners/new", method = RequestMethod.POST) public String processCreationForm(@Valid Owner owner, BindingResult result) { if (result.hasErrors()) { return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; } else { this.owners.save(owner); return "redirect:/owners/" + owner.getId(); } }
@RequestMapping(value = "/owners/{ownerId}/edit", method = RequestMethod.GET) public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) { Owner owner = this.owners.findById(ownerId); model.addAttribute(owner); return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; }
@RequestMapping(value = "/owners/{ownerId}/edit", method = RequestMethod.POST) public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result, @PathVariable("ownerId") int ownerId) { if (result.hasErrors()) { return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; } else { owner.setId(ownerId); this.owners.save(owner); return "redirect:/owners/{ownerId}"; } }
@ModelAttribute("owner") public Owner findOwner(@PathVariable("ownerId") int ownerId) { return this.owners.findById(ownerId); }
/** * Custom handler for displaying an owner. * * @param ownerId the ID of the owner to display * @return a ModelMap with the model attributes for the view */ @RequestMapping("/owners/{ownerId}") public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) { ModelAndView mav = new ModelAndView("owners/ownerDetails"); mav.addObject(this.owners.findById(ownerId)); return mav; }
@RequestMapping(value = "/owner/{ownerId}/getVisits", method = RequestMethod.GET) public ResponseEntity<List<Visit>> getOwnerVisits(@PathVariable int ownerId){ List<Pet> petList = this.owners.findById(ownerId).getPets(); List<Visit> visitList = new ArrayList<Visit>(); petList.forEach(pet -> visitList.addAll(pet.getVisits())); return new ResponseEntity<List<Visit>>(visitList, HttpStatus.OK); } }