mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-22 15:25:49 +00:00
Merge branch 'feature_rest_api'
This commit is contained in:
commit
6ee7f69a42
4 changed files with 208 additions and 1 deletions
|
@ -0,0 +1,26 @@
|
||||||
|
package org.springframework.samples.petclinic.owner.rest;
|
||||||
|
|
||||||
|
import org.springframework.samples.petclinic.owner.Owner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Awadhesh Kumar
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class ExistingOwnerForm extends NewOwnerForm{
|
||||||
|
private int id;
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Owner NewOwner() {
|
||||||
|
Owner owner=super.NewOwner();
|
||||||
|
owner.setId(this.id);
|
||||||
|
return owner;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,74 @@
|
||||||
|
package org.springframework.samples.petclinic.owner.rest;
|
||||||
|
|
||||||
|
import javax.validation.constraints.Digits;
|
||||||
|
import javax.validation.constraints.NotEmpty;
|
||||||
|
|
||||||
|
import org.springframework.samples.petclinic.owner.Owner;
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Awadhesh Kumar
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class NewOwnerForm {
|
||||||
|
@NotEmpty
|
||||||
|
private String firstName;
|
||||||
|
@NotEmpty
|
||||||
|
private String lastName;
|
||||||
|
@NotEmpty
|
||||||
|
private String address;
|
||||||
|
@NotEmpty
|
||||||
|
private String city;
|
||||||
|
@NotEmpty
|
||||||
|
@Digits(fraction = 0, integer = 10)
|
||||||
|
private String telephone;
|
||||||
|
|
||||||
|
public String getFirstName() {
|
||||||
|
return firstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFirstName(String firstName) {
|
||||||
|
this.firstName = firstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLastName() {
|
||||||
|
return lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLastName(String lastName) {
|
||||||
|
this.lastName = lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAddress() {
|
||||||
|
return address;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAddress(String address) {
|
||||||
|
this.address = address;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCity() {
|
||||||
|
return city;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCity(String city) {
|
||||||
|
this.city = city;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTelephone() {
|
||||||
|
return telephone;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTelephone(String telephone) {
|
||||||
|
this.telephone = telephone;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Owner NewOwner() {
|
||||||
|
Owner owner=new Owner();
|
||||||
|
owner.setFirstName(this.firstName);
|
||||||
|
owner.setLastName(this.lastName);
|
||||||
|
owner.setAddress(this.address);
|
||||||
|
owner.setCity(this.city);
|
||||||
|
owner.setTelephone(this.telephone);
|
||||||
|
return owner;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,106 @@
|
||||||
|
package org.springframework.samples.petclinic.owner.rest;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.validation.Valid;
|
||||||
|
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.RequestEntity;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.samples.petclinic.owner.Owner;
|
||||||
|
import org.springframework.samples.petclinic.owner.OwnerRepository;
|
||||||
|
import org.springframework.samples.petclinic.visit.VisitRepository;
|
||||||
|
import org.springframework.ui.Model;
|
||||||
|
import org.springframework.validation.BindingResult;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PutMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Awadhesh Kumar
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@RestController("OwnerRestController")
|
||||||
|
@RequestMapping("/api/owners")
|
||||||
|
public class OwnerController {
|
||||||
|
private final OwnerRepository owners;
|
||||||
|
|
||||||
|
private VisitRepository visits;
|
||||||
|
|
||||||
|
public OwnerController(OwnerRepository clinicService, VisitRepository visits) {
|
||||||
|
this.owners = clinicService;
|
||||||
|
this.visits = visits;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("")
|
||||||
|
public ResponseEntity<Object> createOwner(@Valid @RequestBody NewOwnerForm owner, BindingResult result) {
|
||||||
|
if (result.hasErrors()) {
|
||||||
|
return new ResponseEntity<Object>(result.getAllErrors(),HttpStatus.BAD_REQUEST);
|
||||||
|
} else {
|
||||||
|
createNewOwner(owner);
|
||||||
|
return new ResponseEntity<Object>("Owner created",HttpStatus.OK);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("")
|
||||||
|
public ResponseEntity<Object> processFindForm(NewOwnerForm owner) {
|
||||||
|
|
||||||
|
// allow parameterless GET request to return all records
|
||||||
|
if (owner.getLastName() == null) {
|
||||||
|
owner.setLastName(""); // empty string signifies broadest possible search
|
||||||
|
}
|
||||||
|
|
||||||
|
// find owners by last name
|
||||||
|
Collection<ExistingOwnerForm> results = getAllOwners(owner);
|
||||||
|
if (results.isEmpty()) {
|
||||||
|
// no owners found
|
||||||
|
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||||
|
} else {
|
||||||
|
// owners found
|
||||||
|
return new ResponseEntity<>(results, HttpStatus.OK);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("")
|
||||||
|
public ResponseEntity<Object> updateOwner(@Valid @RequestBody ExistingOwnerForm existingOwnerForm,BindingResult result) {
|
||||||
|
if (result.hasErrors()) {
|
||||||
|
return new ResponseEntity<Object>(result.getAllErrors(),HttpStatus.BAD_REQUEST);
|
||||||
|
} else {
|
||||||
|
updateOwner(existingOwnerForm);
|
||||||
|
return new ResponseEntity<>("Owner updated",HttpStatus.OK);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateOwner(final ExistingOwnerForm existingOwnerForm) {
|
||||||
|
Owner existingOwner=this.owners.findById(existingOwnerForm.getId());
|
||||||
|
existingOwner=existingOwnerForm.NewOwner();
|
||||||
|
this.owners.save(existingOwner);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Collection<ExistingOwnerForm> getAllOwners(NewOwnerForm ownerForm) {
|
||||||
|
Collection<Owner>existingOwners=owners.findByLastName(ownerForm.getLastName());
|
||||||
|
Collection<ExistingOwnerForm>owners=new ArrayList<>();
|
||||||
|
for(Owner existingOwner:existingOwners) {
|
||||||
|
ExistingOwnerForm owner=new ExistingOwnerForm();
|
||||||
|
owner.setId(existingOwner.getId());
|
||||||
|
owner.setAddress(existingOwner.getAddress());
|
||||||
|
owner.setCity(existingOwner.getCity());
|
||||||
|
owner.setFirstName(existingOwner.getFirstName());
|
||||||
|
owner.setLastName(existingOwner.getLastName());
|
||||||
|
owner.setTelephone(existingOwner.getTelephone());
|
||||||
|
owners.add(owner);
|
||||||
|
}
|
||||||
|
return owners;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createNewOwner(final NewOwnerForm owner) {
|
||||||
|
Owner newOwner=owner.NewOwner();
|
||||||
|
this.owners.save(newOwner);
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,6 +2,7 @@ package org.springframework.samples.petclinic.system;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import springfox.documentation.builders.RequestHandlerSelectors;
|
import springfox.documentation.builders.RequestHandlerSelectors;
|
||||||
import springfox.documentation.spi.DocumentationType;
|
import springfox.documentation.spi.DocumentationType;
|
||||||
|
@ -14,7 +15,7 @@ public class SwaggerConfiguration {
|
||||||
@Bean
|
@Bean
|
||||||
public Docket productApi() {
|
public Docket productApi() {
|
||||||
return new Docket(DocumentationType.SWAGGER_2).select()
|
return new Docket(DocumentationType.SWAGGER_2).select()
|
||||||
.apis(RequestHandlerSelectors.basePackage("org.springframework.samples.petclinic.rest"))
|
.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue