mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-16 12:45:48 +00:00
Update OwnerController.java
This commit is contained in:
parent
b1ad8a32fe
commit
9f20e90a0c
1 changed files with 134 additions and 90 deletions
|
@ -44,112 +44,156 @@ import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||||
@Controller
|
@Controller
|
||||||
class OwnerController {
|
class OwnerController {
|
||||||
|
|
||||||
private static final String VIEWS_OWNER_CREATE_OR_UPDATE_FORM = "owners/createOrUpdateOwnerForm";
|
private static final String VIEWS_OWNER_CREATE_OR_UPDATE_FORM = "owners/createOrUpdateOwnerForm";
|
||||||
|
|
||||||
private final OwnerRepository owners;
|
private final OwnerRepository owners;
|
||||||
|
|
||||||
public OwnerController(OwnerRepository clinicService) {
|
public OwnerController(OwnerRepository clinicService) {
|
||||||
this.owners = clinicService;
|
this.owners = clinicService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@InitBinder
|
@InitBinder
|
||||||
public void setAllowedFields(WebDataBinder dataBinder) {
|
public void setAllowedFields(WebDataBinder dataBinder) {
|
||||||
dataBinder.setDisallowedFields("id");
|
dataBinder.setDisallowedFields("id");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ModelAttribute("owner")
|
@ModelAttribute("owner")
|
||||||
public Owner findOwner(@PathVariable(name = "ownerId", required = false) Integer ownerId) {
|
public Owner findOwner(@PathVariable(name = "ownerId", required = false) Integer ownerId) {
|
||||||
return ownerId == null ? new Owner() : this.owners.findById(ownerId);
|
return ownerId == null ? new Owner() : this.owners.findById(ownerId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/owners/new")
|
@PostMapping("/owners/new")
|
||||||
public String processCreationForm(@Valid Owner owner, BindingResult result, RedirectAttributes redirectAttributes) {
|
public String processCreationForm(@Valid Owner owner, BindingResult result, RedirectAttributes redirectAttributes) {
|
||||||
if (result.hasErrors()) {
|
if (result.hasErrors()) {
|
||||||
redirectAttributes.addFlashAttribute("error", "There was an error in creating the owner.");
|
redirectAttributes.addFlashAttribute("error", "There was an error in creating the owner.");
|
||||||
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
|
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.owners.save(owner);
|
this.owners.save(owner);
|
||||||
redirectAttributes.addFlashAttribute("message", "New Owner Created");
|
redirectAttributes.addFlashAttribute("message", "New Owner Created");
|
||||||
return "redirect:/owners/" + owner.getId();
|
return "redirect:/owners/" + owner.getId();
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/owners/find")
|
@GetMapping("/owners/find")
|
||||||
public String initFindForm() {
|
public String initFindForm() {
|
||||||
return "owners/findOwners";
|
return "owners/findOwners";
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/owners")
|
@GetMapping("/owners")
|
||||||
public String processFindForm(@RequestParam(defaultValue = "1") int page, Owner owner, BindingResult result,
|
public String processFindForm(@RequestParam(defaultValue = "1") int page, Owner owner, BindingResult result, Model model) {
|
||||||
Model model) {
|
// allow parameterless GET request for /owners to return all records
|
||||||
// allow parameterless GET request for /owners to return all records
|
if (owner.getLastName() == null) {
|
||||||
if (owner.getLastName() == null) {
|
owner.setLastName(""); // empty string signifies broadest possible search
|
||||||
owner.setLastName(""); // empty string signifies broadest possible search
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// find owners by last name
|
// find owners by last name
|
||||||
Page<Owner> ownersResults = findPaginatedForOwnersLastName(page, owner.getLastName());
|
Page<Owner> ownersResults = findPaginatedForOwnersLastName(page, owner.getLastName());
|
||||||
if (ownersResults.isEmpty()) {
|
if (ownersResults.isEmpty()) {
|
||||||
// no owners found
|
// no owners found
|
||||||
result.rejectValue("lastName", "notFound", "not found");
|
result.rejectValue("lastName", "notFound", "not found");
|
||||||
return "owners/findOwners";
|
return "owners/findOwners";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ownersResults.getTotalElements() == 1) {
|
if (ownersResults.getTotalElements() == 1) {
|
||||||
// 1 owner found
|
// 1 owner found
|
||||||
owner = ownersResults.iterator().next();
|
owner = ownersResults.iterator().next();
|
||||||
return "redirect:/owners/" + owner.getId();
|
return "redirect:/owners/" + owner.getId();
|
||||||
}
|
}
|
||||||
|
|
||||||
// multiple owners found
|
// multiple owners found
|
||||||
return addPaginationModel(page, model, ownersResults);
|
return addPaginationModel(page, model, ownersResults);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String addPaginationModel(int page, Model model, Page<Owner> paginated) {
|
private String addPaginationModel(int page, Model model, Page<Owner> paginated) {
|
||||||
List<Owner> listOwners = paginated.getContent();
|
List<Owner> listOwners = paginated.getContent();
|
||||||
model.addAttribute("currentPage", page);
|
model.addAttribute("currentPage", page);
|
||||||
model.addAttribute("totalPages", paginated.getTotalPages());
|
model.addAttribute("totalPages", paginated.getTotalPages());
|
||||||
model.addAttribute("totalItems", paginated.getTotalElements());
|
model.addAttribute("totalItems", paginated.getTotalElements());
|
||||||
model.addAttribute("listOwners", listOwners);
|
model.addAttribute("listOwners", listOwners);
|
||||||
return "owners/ownersList";
|
return "owners/ownersList";
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/owners/{ownerId}/edit")
|
@GetMapping("/owners/{ownerId}/edit")
|
||||||
public String initUpdateOwnerForm() {
|
public String initUpdateOwnerForm() {
|
||||||
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
|
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/owners/{ownerId}/edit")
|
@PostMapping("/owners/{ownerId}/edit")
|
||||||
public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result, @PathVariable("ownerId") int ownerId,
|
public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result, @PathVariable("ownerId") int ownerId,
|
||||||
RedirectAttributes redirectAttributes) {
|
RedirectAttributes redirectAttributes) {
|
||||||
if (result.hasErrors()) {
|
if (result.hasErrors()) {
|
||||||
redirectAttributes.addFlashAttribute("error", "There was an error in updating the owner.");
|
redirectAttributes.addFlashAttribute("error", "There was an error in updating the owner.");
|
||||||
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
|
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (owner.getId() != ownerId) {
|
if (owner.getId() != ownerId) {
|
||||||
result.rejectValue("id", "mismatch", "The owner ID in the form does not match the URL.");
|
result.rejectValue("id", "mismatch", "The owner ID in the form does not match the URL.");
|
||||||
redirectAttributes.addFlashAttribute("error", "Owner ID mismatch. Please try again.");
|
redirectAttributes.addFlashAttribute("error", "Owner ID mismatch. Please try again.");
|
||||||
return "redirect:/owners/{ownerId}/edit";
|
return "redirect:/owners/{ownerId}/edit";
|
||||||
}
|
}
|
||||||
|
|
||||||
owner.setId(ownerId);
|
owner.setId(ownerId);
|
||||||
this.owners.save(owner);
|
this.owners.save(owner);
|
||||||
redirectAttributes.addFlashAttribute("message", "Owner Values Updated");
|
redirectAttributes.addFlashAttribute("message", "Owner Values Updated");
|
||||||
return "redirect:/owners/{ownerId}";
|
return "redirect:/owners/{ownerId}";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Custom handler for displaying an owner.
|
* Custom handler for displaying an owner.
|
||||||
* @param ownerId the ID of the owner to display
|
* @param ownerId the ID of the owner to display
|
||||||
* @return a ModelMap with the model attributes for the view
|
* @return a ModelMap with the model attributes for the view
|
||||||
*/
|
*/
|
||||||
@GetMapping("/owners/{ownerId}")
|
@GetMapping("/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");
|
||||||
Owner owner = this.owners.findById(ownerId);
|
Owner owner = this.owners.findById(ownerId);
|
||||||
mav.addObject(owner);
|
mav.addObject(owner);
|
||||||
return mav;
|
return mav;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// New method to delete an owner by ID
|
||||||
|
@PostMapping("/owners/{ownerId}/delete")
|
||||||
|
public String processDeleteOwner(@PathVariable("ownerId") int ownerId, RedirectAttributes redirectAttributes) {
|
||||||
|
this.owners.deleteById(ownerId);
|
||||||
|
redirectAttributes.addFlashAttribute("message", "Owner Deleted Successfully");
|
||||||
|
return "redirect:/owners";
|
||||||
|
}
|
||||||
|
|
||||||
|
// New method to search owners by first name
|
||||||
|
@GetMapping("/owners/findByFirstName")
|
||||||
|
public String processFindByFirstNameForm(@RequestParam(defaultValue = "1") int page, Owner owner, BindingResult result, Model model) {
|
||||||
|
// Allow parameterless GET request for /owners/findByFirstName to return all records
|
||||||
|
if (owner.getFirstName() == null) {
|
||||||
|
owner.setFirstName(""); // empty string signifies broadest possible search
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use existing method to find owners by first name
|
||||||
|
Page<Owner> ownersResults = findPaginatedForOwnersFirstName(page, owner.getFirstName());
|
||||||
|
if (ownersResults.isEmpty()) {
|
||||||
|
// No owners found
|
||||||
|
result.rejectValue("firstName", "notFound", "not found");
|
||||||
|
return "owners/findOwners";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ownersResults.getTotalElements() == 1) {
|
||||||
|
// 1 owner found
|
||||||
|
owner = ownersResults.iterator().next();
|
||||||
|
return "redirect:/owners/" + owner.getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Multiple owners found
|
||||||
|
return addPaginationModel(page, model, ownersResults);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper method to paginate results for searching by first name
|
||||||
|
private Page<Owner> findPaginatedForOwnersFirstName(int page, String firstName) {
|
||||||
|
Pageable pageable = PageRequest.of(page - 1, 5); // Example page size of 5
|
||||||
|
return this.owners.findByFirstNameContaining(firstName, pageable); // Ensure this method exists in your repository
|
||||||
|
}
|
||||||
|
|
||||||
|
// Existing method to paginate owners by last name
|
||||||
|
private Page<Owner> findPaginatedForOwnersLastName(int page, String lastName) {
|
||||||
|
Pageable pageable = PageRequest.of(page - 1, 5); // Example page size of 5
|
||||||
|
return this.owners.findByLastNameContaining(lastName, pageable); // Ensure this method exists in your repository
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue