diff --git a/src/main/java/org/springframework/samples/petclinic/rest/PetRestController.java b/src/main/java/org/springframework/samples/petclinic/rest/PetRestController.java index 875956b0afa3789448fb12ac6ba6fea718e8c46b..fa077e91367a16bee024e4b1edefcc0d2ccf05be 100644 --- a/src/main/java/org/springframework/samples/petclinic/rest/PetRestController.java +++ b/src/main/java/org/springframework/samples/petclinic/rest/PetRestController.java @@ -41,7 +41,6 @@ import org.springframework.web.util.UriComponentsBuilder; /** * @author Vitaliy Fedoriv - * */ @RestController @@ -49,83 +48,83 @@ import org.springframework.web.util.UriComponentsBuilder; @RequestMapping("api/pets") public class PetRestController { - @Autowired - private ClinicService clinicService; + @Autowired + private ClinicService clinicService; - @PreAuthorize( "hasRole(@roles.OWNER_ADMIN)" ) - @RequestMapping(value = "/{petId}", method = RequestMethod.GET, produces = "application/json") - public ResponseEntity<Pet> getPet(@PathVariable("petId") int petId){ - Pet pet = this.clinicService.findPetById(petId); - if(pet == null){ - return new ResponseEntity<Pet>(HttpStatus.NOT_FOUND); - } - return new ResponseEntity<Pet>(pet, HttpStatus.OK); - } + @PreAuthorize("hasRole(@roles.OWNER_ADMIN)") + @RequestMapping(value = "/{petId}", method = RequestMethod.GET, produces = "application/json") + public ResponseEntity<Pet> getPet(@PathVariable("petId") int petId) { + Pet pet = this.clinicService.findPetById(petId); + if (pet == null) { + return new ResponseEntity<Pet>(HttpStatus.NOT_FOUND); + } + return new ResponseEntity<Pet>(pet, HttpStatus.OK); + } - @PreAuthorize( "hasRole(@roles.OWNER_ADMIN)" ) - @RequestMapping(value = "", method = RequestMethod.GET, produces = "application/json") - public ResponseEntity<Collection<Pet>> getPets(){ - Collection<Pet> pets = this.clinicService.findAllPets(); - if(pets.isEmpty()){ - return new ResponseEntity<Collection<Pet>>(HttpStatus.NOT_FOUND); - } - return new ResponseEntity<Collection<Pet>>(pets, HttpStatus.OK); - } + @PreAuthorize("hasRole(@roles.OWNER_ADMIN)") + @RequestMapping(value = "", method = RequestMethod.GET, produces = "application/json") + public ResponseEntity<Collection<Pet>> getPets() { + Collection<Pet> pets = this.clinicService.findAllPets(); + if (pets.isEmpty()) { + return new ResponseEntity<Collection<Pet>>(HttpStatus.NOT_FOUND); + } + return new ResponseEntity<Collection<Pet>>(pets, HttpStatus.OK); + } - @PreAuthorize( "hasRole(@roles.OWNER_ADMIN)" ) - @RequestMapping(value = "/pettypes", method = RequestMethod.GET, produces = "application/json") - public ResponseEntity<Collection<PetType>> getPetTypes(){ - return new ResponseEntity<Collection<PetType>>(this.clinicService.findPetTypes(), HttpStatus.OK); - } + @PreAuthorize("hasRole(@roles.OWNER_ADMIN)") + @RequestMapping(value = "/pettypes", method = RequestMethod.GET, produces = "application/json") + public ResponseEntity<Collection<PetType>> getPetTypes() { + return new ResponseEntity<Collection<PetType>>(this.clinicService.findPetTypes(), HttpStatus.OK); + } - @PreAuthorize( "hasRole(@roles.OWNER_ADMIN)" ) - @RequestMapping(value = "", method = RequestMethod.POST, produces = "application/json") - public ResponseEntity<Pet> addPet(@RequestBody @Valid Pet pet, BindingResult bindingResult, UriComponentsBuilder ucBuilder){ - BindingErrorsResponse errors = new BindingErrorsResponse(); - HttpHeaders headers = new HttpHeaders(); - if(bindingResult.hasErrors() || (pet == null)){ - errors.addAllErrors(bindingResult); - headers.add("errors", errors.toJSON()); - return new ResponseEntity<Pet>(headers, HttpStatus.BAD_REQUEST); - } - this.clinicService.savePet(pet); - headers.setLocation(ucBuilder.path("/api/pets/{id}").buildAndExpand(pet.getId()).toUri()); - return new ResponseEntity<Pet>(pet, headers, HttpStatus.CREATED); - } + @PreAuthorize("hasRole(@roles.OWNER_ADMIN)") + @RequestMapping(value = "", method = RequestMethod.POST, produces = "application/json") + public ResponseEntity<Pet> addPet(@RequestBody @Valid Pet pet, BindingResult bindingResult, UriComponentsBuilder ucBuilder) { + BindingErrorsResponse errors = new BindingErrorsResponse(); + HttpHeaders headers = new HttpHeaders(); + if (bindingResult.hasErrors() || (pet == null)) { + errors.addAllErrors(bindingResult); + headers.add("errors", errors.toJSON()); + return new ResponseEntity<Pet>(headers, HttpStatus.BAD_REQUEST); + } + this.clinicService.savePet(pet); + headers.setLocation(ucBuilder.path("/api/pets/{id}").buildAndExpand(pet.getId()).toUri()); + return new ResponseEntity<Pet>(pet, headers, HttpStatus.CREATED); + } - @PreAuthorize( "hasRole(@roles.OWNER_ADMIN)" ) - @RequestMapping(value = "/{petId}", method = RequestMethod.PUT, produces = "application/json") - public ResponseEntity<Pet> updatePet(@PathVariable("petId") int petId, @RequestBody @Valid Pet pet, BindingResult bindingResult){ - BindingErrorsResponse errors = new BindingErrorsResponse(); - HttpHeaders headers = new HttpHeaders(); - if(bindingResult.hasErrors() || (pet == null)){ - errors.addAllErrors(bindingResult); - headers.add("errors", errors.toJSON()); - return new ResponseEntity<Pet>(headers, HttpStatus.BAD_REQUEST); - } - Pet currentPet = this.clinicService.findPetById(petId); - if(currentPet == null){ - return new ResponseEntity<Pet>(HttpStatus.NOT_FOUND); - } - currentPet.setBirthDate(pet.getBirthDate()); - currentPet.setName(pet.getName()); - currentPet.setType(pet.getType()); - currentPet.setOwner(pet.getOwner()); - this.clinicService.savePet(currentPet); - return new ResponseEntity<Pet>(currentPet, HttpStatus.NO_CONTENT); - } + @PreAuthorize("hasRole(@roles.OWNER_ADMIN)") + @RequestMapping(value = "/{petId}", method = RequestMethod.PUT, produces = "application/json") + public ResponseEntity<Pet> updatePet(@PathVariable("petId") int petId, @RequestBody @Valid Pet pet, BindingResult bindingResult) { + BindingErrorsResponse errors = new BindingErrorsResponse(); + HttpHeaders headers = new HttpHeaders(); + if (bindingResult.hasErrors() || (pet == null)) { + errors.addAllErrors(bindingResult); + headers.add("errors", errors.toJSON()); + return new ResponseEntity<Pet>(headers, HttpStatus.BAD_REQUEST); + } + Pet currentPet = this.clinicService.findPetById(petId); + if (currentPet == null) { + return new ResponseEntity<Pet>(HttpStatus.NOT_FOUND); + } + currentPet.setBirthDate(pet.getBirthDate()); + currentPet.setName(pet.getName()); + currentPet.setType(pet.getType()); + currentPet.setOwner(pet.getOwner()); + this.clinicService.savePet(currentPet); + return new ResponseEntity<Pet>(currentPet, HttpStatus.NO_CONTENT); + } - @PreAuthorize( "hasRole(@roles.OWNER_ADMIN)" ) - @RequestMapping(value = "/{petId}", method = RequestMethod.DELETE, produces = "application/json") - @Transactional - public ResponseEntity<Void> deletePet(@PathVariable("petId") int petId){ - Pet pet = this.clinicService.findPetById(petId); - if(pet == null){ - return new ResponseEntity<Void>(HttpStatus.NOT_FOUND); - } - this.clinicService.deletePet(pet); - return new ResponseEntity<Void>(HttpStatus.NO_CONTENT); - } + @PreAuthorize("hasRole(@roles.OWNER_ADMIN)") + @RequestMapping(value = "/{petId}", method = RequestMethod.DELETE, produces = "application/json") + @Transactional + public ResponseEntity<Void> deletePet(@PathVariable("petId") int petId) { + Pet pet = this.clinicService.findPetById(petId); + if (pet == null) { + return new ResponseEntity<Void>(HttpStatus.NOT_FOUND); + } + this.clinicService.deletePet(pet); + return new ResponseEntity<Void>(HttpStatus.NO_CONTENT); + } }