mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-22 07:15:49 +00:00
Added api to find owner.
This commit is contained in:
parent
cacf6f0fdd
commit
0db37cda20
3 changed files with 57 additions and 1 deletions
|
@ -0,0 +1,14 @@
|
|||
package org.springframework.samples.petclinic.owner.rest;
|
||||
|
||||
public class ExistingOwnerForm extends NewOwnerForm{
|
||||
private int id;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,13 +1,19 @@
|
|||
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.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
@ -38,6 +44,41 @@ public class OwnerController {
|
|||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue