mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-22 15:25:49 +00:00
fix search with first name
This commit is contained in:
parent
696b31206d
commit
2fa9f2ff2f
3 changed files with 16 additions and 6 deletions
|
@ -83,15 +83,15 @@ class OwnerController {
|
||||||
public String processFindForm(Owner owner, BindingResult result, Map<String, Object> model) {
|
public String processFindForm(Owner owner, BindingResult result, Map<String, Object> 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.getFirstName() == null) {
|
||||||
owner.setLastName(""); // empty string signifies broadest possible search
|
owner.setFirstName(""); // empty string signifies broadest possible search
|
||||||
}
|
}
|
||||||
|
|
||||||
// find owners by last name
|
// find owners by last name
|
||||||
Collection<Owner> results = this.owners.findByLastName(owner.getLastName());
|
Collection<Owner> results = this.owners.findByFirstName(owner.getFirstName());
|
||||||
if (results.isEmpty()) {
|
if (results.isEmpty()) {
|
||||||
// no owners found
|
// no owners found
|
||||||
result.rejectValue("lastName", "notFound", "not found");
|
result.rejectValue("firstName", "notFound", "not found");
|
||||||
return "owners/findOwners";
|
return "owners/findOwners";
|
||||||
}
|
}
|
||||||
else if (results.size() == 1) {
|
else if (results.size() == 1) {
|
||||||
|
|
|
@ -61,4 +61,14 @@ public interface OwnerRepository extends Repository<Owner, Integer> {
|
||||||
*/
|
*/
|
||||||
void save(Owner owner);
|
void save(Owner owner);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Query("SELECT DISTINCT owner FROM Owner owner left join fetch owner.pets WHERE owner.lastName LIKE %:firstName%")
|
||||||
|
Collection<Owner> findByFirstName(String firstName);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,9 +9,9 @@
|
||||||
class="form-horizontal" id="search-owner-form">
|
class="form-horizontal" id="search-owner-form">
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<div class="control-group" id="lastNameGroup">
|
<div class="control-group" id="lastNameGroup">
|
||||||
<label class="col-sm-2 control-label">Last name </label>
|
<label class="col-sm-2 control-label">First name </label>
|
||||||
<div class="col-sm-10">
|
<div class="col-sm-10">
|
||||||
<input class="form-control" th:field="*{lastName}" size="30"
|
<input class="form-control" th:field="*{firstName}" size="30"
|
||||||
maxlength="80" /> <span class="help-inline"><div
|
maxlength="80" /> <span class="help-inline"><div
|
||||||
th:if="${#fields.hasAnyErrors()}">
|
th:if="${#fields.hasAnyErrors()}">
|
||||||
<p th:each="err : ${#fields.allErrors()}" th:text="${err}">Error</p>
|
<p th:each="err : ${#fields.allErrors()}" th:text="${err}">Error</p>
|
||||||
|
|
Loading…
Reference in a new issue