forked from springframeworkguru/sfg-pet-clinic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Visit Controller and mappings. closes springframeworkguru#57
- Loading branch information
1 parent
d4654dd
commit 464ad04
Showing
3 changed files
with
76 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...inic-web/src/main/java/guru/springframework/sfgpetclinic/controllers/VisitController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters