Improved the Owner search query

Signed-off-by: Perumal Raj A <perumalraj024@gmail.com>
This commit is contained in:
Perumal Raj A 2025-02-22 05:38:18 +00:00
parent 2ab1cef91f
commit e15623d232
2 changed files with 20 additions and 28 deletions

View file

@ -92,33 +92,27 @@ class OwnerController {
@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) {
try { // 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
Page<Owner> ownersResults = findPaginatedForOwnersLastName(page, owner.getLastName());
if (ownersResults.isEmpty()) {
// no owners found
result.rejectValue("lastName", "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);
} }
catch (Exception e) {
e.printStackTrace();// print exception to console or log in file // find owners by last name
return "error"; // redirect to generic error page Page<Owner> ownersResults = findPaginatedForOwnersLastName(page, owner.getLastName());
if (ownersResults.isEmpty()) {
// no owners found
result.rejectValue("lastName", "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);
} }
private String addPaginationModel(int page, Model model, Page<Owner> paginated) { private String addPaginationModel(int page, Model model, Page<Owner> paginated) {

View file

@ -18,10 +18,8 @@ management.endpoints.web.exposure.include=*
# Logging # Logging
logging.level.org.springframework=INFO logging.level.org.springframework=INFO
logging.level.org.springframework.web=DEBUG # logging.level.org.springframework.web=DEBUG
logging.level.org.springframework.context.annotation=TRACE # logging.level.org.springframework.context.annotation=TRACE
logging.level.org.springframework=DEBUG
logging.level.org.hibernate.SQL=DEBUG
# Maximum time static resources should be cached # Maximum time static resources should be cached
spring.web.resources.cache.cachecontrol.max-age=12h spring.web.resources.cache.cachecontrol.max-age=12h