Enhance_owner_search_functionality_1.0

Signed-off-by: Perumal Raj A <perumalraj024@gmail.com>
This commit is contained in:
Perumal Raj A 2025-01-22 11:36:31 +00:00
parent 6148ddd967
commit 94d6335022
3 changed files with 15 additions and 7 deletions

View file

@ -98,7 +98,7 @@ class OwnerController {
}
// find owners by last name
Page<Owner> ownersResults = findPaginatedForOwnersLastName(page, owner.getLastName());
Page<Owner> ownersResults = findPaginatedForOwnersName(page, owner.getLastName());
if (ownersResults.isEmpty()) {
// no owners found
result.rejectValue("lastName", "notFound", "not found");
@ -124,10 +124,15 @@ class OwnerController {
return "owners/ownersList";
}
private Page<Owner> findPaginatedForOwnersLastName(int page, String lastname) {
// private Page<Owner> findPaginatedForOwnersLastName(int page, String lastname) {
// int pageSize = 5;
// Pageable pageable = PageRequest.of(page - 1, pageSize);
// return owners.findByLastNameStartingWith(lastname, pageable);
// }
private Page<Owner> findPaginatedForOwnersName(int page, String lastname) {
int pageSize = 5;
Pageable pageable = PageRequest.of(page - 1, pageSize);
return owners.findByLastNameStartingWith(lastname, pageable);
return owners.findByNameContaining(lastname, pageable);
}
@GetMapping("/owners/{ownerId}/edit")

View file

@ -23,6 +23,7 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;
/**
@ -53,7 +54,10 @@ public interface OwnerRepository extends JpaRepository<Owner, Integer> {
* @return a Collection of matching {@link Owner}s (or an empty Collection if none
* found)
*/
Page<Owner> findByLastNameStartingWith(String lastName, Pageable pageable);
// Page<Owner> findByLastNameStartingWith(String lastName, Pageable pageable);
@Query("SELECT o FROM Owner o " + "WHERE LOWER(o.firstName) LIKE LOWER(CONCAT('%', :namePart, '%')) "
+ "OR LOWER(o.lastName) LIKE LOWER(CONCAT('%', :namePart, '%'))")
Page<Owner> findByNameContaining(@Param("namePart") String namePart, Pageable pageable);
/**
* Retrieve an {@link Owner} from the data store by id.

View file

@ -9,7 +9,7 @@
class="form-horizontal" id="search-owner-form">
<div class="form-group">
<div class="control-group" id="lastNameGroup">
<label class="col-sm-2 control-label">Last name </label>
<label class="col-sm-2 control-label">Search by Name</label>
<div class="col-sm-10">
<input class="form-control" th:field="*{lastName}" size="30"
maxlength="80" /> <span class="help-inline"><div
@ -21,8 +21,7 @@
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">Find
Owner</button>
<button type="submit" class="btn btn-primary">Search</button>
</div>
</div>