Skip to content

Commit

Permalink
Enhance_owner_search_functionality_1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
APerumalRaj committed Jan 22, 2025
1 parent 6148ddd commit 3edb24d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public String processFindForm(@RequestParam(defaultValue = "1") int page, Owner
}

// 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");
Expand All @@ -124,10 +124,15 @@ private String addPaginationModel(int page, Model model, Page<Owner> paginated)
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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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;

/**
Expand Down Expand Up @@ -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.
Expand Down
5 changes: 2 additions & 3 deletions src/main/resources/templates/owners/findOwners.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ <h2>Find Owners</h2>
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
Expand All @@ -21,8 +21,7 @@ <h2>Find Owners</h2>
</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>

Expand Down

0 comments on commit 3edb24d

Please sign in to comment.