diff --git a/src/main/java/org/springframework/samples/petclinic/owner/rest/ExistingOwnerForm.java b/src/main/java/org/springframework/samples/petclinic/owner/rest/ExistingOwnerForm.java new file mode 100644 index 000000000..666e9de11 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/owner/rest/ExistingOwnerForm.java @@ -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; + } +} diff --git a/src/main/java/org/springframework/samples/petclinic/owner/rest/NewOwnerForm.java b/src/main/java/org/springframework/samples/petclinic/owner/rest/NewOwnerForm.java new file mode 100644 index 000000000..738946852 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/owner/rest/NewOwnerForm.java @@ -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; + } +} diff --git a/src/main/java/org/springframework/samples/petclinic/owner/rest/OwnerController.java b/src/main/java/org/springframework/samples/petclinic/owner/rest/OwnerController.java new file mode 100644 index 000000000..783cffc97 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/owner/rest/OwnerController.java @@ -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 createOwner(@Valid @RequestBody NewOwnerForm owner, BindingResult result) { + if (result.hasErrors()) { + return new ResponseEntity(result.getAllErrors(),HttpStatus.BAD_REQUEST); + } else { + createNewOwner(owner); + return new ResponseEntity("Owner created",HttpStatus.OK); + } + } + + @GetMapping("") + public ResponseEntity 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 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 updateOwner(@Valid @RequestBody ExistingOwnerForm existingOwnerForm,BindingResult result) { + if (result.hasErrors()) { + return new ResponseEntity(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 getAllOwners(NewOwnerForm ownerForm) { + CollectionexistingOwners=owners.findByLastName(ownerForm.getLastName()); + Collectionowners=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); + } +} diff --git a/src/main/java/org/springframework/samples/petclinic/system/SwaggerConfiguration.java b/src/main/java/org/springframework/samples/petclinic/system/SwaggerConfiguration.java index d48abd007..d267c9f52 100644 --- a/src/main/java/org/springframework/samples/petclinic/system/SwaggerConfiguration.java +++ b/src/main/java/org/springframework/samples/petclinic/system/SwaggerConfiguration.java @@ -2,6 +2,7 @@ package org.springframework.samples.petclinic.system; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.web.bind.annotation.RestController; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.spi.DocumentationType; @@ -14,7 +15,7 @@ public class SwaggerConfiguration { @Bean public Docket productApi() { return new Docket(DocumentationType.SWAGGER_2).select() - .apis(RequestHandlerSelectors.basePackage("org.springframework.samples.petclinic.rest")) + .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)) .build(); } }