mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-21 06:45:49 +00:00
Feat: Search by firstName
인프런 기선님 강의 과제 1
This commit is contained in:
parent
ac3e64208e
commit
b9414e29d7
4 changed files with 9 additions and 6 deletions
|
@ -83,12 +83,12 @@ class OwnerController {
|
|||
public String processFindForm(Owner owner, BindingResult result, Map<String, Object> model) {
|
||||
|
||||
// allow parameterless GET request for /owners to return all records
|
||||
if (owner.getLastName() == null) {
|
||||
owner.setLastName(""); // empty string signifies broadest possible search
|
||||
if (owner.getFirstName() == null) {
|
||||
owner.setFirstName(""); // empty string signifies broadest possible search
|
||||
}
|
||||
|
||||
// find owners by last name
|
||||
Collection<Owner> results = this.owners.findByLastName(owner.getLastName());
|
||||
Collection<Owner> results = this.owners.findByFirstName(owner.getFirstName());
|
||||
if (results.isEmpty()) {
|
||||
// no owners found
|
||||
result.rejectValue("lastName", "notFound", "not found");
|
||||
|
|
|
@ -61,4 +61,7 @@ public interface OwnerRepository extends Repository<Owner, Integer> {
|
|||
*/
|
||||
void save(Owner owner);
|
||||
|
||||
@Query("SELECT DISTINCT owner FROM Owner owner left join fetch owner.pets WHERE owner.firstName LIKE :firstName%")
|
||||
@Transactional(readOnly = true)
|
||||
Collection<Owner> findByFirstName(String firstName);
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@ management.endpoints.web.exposure.include=*
|
|||
|
||||
# Logging
|
||||
logging.level.org.springframework=INFO
|
||||
# logging.level.org.springframework.web=DEBUG
|
||||
logging.level.org.springframework.web=DEBUG
|
||||
# logging.level.org.springframework.context.annotation=TRACE
|
||||
|
||||
# Maximum time static resources should be cached
|
||||
|
|
|
@ -9,9 +9,9 @@
|
|||
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">First name </label>
|
||||
<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
|
||||
th:if="${#fields.hasAnyErrors()}">
|
||||
<p th:each="err : ${#fields.allErrors()}" th:text="${err}">Error</p>
|
||||
|
|
Loading…
Reference in a new issue