Merge pull request #1 from awadhesh22791/feature_rest_api

Enhancement
This commit is contained in:
Awadhesh Kumar 2020-04-28 09:15:26 +05:30 committed by GitHub
commit f34e65ebd1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 66 additions and 3 deletions

View file

@ -67,6 +67,25 @@ public class OwnerController {
}
}
@GetMapping("/{ownerid}")
public ResponseEntity<Object> getOwner(@PathVariable("ownerid") int ownerId) {
// find owners by last name
ExistingOwnerForm result = getOwnerDetail(ownerId);
if (result==null) {
// no owners found
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
} else {
// owners found
return new ResponseEntity<>(result, HttpStatus.OK);
}
}
private ExistingOwnerForm getOwnerDetail(int ownerId) {
Owner existingOwner=this.owners.findById(ownerId);
return getOwnerDetail(existingOwner);
}
@PutMapping("")
public ResponseEntity<Object> updateOwner(@Valid @RequestBody ExistingOwnerForm existingOwnerForm,BindingResult result) {
if (result.hasErrors()) {
@ -87,16 +106,23 @@ public class OwnerController {
Collection<Owner>existingOwners=owners.findByLastName(ownerForm.getLastName());
Collection<ExistingOwnerForm>owners=new ArrayList<>();
for(Owner existingOwner:existingOwners) {
ExistingOwnerForm owner=new ExistingOwnerForm();
owners.add(getOwnerDetail(existingOwner));
}
return owners;
}
private ExistingOwnerForm getOwnerDetail(Owner existingOwner) {
ExistingOwnerForm owner=null;
if(existingOwner!=null) {
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;
return owner;
}
private void createNewOwner(final NewOwnerForm owner) {

View file

@ -0,0 +1,37 @@
package org.springframework.samples.petclinic.owner.rest;
import java.util.Collection;
import org.springframework.samples.petclinic.owner.Owner;
import org.springframework.samples.petclinic.owner.OwnerRepository;
import org.springframework.samples.petclinic.owner.PetRepository;
import org.springframework.samples.petclinic.owner.PetType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
*
* @author Awadhesh Kumar
*
*/
@RestController("PetController")
@RequestMapping("/api/owners/{ownerid}/pets")
public class PetController {
private final PetRepository pets;
private final OwnerRepository owners;
public PetController(PetRepository pets, OwnerRepository owners) {
this.pets = pets;
this.owners = owners;
}
@GetMapping("/types")
public Collection<PetType> populatePetTypes() {
return this.pets.findPetTypes();
}
}