From aaaddf196f3dbebf9666b7ddbb3682b1b19dc1de Mon Sep 17 00:00:00 2001 From: Vitaliy Fedoriv Date: Tue, 1 Nov 2016 21:15:53 +0200 Subject: [PATCH] Add PetTypeRestController --- .../petclinic/rest/PetTypeRestController.java | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 src/main/java/org/springframework/samples/petclinic/rest/PetTypeRestController.java diff --git a/src/main/java/org/springframework/samples/petclinic/rest/PetTypeRestController.java b/src/main/java/org/springframework/samples/petclinic/rest/PetTypeRestController.java new file mode 100644 index 000000000..1ea408c1f --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/rest/PetTypeRestController.java @@ -0,0 +1,86 @@ +package org.springframework.samples.petclinic.rest; + +import java.util.ArrayList; +import java.util.Collection; + +import javax.validation.Valid; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.samples.petclinic.model.PetType; +import org.springframework.samples.petclinic.service.ClinicServiceExt; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.util.UriComponentsBuilder; + +@RestController +@RequestMapping("api/pettypes") +public class PetTypeRestController { + + @Autowired + private ClinicServiceExt clinicService; + + @RequestMapping(value = "/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + public ResponseEntity> getAllPetTypes(){ + Collection petTypes = new ArrayList(); + petTypes.addAll(this.clinicService.findAllPetTypes()); + if (petTypes.isEmpty()){ + return new ResponseEntity>(HttpStatus.NOT_FOUND); + } + return new ResponseEntity>(petTypes, HttpStatus.OK); + } + + @RequestMapping(value = "/{petTypeId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + public ResponseEntity getPetType(@PathVariable("petTypeId") int petTypeId){ + PetType petType = this.clinicService.findPetTypeById(petTypeId); + if(petType == null){ + return new ResponseEntity(HttpStatus.NOT_FOUND); + } + return new ResponseEntity(petType, HttpStatus.OK); + } + + + @RequestMapping(value = "/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + public ResponseEntity addPetType(@RequestBody @Valid PetType petType, BindingResult bindingResult, UriComponentsBuilder ucBuilder){ + if(bindingResult.hasErrors() || (petType == null)){ + return new ResponseEntity(HttpStatus.BAD_REQUEST); + } + this.clinicService.savePetType(petType); + HttpHeaders headers = new HttpHeaders(); + headers.setLocation(ucBuilder.path("/api/pettypes/{id}").buildAndExpand(petType.getId()).toUri()); + return new ResponseEntity(headers, HttpStatus.CREATED); + } + + @RequestMapping(value = "/{petTypeId}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + public ResponseEntity updatePetType(@PathVariable("petTypeId") int petTypeId, @RequestBody @Valid PetType petType, BindingResult bindingResult){ + if(bindingResult.hasErrors() || (petType == null)){ + return new ResponseEntity(HttpStatus.BAD_REQUEST); + } + PetType currentPetType = this.clinicService.findPetTypeById(petTypeId); + if(currentPetType == null){ + return new ResponseEntity(HttpStatus.NOT_FOUND); + } + currentPetType.setName(petType.getName()); + this.clinicService.savePetType(currentPetType); + return new ResponseEntity(currentPetType, HttpStatus.NO_CONTENT); + } + + @RequestMapping(value = "/{petTypeId}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + public ResponseEntity deletePetType(@PathVariable("petTypeId") int petTypeId){ + PetType petType = this.clinicService.findPetTypeById(petTypeId); + if(petType == null){ + return new ResponseEntity(HttpStatus.NOT_FOUND); + } + this.clinicService.deletePetType(petType); + // TODO delete error - FK etc. + return new ResponseEntity(HttpStatus.NO_CONTENT); + } + +}