Skip to content

Commit

Permalink
#56
Browse files Browse the repository at this point in the history
  • Loading branch information
Priyanshu-git committed Jun 11, 2021
1 parent a5c941d commit 65dd52c
Showing 1 changed file with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package spring.framework.petclinic.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import spring.framework.petclinic.model.Owner;
import spring.framework.petclinic.model.PetType;
import spring.framework.petclinic.services.OwnerService;
import spring.framework.petclinic.services.PetService;
import spring.framework.petclinic.services.PetTypeService;

import java.util.Collection;

@Controller
@RequestMapping("/owners/{ownerId}")
public class PetController {

private static final String VIEWS_PETS_CREATE_OR_UPDATE_FORM = "pets/createOrUpdatePetForm";

private final PetService petService;
private final OwnerService ownerService;
private final PetTypeService petTypeService;

public PetController(PetService petService, OwnerService ownerService, PetTypeService petTypeService) {
this.petService = petService;
this.ownerService = ownerService;
this.petTypeService = petTypeService;
}

@ModelAttribute("types")
public Collection<PetType> populatePetTypes() {
return petTypeService.findAll();
}

@ModelAttribute("owner")
public Owner findOwner(@PathVariable("ownerId") Long ownerId) {
return ownerService.findById(ownerId);
}

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

0 comments on commit 65dd52c

Please sign in to comment.