Skip to content

Commit

Permalink
Added Visit Controller and mappings. closes springframeworkguru#57
Browse files Browse the repository at this point in the history
  • Loading branch information
springframeworkguru committed Sep 27, 2018
1 parent d4654dd commit 464ad04
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ public Pet(Long id, String name, PetType petType, Owner owner, LocalDate birthDa
this.petType = petType;
this.owner = owner;
this.birthDate = birthDate;
this.visits = visits;

if (visits == null || visits.size() > 0 ) {
this.visits = visits;
}
}

@Column(name = "name")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package guru.springframework.sfgpetclinic.controllers;

import guru.springframework.sfgpetclinic.model.Pet;
import guru.springframework.sfgpetclinic.model.Visit;
import guru.springframework.sfgpetclinic.services.PetService;
import guru.springframework.sfgpetclinic.services.VisitService;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
* Created by jt on 2018-09-27.
*/
@Controller
public class VisitController {

private final VisitService visitService;
private final PetService petService;

public VisitController(VisitService visitService, PetService petService) {
this.visitService = visitService;
this.petService = petService;
}

@InitBinder
public void setAllowedFields(WebDataBinder dataBinder) {
dataBinder.setDisallowedFields("id");
}

/**
* Called before each and every @RequestMapping annotated method.
* 2 goals:
* - Make sure we always have fresh data
* - Since we do not use the session scope, make sure that Pet object always has an id
* (Even though id is not part of the form fields)
*
* @param petId
* @return Pet
*/
@ModelAttribute("visit")
public Visit loadPetWithVisit(@PathVariable("petId") Long petId, Map<String, Object> model) {
Pet pet = petService.findById(petId);
model.put("pet", pet);
Visit visit = new Visit();
pet.getVisits().add(visit);
visit.setPet(pet);
return visit;
}

// Spring MVC calls method loadPetWithVisit(...) before initNewVisitForm is called
@GetMapping("/owners/*/pets/{petId}/visits/new")
public String initNewVisitForm(@PathVariable("petId") Long petId, Map<String, Object> model) {
return "pets/createOrUpdateVisitForm";
}

// Spring MVC calls method loadPetWithVisit(...) before processNewVisitForm is called
@PostMapping("/owners/{ownerId}/pets/{petId}/visits/new")
public String processNewVisitForm(@Valid Visit visit, BindingResult result) {
if (result.hasErrors()) {
return "pets/createOrUpdateVisitForm";
} else {
visitService.save(visit);

return "redirect:/owners/{ownerId}";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ <h2>
<td th:text="${pet.name}" /></td>
<td
th:text="${#temporals.format(pet.birthDate, 'yyyy-MM-dd')}" /></td>
<td th:text="${pet.type}" /></td>
<td th:text="${pet.petType}" /></td>
<td
th:text="${pet.owner?.firstName + ' ' + pet.owner?.lastName}" /></td>
</tr>
Expand Down

0 comments on commit 464ad04

Please sign in to comment.