Skip to content

Commit

Permalink
display Owner Record closes springframeworkguru#53
Browse files Browse the repository at this point in the history
  • Loading branch information
springframeworkguru committed Sep 22, 2018
1 parent 8f0670d commit 888d2a8
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import guru.springframework.sfgpetclinic.services.OwnerService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
* Created by jt on 7/22/18.
Expand Down Expand Up @@ -31,4 +34,11 @@ public String findOwners(){
return "notimplemented";
}

@GetMapping("/{ownerId}")
public ModelAndView showOwner(@PathVariable("ownerId") Long ownerId) {
ModelAndView mav = new ModelAndView("owners/ownerDetails");
mav.addObject(ownerService.findById(ownerId));
return mav;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<html xmlns:th="http://www.thymeleaf.org"
th:replace="~{fragments/layout :: layout (~{::body},'owners')}">

<body>

<h2>Owner</h2>
<form th:object="${owner}" class="form-horizontal" id="add-owner-form" method="post">
<div class="form-group has-feedback">
<input
th:replace="~{fragments/inputField :: input ('First Name', 'firstName', 'text')}" />
<input
th:replace="~{fragments/inputField :: input ('Last Name', 'lastName', 'text')}" />
<input
th:replace="~{fragments/inputField :: input ('Address', 'address', 'text')}" />
<input
th:replace="~{fragments/inputField :: input ('City', 'city', 'text')}" />
<input
th:replace="~{fragments/inputField :: input ('Telephone', 'telephone', 'text')}" />
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button
th:with="text=${owner['new']} ? 'Add Owner' : 'Update Owner'"
class="btn btn-default" type="submit" th:text="${text}">Add
Owner</button>
</div>
</div>
</form>
</body>
</html>
35 changes: 35 additions & 0 deletions pet-clinic-web/src/main/resources/templates/owners/findOwners.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<html xmlns:th="http://www.thymeleaf.org"
th:replace="~{fragments/layout :: layout (~{::body},'owners')}">

<body>

<h2>Find Owners</h2>

<form th:object="${owner}" th:action="@{/owners}" method="get"
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>
<div class="col-sm-10">
<input class="form-control" th:field="*{lastName}" size="30"
maxlength="80" /> <span class="help-inline"><div
th:if="${#fields.hasAnyErrors()}">
<p th:each="err : ${#fields.allErrors()}" th:text="${err}">Error</p>
</div></span>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">Find
Owner</button>
</div>
</div>

</form>

<br />
<a class="btn btn-default" th:href="@{/owners/new}">Add Owner</a>

</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<!DOCTYPE html>

<html xmlns:th="http://www.thymeleaf.org"
th:replace="~{fragments/layout :: layout (~{::body},'owners')}">

<body>


<h2>Owner Information</h2>


<table class="table table-striped" th:object="${owner}">
<tr>
<th>Name</th>
<td><b th:text="*{firstName + ' ' + lastName}"></b></td>
</tr>
<tr>
<th>Address</th>
<td th:text="*{address}" /></td>
</tr>
<tr>
<th>City</th>
<td th:text="*{city}" /></td>
</tr>
<tr>
<th>Telephone</th>
<td th:text="*{telephone}" /></td>
</tr>
</table>

<a th:href="@{{id}/edit(id=${owner.id})}" class="btn btn-default">Edit
Owner</a>
<a th:href="@{{id}/pets/new(id=${owner.id})}" class="btn btn-default">Add
New Pet</a>

<br />
<br />
<br />
<h2>Pets and Visits</h2>

<table class="table table-striped">

<tr th:each="pet : ${owner.pets}">
<td valign="top">
<dl class="dl-horizontal">
<dt>Name</dt>
<dd th:text="${pet.name}" /></dd>
<dt>Birth Date</dt>
<dd
th:text="${#temporals.format(pet.birthDate, 'yyyy-MM-dd')}" /></dd>
<dt>Type</dt>
<dd th:text="${pet.type}" /></dd>
</dl>
</td>
<td valign="top">
<table class="table-condensed">
<thead>
<tr>
<th>Visit Date</th>
<th>Description</th>
</tr>
</thead>
<tr th:each="visit : ${pet.visits}">
<td th:text="${#temporals.format(visit.date, 'yyyy-MM-dd')}"></td>
<td th:text="${visit?.description}"></td>
</tr>
<tr>
<td><a
th:href="@{{ownerId}/pets/{petId}/edit(ownerId=${owner.id},petId=${pet.id})}">Edit
Pet</a></td>
<td><a
th:href="@{{ownerId}/pets/{petId}/visits/new(ownerId=${owner.id},petId=${pet.id})}">Add
Visit</a></td>
</tr>
</table>
</td>
</tr>

</table>

</body>

</html>
33 changes: 33 additions & 0 deletions pet-clinic-web/src/main/resources/templates/owners/ownersList.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>

<html xmlns:th="http://www.thymeleaf.org" th:replace="~{fragments/layout :: layout (~{::body},'owners')}">

<body>

<h2>Owners</h2>

<table id="vets" class="table table-striped">
<thead>
<tr>
<th style="width: 150px;">Name</th>
<th style="width: 200px;">Address</th>
<th>City</th>
<th style="width: 120px">Telephone</th>
<th>Pets</th>
</tr>
</thead>
<tbody>
<tr th:each="owner : ${selections}">
<td>
<a th:href="@{/owners/__${owner.id}__}" th:text="${owner.firstName + ' ' + owner.lastName}"/></a>
</td>
<td th:text="${owner.address}"/>
<td th:text="${owner.city}"/>
<td th:text="${owner.telephone}"/>
<td><span th:each="pet : ${owner.pets}" th:text="${pet.name} "/></td>
</tr>
</tbody>
</table>

</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
import java.util.HashSet;
import java.util.Set;

import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.*;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
Expand Down Expand Up @@ -73,4 +74,14 @@ void findOwners() throws Exception {

verifyZeroInteractions(ownerService);
}

@Test
void displayOwner() throws Exception {
when(ownerService.findById(anyLong())).thenReturn(Owner.builder().id(1l).build());

mockMvc.perform(get("/owners/123"))
.andExpect(status().isOk())
.andExpect(view().name("owners/ownerDetails"))
.andExpect(model().attribute("owner", hasProperty("id", is(1l))));
}
}

0 comments on commit 888d2a8

Please sign in to comment.