mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-17 21:15:50 +00:00
update rest controllers
This commit is contained in:
parent
d230db22b9
commit
bedc97c048
5 changed files with 258 additions and 6 deletions
|
@ -26,7 +26,8 @@ import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.samples.petclinic.model.Owner;
|
import org.springframework.samples.petclinic.model.Owner;
|
||||||
import org.springframework.samples.petclinic.service.ClinicService;
|
import org.springframework.samples.petclinic.model.Pet;
|
||||||
|
import org.springframework.samples.petclinic.service.ClinicServiceExt;
|
||||||
import org.springframework.validation.BindingResult;
|
import org.springframework.validation.BindingResult;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
@ -46,7 +47,7 @@ import org.springframework.web.util.UriComponentsBuilder;
|
||||||
public class OwnerRestController {
|
public class OwnerRestController {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ClinicService clinicService;
|
private ClinicServiceExt clinicService;
|
||||||
|
|
||||||
@RequestMapping(value = "/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
@RequestMapping(value = "/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||||
public ResponseEntity<Collection<Owner>> getOwnersList(@RequestParam("lastName") String ownerLastName){
|
public ResponseEntity<Collection<Owner>> getOwnersList(@RequestParam("lastName") String ownerLastName){
|
||||||
|
@ -97,6 +98,17 @@ public class OwnerRestController {
|
||||||
this.clinicService.saveOwner(currentOwner);
|
this.clinicService.saveOwner(currentOwner);
|
||||||
return new ResponseEntity<Owner>(currentOwner, HttpStatus.NO_CONTENT);
|
return new ResponseEntity<Owner>(currentOwner, HttpStatus.NO_CONTENT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value = "/{ownerId}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||||
|
public ResponseEntity<Void> deleteOwner(@PathVariable("ownerId") int ownerId){
|
||||||
|
Owner owner = this.clinicService.findOwnerById(ownerId);
|
||||||
|
if(owner == null){
|
||||||
|
return new ResponseEntity<Void>(HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
this.clinicService.deleteOwner(owner);
|
||||||
|
// TODO delete error - FK etc.
|
||||||
|
return new ResponseEntity<Void>(HttpStatus.NO_CONTENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,7 @@ import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.samples.petclinic.model.Pet;
|
import org.springframework.samples.petclinic.model.Pet;
|
||||||
import org.springframework.samples.petclinic.model.PetType;
|
import org.springframework.samples.petclinic.model.PetType;
|
||||||
import org.springframework.samples.petclinic.service.ClinicService;
|
import org.springframework.samples.petclinic.service.ClinicServiceExt;
|
||||||
import org.springframework.validation.BindingResult;
|
import org.springframework.validation.BindingResult;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
@ -46,7 +46,7 @@ import org.springframework.web.util.UriComponentsBuilder;
|
||||||
public class PetRestController {
|
public class PetRestController {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ClinicService clinicService;
|
private ClinicServiceExt clinicService;
|
||||||
|
|
||||||
@RequestMapping(value = "/{petId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
@RequestMapping(value = "/{petId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||||
public ResponseEntity<Pet> getPet(@PathVariable("petId") int petId){
|
public ResponseEntity<Pet> getPet(@PathVariable("petId") int petId){
|
||||||
|
@ -89,5 +89,16 @@ public class PetRestController {
|
||||||
return new ResponseEntity<Pet>(currentPet, HttpStatus.NO_CONTENT);
|
return new ResponseEntity<Pet>(currentPet, HttpStatus.NO_CONTENT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value = "/{petId}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||||
|
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);
|
||||||
|
// TODO delete error - FK etc.
|
||||||
|
return new ResponseEntity<Void>(HttpStatus.NO_CONTENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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.Specialty;
|
||||||
|
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/specialties")
|
||||||
|
public class SpecialtyRestController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ClinicServiceExt clinicService;
|
||||||
|
|
||||||
|
@RequestMapping(value = "/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||||
|
public ResponseEntity<Collection<Specialty>> getAllSpecialtys(){
|
||||||
|
Collection<Specialty> specialties = new ArrayList<Specialty>();
|
||||||
|
specialties.addAll(this.clinicService.findAllSpecialties());
|
||||||
|
if (specialties.isEmpty()){
|
||||||
|
return new ResponseEntity<Collection<Specialty>>(HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
return new ResponseEntity<Collection<Specialty>>(specialties, HttpStatus.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value = "/{specialtyId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||||
|
public ResponseEntity<Specialty> getSpecialty(@PathVariable("specialtyId") int specialtyId){
|
||||||
|
Specialty specialty = this.clinicService.findSpecialtyById(specialtyId);
|
||||||
|
if(specialty == null){
|
||||||
|
return new ResponseEntity<Specialty>(HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
return new ResponseEntity<Specialty>(specialty, HttpStatus.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@RequestMapping(value = "/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||||
|
public ResponseEntity<Void> addSpecialty(@RequestBody @Valid Specialty specialty, BindingResult bindingResult, UriComponentsBuilder ucBuilder){
|
||||||
|
if(bindingResult.hasErrors() || (specialty == null)){
|
||||||
|
return new ResponseEntity<Void>(HttpStatus.BAD_REQUEST);
|
||||||
|
}
|
||||||
|
this.clinicService.saveSpecialty(specialty);
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.setLocation(ucBuilder.path("/api/specialtys/{id}").buildAndExpand(specialty.getId()).toUri());
|
||||||
|
return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value = "/{specialtyId}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||||
|
public ResponseEntity<Specialty> updateSpecialty(@PathVariable("specialtyId") int specialtyId, @RequestBody @Valid Specialty specialty, BindingResult bindingResult){
|
||||||
|
if(bindingResult.hasErrors() || (specialty == null)){
|
||||||
|
return new ResponseEntity<Specialty>(HttpStatus.BAD_REQUEST);
|
||||||
|
}
|
||||||
|
Specialty currentSpecialty = this.clinicService.findSpecialtyById(specialtyId);
|
||||||
|
if(currentSpecialty == null){
|
||||||
|
return new ResponseEntity<Specialty>(HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
currentSpecialty.setName(specialty.getName());
|
||||||
|
this.clinicService.saveSpecialty(currentSpecialty);
|
||||||
|
return new ResponseEntity<Specialty>(currentSpecialty, HttpStatus.NO_CONTENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value = "/{specialtyId}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||||
|
public ResponseEntity<Void> deleteSpecialty(@PathVariable("specialtyId") int specialtyId){
|
||||||
|
Specialty specialty = this.clinicService.findSpecialtyById(specialtyId);
|
||||||
|
if(specialty == null){
|
||||||
|
return new ResponseEntity<Void>(HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
this.clinicService.deleteSpecialty(specialty);
|
||||||
|
// TODO delete error - FK etc.
|
||||||
|
return new ResponseEntity<Void>(HttpStatus.NO_CONTENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -18,15 +18,22 @@ package org.springframework.samples.petclinic.rest;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import javax.validation.Valid;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.samples.petclinic.model.Vet;
|
import org.springframework.samples.petclinic.model.Vet;
|
||||||
import org.springframework.samples.petclinic.service.ClinicService;
|
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.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.util.UriComponentsBuilder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Vitaliy Fedoriv
|
* @author Vitaliy Fedoriv
|
||||||
|
@ -38,7 +45,7 @@ import org.springframework.web.bind.annotation.RestController;
|
||||||
public class VetRestController {
|
public class VetRestController {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ClinicService clinicService;
|
private ClinicServiceExt clinicService;
|
||||||
|
|
||||||
@RequestMapping(value = "/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
@RequestMapping(value = "/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||||
public ResponseEntity<Collection<Vet>> getAllVets(){
|
public ResponseEntity<Collection<Vet>> getAllVets(){
|
||||||
|
@ -50,5 +57,53 @@ public class VetRestController {
|
||||||
return new ResponseEntity<Collection<Vet>>(vets, HttpStatus.OK);
|
return new ResponseEntity<Collection<Vet>>(vets, HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value = "/{vetId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||||
|
public ResponseEntity<Vet> getVet(@PathVariable("vetId") int vetId){
|
||||||
|
Vet vet = this.clinicService.findVetById(vetId);
|
||||||
|
if(vet == null){
|
||||||
|
return new ResponseEntity<Vet>(HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
return new ResponseEntity<Vet>(vet, HttpStatus.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@RequestMapping(value = "/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||||
|
public ResponseEntity<Void> addVet(@RequestBody @Valid Vet vet, BindingResult bindingResult, UriComponentsBuilder ucBuilder){
|
||||||
|
if(bindingResult.hasErrors() || (vet == null)){
|
||||||
|
return new ResponseEntity<Void>(HttpStatus.BAD_REQUEST);
|
||||||
|
}
|
||||||
|
this.clinicService.saveVet(vet);
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.setLocation(ucBuilder.path("/api/vets/{id}").buildAndExpand(vet.getId()).toUri());
|
||||||
|
return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value = "/{vetId}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||||
|
public ResponseEntity<Vet> updateVet(@PathVariable("vetId") int vetId, @RequestBody @Valid Vet vet, BindingResult bindingResult){
|
||||||
|
if(bindingResult.hasErrors() || (vet == null)){
|
||||||
|
return new ResponseEntity<Vet>(HttpStatus.BAD_REQUEST);
|
||||||
|
}
|
||||||
|
Vet currentVet = this.clinicService.findVetById(vetId);
|
||||||
|
if(currentVet == null){
|
||||||
|
return new ResponseEntity<Vet>(HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
currentVet.setFirstName(vet.getFirstName());
|
||||||
|
currentVet.setLastName(vet.getLastName());
|
||||||
|
this.clinicService.saveVet(currentVet);
|
||||||
|
return new ResponseEntity<Vet>(currentVet, HttpStatus.NO_CONTENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value = "/{vetId}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||||
|
public ResponseEntity<Void> deleteVet(@PathVariable("vetId") int vetId){
|
||||||
|
Vet vet = this.clinicService.findVetById(vetId);
|
||||||
|
if(vet == null){
|
||||||
|
return new ResponseEntity<Void>(HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
this.clinicService.deleteVet(vet);
|
||||||
|
// TODO delete error - FK etc.
|
||||||
|
return new ResponseEntity<Void>(HttpStatus.NO_CONTENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,88 @@
|
||||||
|
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.Visit;
|
||||||
|
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/visits")
|
||||||
|
public class VisitRestController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ClinicServiceExt clinicService;
|
||||||
|
|
||||||
|
@RequestMapping(value = "/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||||
|
public ResponseEntity<Collection<Visit>> getAllVisits(){
|
||||||
|
Collection<Visit> visits = new ArrayList<Visit>();
|
||||||
|
visits.addAll(this.clinicService.findAllVisits());
|
||||||
|
if (visits.isEmpty()){
|
||||||
|
return new ResponseEntity<Collection<Visit>>(HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
return new ResponseEntity<Collection<Visit>>(visits, HttpStatus.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value = "/{visitId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||||
|
public ResponseEntity<Visit> getVisit(@PathVariable("visitId") int visitId){
|
||||||
|
Visit visit = this.clinicService.findVisitById(visitId);
|
||||||
|
if(visit == null){
|
||||||
|
return new ResponseEntity<Visit>(HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
return new ResponseEntity<Visit>(visit, HttpStatus.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@RequestMapping(value = "/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||||
|
public ResponseEntity<Void> addVisit(@RequestBody @Valid Visit visit, BindingResult bindingResult, UriComponentsBuilder ucBuilder){
|
||||||
|
if(bindingResult.hasErrors() || (visit == null)){
|
||||||
|
return new ResponseEntity<Void>(HttpStatus.BAD_REQUEST);
|
||||||
|
}
|
||||||
|
this.clinicService.saveVisit(visit);
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.setLocation(ucBuilder.path("/api/visits/{id}").buildAndExpand(visit.getId()).toUri());
|
||||||
|
return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value = "/{visitId}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||||
|
public ResponseEntity<Visit> updateVisit(@PathVariable("visitId") int visitId, @RequestBody @Valid Visit visit, BindingResult bindingResult){
|
||||||
|
if(bindingResult.hasErrors() || (visit == null)){
|
||||||
|
return new ResponseEntity<Visit>(HttpStatus.BAD_REQUEST);
|
||||||
|
}
|
||||||
|
Visit currentVisit = this.clinicService.findVisitById(visitId);
|
||||||
|
if(currentVisit == null){
|
||||||
|
return new ResponseEntity<Visit>(HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
currentVisit.setDate(visit.getDate());
|
||||||
|
currentVisit.setDescription(visit.getDescription());
|
||||||
|
currentVisit.setPet(visit.getPet());
|
||||||
|
this.clinicService.saveVisit(currentVisit);
|
||||||
|
return new ResponseEntity<Visit>(currentVisit, HttpStatus.NO_CONTENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value = "/{visitId}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||||
|
public ResponseEntity<Void> deleteVisit(@PathVariable("visitId") int visitId){
|
||||||
|
Visit visit = this.clinicService.findVisitById(visitId);
|
||||||
|
if(visit == null){
|
||||||
|
return new ResponseEntity<Void>(HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
this.clinicService.deleteVisit(visit);
|
||||||
|
// TODO delete error - FK etc.
|
||||||
|
return new ResponseEntity<Void>(HttpStatus.NO_CONTENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue