Added api to find owner.

This commit is contained in:
Awadhesh Kumar 2020-04-27 21:39:32 +05:30
parent cacf6f0fdd
commit 0db37cda20
3 changed files with 57 additions and 1 deletions

View file

@ -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;
}
}

View file

@ -1,13 +1,19 @@
package org.springframework.samples.petclinic.owner.rest; package org.springframework.samples.petclinic.owner.rest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import javax.validation.Valid; import javax.validation.Valid;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.samples.petclinic.owner.Owner; import org.springframework.samples.petclinic.owner.Owner;
import org.springframework.samples.petclinic.owner.OwnerRepository; import org.springframework.samples.petclinic.owner.OwnerRepository;
import org.springframework.samples.petclinic.visit.VisitRepository; import org.springframework.samples.petclinic.visit.VisitRepository;
import org.springframework.validation.BindingResult; 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.PostMapping;
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;
@ -38,6 +44,41 @@ public class OwnerController {
return new ResponseEntity<Object>("Owner created",HttpStatus.OK); 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) { private void createNewOwner(final NewOwnerForm owner) {
Owner newOwner=owner.NewOwner(); Owner newOwner=owner.NewOwner();

View file

@ -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();
} }
} }