Feat: Search by firstName

인프런 기선님 강의 과제 1
This commit is contained in:
Hoyoung Jung 2020-03-02 18:26:59 +09:00
parent ac3e64208e
commit b9414e29d7
4 changed files with 9 additions and 6 deletions

View file

@ -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");

View file

@ -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);
}

View file

@ -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

View file

@ -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>