Update Owner and Vets REST controllers

This commit is contained in:
Vitaliy Fedoriv 2016-10-27 20:27:03 +03:00
parent bbf2946b84
commit e9fb7c5356
2 changed files with 13 additions and 9 deletions

View file

@ -32,6 +32,7 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody; 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.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriComponentsBuilder; import org.springframework.web.util.UriComponentsBuilder;
@ -47,16 +48,19 @@ public class OwnerRestController {
@Autowired @Autowired
private ClinicService clinicService; private ClinicService clinicService;
@RequestMapping(value = "/}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @RequestMapping(value = "/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<Collection<Owner>> getAllOwners(){ public ResponseEntity<Collection<Owner>> getOwnersList(@RequestParam("lastName") String ownerLastName){
Collection<Owner> owners = this.clinicService.findOwnerByLastName(""); if (ownerLastName == null){
ownerLastName = "";
}
Collection<Owner> owners = this.clinicService.findOwnerByLastName(ownerLastName);
if(owners.isEmpty()){ if(owners.isEmpty()){
return new ResponseEntity<Collection<Owner>>(HttpStatus.NOT_FOUND); return new ResponseEntity<Collection<Owner>>(HttpStatus.NOT_FOUND);
} }
return new ResponseEntity<Collection<Owner>>(owners, HttpStatus.OK); return new ResponseEntity<Collection<Owner>>(owners, HttpStatus.OK);
} }
@RequestMapping(value = "/{ownerId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @RequestMapping(value = "/{ownerId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<Owner> getOwner(@PathVariable("ownerId") int ownerId){ public ResponseEntity<Owner> getOwner(@PathVariable("ownerId") int ownerId){
Owner owner = this.clinicService.findOwnerById(ownerId); Owner owner = this.clinicService.findOwnerById(ownerId);
if(owner == null){ if(owner == null){
@ -65,18 +69,18 @@ public class OwnerRestController {
return new ResponseEntity<Owner>(owner, HttpStatus.OK); return new ResponseEntity<Owner>(owner, HttpStatus.OK);
} }
@RequestMapping(value = "/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) @RequestMapping(value = "/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<Void> addOwner(@RequestBody @Valid Owner owner, BindingResult bindingResult, UriComponentsBuilder ucBuilder){ public ResponseEntity<Void> addOwner(@RequestBody @Valid Owner owner, BindingResult bindingResult, UriComponentsBuilder ucBuilder){
if(bindingResult.hasErrors() || (owner == null)){ if(bindingResult.hasErrors() || (owner == null)){
return new ResponseEntity<Void>(HttpStatus.BAD_REQUEST); return new ResponseEntity<Void>(HttpStatus.BAD_REQUEST);
} }
this.clinicService.saveOwner(owner); this.clinicService.saveOwner(owner);
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setLocation(ucBuilder.path("/owners/{id}").buildAndExpand(owner.getId()).toUri()); headers.setLocation(ucBuilder.path("/api/owners/{id}").buildAndExpand(owner.getId()).toUri());
return new ResponseEntity<Void>(headers, HttpStatus.CREATED); return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
} }
@RequestMapping(value = "/{ownerId}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE) @RequestMapping(value = "/{ownerId}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<Owner> updateOwner(@PathVariable("ownerId") int ownerId, @RequestBody @Valid Owner owner, BindingResult bindingResult, UriComponentsBuilder ucBuilder){ public ResponseEntity<Owner> updateOwner(@PathVariable("ownerId") int ownerId, @RequestBody @Valid Owner owner, BindingResult bindingResult, UriComponentsBuilder ucBuilder){
if(bindingResult.hasErrors() || (owner == null)){ if(bindingResult.hasErrors() || (owner == null)){
return new ResponseEntity<Owner>(HttpStatus.BAD_REQUEST); return new ResponseEntity<Owner>(HttpStatus.BAD_REQUEST);
@ -91,7 +95,7 @@ public class OwnerRestController {
currentOwner.setLastName(owner.getLastName());; currentOwner.setLastName(owner.getLastName());;
currentOwner.setTelephone(owner.getTelephone()); currentOwner.setTelephone(owner.getTelephone());
this.clinicService.saveOwner(currentOwner); this.clinicService.saveOwner(currentOwner);
return new ResponseEntity<Owner>(currentOwner, HttpStatus.OK); return new ResponseEntity<Owner>(currentOwner, HttpStatus.NO_CONTENT);
} }

View file

@ -40,7 +40,7 @@ public class VetRestController {
@Autowired @Autowired
private ClinicService clinicService; private ClinicService clinicService;
@RequestMapping(value = "/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @RequestMapping(value = "/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<Collection<Vet>> getAllVets(){ public ResponseEntity<Collection<Vet>> getAllVets(){
Collection<Vet> vets = new ArrayList<Vet>(); Collection<Vet> vets = new ArrayList<Vet>();
vets.addAll(this.clinicService.findVets()); vets.addAll(this.clinicService.findVets());