mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-24 00:05:48 +00:00
fixing adding new visits
This commit is contained in:
parent
31c0604753
commit
c6afe22fd1
6 changed files with 25 additions and 41 deletions
10
readme.md
10
readme.md
|
@ -103,13 +103,3 @@ For pull requests, editor preferences are available in the [editor config](.edit
|
||||||
|
|
||||||
The Spring PetClinic sample application is released under version 2.0 of the [Apache License](https://www.apache.org/licenses/LICENSE-2.0).
|
The Spring PetClinic sample application is released under version 2.0 of the [Apache License](https://www.apache.org/licenses/LICENSE-2.0).
|
||||||
|
|
||||||
[spring-petclinic]: https://github.com/spring-projects/spring-petclinic
|
|
||||||
[spring-framework-petclinic]: https://github.com/spring-petclinic/spring-framework-petclinic
|
|
||||||
[spring-petclinic-angularjs]: https://github.com/spring-petclinic/spring-petclinic-angularjs
|
|
||||||
[javaconfig branch]: https://github.com/spring-petclinic/spring-framework-petclinic/tree/javaconfig
|
|
||||||
[spring-petclinic-angular]: https://github.com/spring-petclinic/spring-petclinic-angular
|
|
||||||
[spring-petclinic-microservices]: https://github.com/spring-petclinic/spring-petclinic-microservices
|
|
||||||
[spring-petclinic-reactjs]: https://github.com/spring-petclinic/spring-petclinic-reactjs
|
|
||||||
[spring-petclinic-graphql]: https://github.com/spring-petclinic/spring-petclinic-graphql
|
|
||||||
[spring-petclinic-kotlin]: https://github.com/spring-petclinic/spring-petclinic-kotlin
|
|
||||||
[spring-petclinic-rest]: https://github.com/spring-petclinic/spring-petclinic-rest
|
|
||||||
|
|
|
@ -90,12 +90,10 @@ public class IndexCMDRunner implements CommandLineRunner {
|
||||||
petRepository.save(new Pet("pet-4", "Jewel", "2010-03-07", "dog", "owner-3", new ArrayList()));
|
petRepository.save(new Pet("pet-4", "Jewel", "2010-03-07", "dog", "owner-3", new ArrayList()));
|
||||||
petRepository.save(new Pet("pet-5", "Iggy", "2010-11-30", "lizard", "owner-4", new ArrayList()));
|
petRepository.save(new Pet("pet-5", "Iggy", "2010-11-30", "lizard", "owner-4", new ArrayList()));
|
||||||
petRepository.save(new Pet("pet-6", "George", "2010-01-20", "snake", "owner-5", new ArrayList()));
|
petRepository.save(new Pet("pet-6", "George", "2010-01-20", "snake", "owner-5", new ArrayList()));
|
||||||
petRepository.save(new Pet("pet-7", "Samantha", "2012-09-04", "cat", "owner-6",
|
petRepository.save(new Pet("pet-7", "Samantha", "2012-09-04", "cat", "owner-6", Arrays.asList(
|
||||||
Arrays.asList(new Visit("visit-1", toMilliseconds("2013-01-01"), "rabies shot"),
|
new Visit("visit-1", "2013-01-01", "rabies shot"), new Visit("visit-4", "2013-01-04", "spayed"))));
|
||||||
new Visit("visit-4", toMilliseconds("2013-01-04"), "spayed"))));
|
petRepository.save(new Pet("pet-8", "Max", "2012-09-04", "cat", "owner-6", Arrays.asList(
|
||||||
petRepository.save(new Pet("pet-8", "Max", "2012-09-04", "cat", "owner-6",
|
new Visit("visit-2", "2013-01-02", "rabies shot"), new Visit("visit-3", "2013-01-03", "neutered"))));
|
||||||
Arrays.asList(new Visit("visit-2", toMilliseconds("2013-01-02"), "rabies shot"),
|
|
||||||
new Visit("visit-3", toMilliseconds("2013-01-03"), "neutered"))));
|
|
||||||
|
|
||||||
petRepository.save(new Pet("pet-9", "Lucky", "2011-08-06", "bird", "owner-7", new ArrayList()));
|
petRepository.save(new Pet("pet-9", "Lucky", "2011-08-06", "bird", "owner-7", new ArrayList()));
|
||||||
petRepository.save(new Pet("pet-10", "Mulligan", "2007-02-24", "dog", "owner-8", new ArrayList()));
|
petRepository.save(new Pet("pet-10", "Mulligan", "2007-02-24", "dog", "owner-8", new ArrayList()));
|
||||||
|
@ -104,10 +102,4 @@ public class IndexCMDRunner implements CommandLineRunner {
|
||||||
petRepository.save(new Pet("pet-13", "Sly", "2012-06-08", "cat", "owner-10", new ArrayList()));
|
petRepository.save(new Pet("pet-13", "Sly", "2012-06-08", "cat", "owner-10", new ArrayList()));
|
||||||
}
|
}
|
||||||
|
|
||||||
private long toMilliseconds(String targetDate) throws Exception {
|
|
||||||
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
|
|
||||||
Date d = f.parse(targetDate);
|
|
||||||
return d.getTime();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,8 +42,11 @@ class VisitController {
|
||||||
|
|
||||||
private final PetRepository petRepository;
|
private final PetRepository petRepository;
|
||||||
|
|
||||||
public VisitController(PetRepository petRepository) {
|
private final OwnerRepository ownerRepository;
|
||||||
|
|
||||||
|
public VisitController(PetRepository petRepository, OwnerRepository ownerRepository) {
|
||||||
this.petRepository = petRepository;
|
this.petRepository = petRepository;
|
||||||
|
this.ownerRepository = ownerRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
@InitBinder
|
@InitBinder
|
||||||
|
@ -62,6 +65,7 @@ class VisitController {
|
||||||
public Visit loadPetWithVisit(@PathVariable("petId") String petId, Map<String, Object> model) {
|
public Visit loadPetWithVisit(@PathVariable("petId") String petId, Map<String, Object> model) {
|
||||||
Pet pet = this.petRepository.findById(petId).get();
|
Pet pet = this.petRepository.findById(petId).get();
|
||||||
model.put("pet", pet);
|
model.put("pet", pet);
|
||||||
|
model.put("owner", this.ownerRepository.findById(pet.getOwnerId()).get());
|
||||||
Visit visit = new Visit();
|
Visit visit = new Visit();
|
||||||
return visit;
|
return visit;
|
||||||
}
|
}
|
||||||
|
@ -69,8 +73,6 @@ class VisitController {
|
||||||
// Spring MVC calls method loadPetWithVisit(...) before initNewVisitForm is called
|
// Spring MVC calls method loadPetWithVisit(...) before initNewVisitForm is called
|
||||||
@GetMapping("/owners/*/pets/{petId}/visits/new")
|
@GetMapping("/owners/*/pets/{petId}/visits/new")
|
||||||
public String initNewVisitForm(@PathVariable("petId") String petId, Map<String, Object> model) {
|
public String initNewVisitForm(@PathVariable("petId") String petId, Map<String, Object> model) {
|
||||||
Pet pet = this.petRepository.findById(petId).get();
|
|
||||||
model.put("pet", pet);
|
|
||||||
return "pets/createOrUpdateVisitForm";
|
return "pets/createOrUpdateVisitForm";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,6 +80,7 @@ class VisitController {
|
||||||
@PostMapping("/owners/{ownerId}/pets/{petId}/visits/new")
|
@PostMapping("/owners/{ownerId}/pets/{petId}/visits/new")
|
||||||
public String processNewVisitForm(@PathVariable("petId") String petId, @Valid Visit visit, BindingResult result) {
|
public String processNewVisitForm(@PathVariable("petId") String petId, @Valid Visit visit, BindingResult result) {
|
||||||
if (result.hasErrors()) {
|
if (result.hasErrors()) {
|
||||||
|
System.out.println(result.getAllErrors());
|
||||||
return "pets/createOrUpdateVisitForm";
|
return "pets/createOrUpdateVisitForm";
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
|
@ -15,14 +15,14 @@
|
||||||
*/
|
*/
|
||||||
package org.springframework.samples.petclinic.visit;
|
package org.springframework.samples.petclinic.visit;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import org.springframework.data.couchbase.core.mapping.id.GeneratedValue;
|
||||||
import java.time.temporal.TemporalField;
|
import org.springframework.data.couchbase.core.mapping.id.GenerationStrategy;
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
import javax.validation.constraints.NotEmpty;
|
import javax.validation.constraints.NotEmpty;
|
||||||
|
|
||||||
import org.springframework.format.annotation.DateTimeFormat;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple JavaBean domain object representing a visit.
|
* Simple JavaBean domain object representing a visit.
|
||||||
*
|
*
|
||||||
|
@ -31,15 +31,14 @@ import org.springframework.format.annotation.DateTimeFormat;
|
||||||
*/
|
*/
|
||||||
public class Visit {
|
public class Visit {
|
||||||
|
|
||||||
@NotEmpty
|
|
||||||
private String id;
|
private String id;
|
||||||
|
|
||||||
private Long visitDate;
|
private String visitDate;
|
||||||
|
|
||||||
@NotEmpty
|
@NotEmpty
|
||||||
private String description;
|
private String description;
|
||||||
|
|
||||||
public Visit(@NotEmpty String id, Long visitDate, @NotEmpty String description) {
|
public Visit(@NotEmpty String id, String visitDate, @NotEmpty String description) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.visitDate = visitDate;
|
this.visitDate = visitDate;
|
||||||
this.description = description;
|
this.description = description;
|
||||||
|
@ -49,7 +48,8 @@ public class Visit {
|
||||||
* Creates a new instance of Visit for the current date
|
* Creates a new instance of Visit for the current date
|
||||||
*/
|
*/
|
||||||
public Visit() {
|
public Visit() {
|
||||||
this.visitDate = new Date().getTime();
|
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
|
this.visitDate = f.format(new Date());
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getId() {
|
public String getId() {
|
||||||
|
@ -60,11 +60,11 @@ public class Visit {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Long getVisitDate() {
|
public String getVisitDate() {
|
||||||
return visitDate;
|
return visitDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setVisitDate(Long visitDate) {
|
public void setVisitDate(String visitDate) {
|
||||||
this.visitDate = visitDate;
|
this.visitDate = visitDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -61,7 +61,7 @@
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tr th:each="visit : ${pet.visits}">
|
<tr th:each="visit : ${pet.visits}">
|
||||||
<td th:text="${#dates.format(new java.util.Date(visit.visitDate), 'yyyy-MM-dd')}"></td>
|
<td th:text="${visit.visitDate}"></td>
|
||||||
<td th:text="${visit?.description}"></td>
|
<td th:text="${visit?.description}"></td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
|
|
|
@ -4,8 +4,7 @@
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
<h2>
|
<h2>
|
||||||
<th:block th:if="${visit['new']}">New </th:block>
|
|
||||||
Visit
|
|
||||||
</h2>
|
</h2>
|
||||||
|
|
||||||
<b>Pet</b>
|
<b>Pet</b>
|
||||||
|
@ -31,7 +30,7 @@
|
||||||
<form th:object="${visit}" class="form-horizontal" method="post">
|
<form th:object="${visit}" class="form-horizontal" method="post">
|
||||||
<div class="form-group has-feedback">
|
<div class="form-group has-feedback">
|
||||||
<input
|
<input
|
||||||
th:replace="~{fragments/inputField :: input ('Date', 'visitDate', 'text')}" />
|
th:replace="~{fragments/inputField :: input ('Date', 'visitDate', 'date')}" />
|
||||||
<input
|
<input
|
||||||
th:replace="~{fragments/inputField :: input ('Description', 'description', 'text')}" />
|
th:replace="~{fragments/inputField :: input ('Description', 'description', 'text')}" />
|
||||||
</div>
|
</div>
|
||||||
|
@ -51,8 +50,8 @@
|
||||||
<th>Date</th>
|
<th>Date</th>
|
||||||
<th>Description</th>
|
<th>Description</th>
|
||||||
</tr>
|
</tr>
|
||||||
<tr th:if="${!visit['new']}" th:each="visit : ${pet.visits}">
|
<tr th:each="visit : ${pet.visits}">
|
||||||
<td th:text="${#dates.format(new java.util.Date(visit.visitDate), 'yyyy-MM-dd')}"></td>
|
<td th:text="${visit.visitDate}"></td>
|
||||||
<td th:text=" ${visit.description}"></td>
|
<td th:text=" ${visit.description}"></td>
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
|
|
Loading…
Reference in a new issue