mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-22 07:15:49 +00:00
commit
f34e65ebd1
2 changed files with 66 additions and 3 deletions
|
@ -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("")
|
@PutMapping("")
|
||||||
public ResponseEntity<Object> updateOwner(@Valid @RequestBody ExistingOwnerForm existingOwnerForm,BindingResult result) {
|
public ResponseEntity<Object> updateOwner(@Valid @RequestBody ExistingOwnerForm existingOwnerForm,BindingResult result) {
|
||||||
if (result.hasErrors()) {
|
if (result.hasErrors()) {
|
||||||
|
@ -87,16 +106,23 @@ public class OwnerController {
|
||||||
Collection<Owner>existingOwners=owners.findByLastName(ownerForm.getLastName());
|
Collection<Owner>existingOwners=owners.findByLastName(ownerForm.getLastName());
|
||||||
Collection<ExistingOwnerForm>owners=new ArrayList<>();
|
Collection<ExistingOwnerForm>owners=new ArrayList<>();
|
||||||
for(Owner existingOwner:existingOwners) {
|
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.setId(existingOwner.getId());
|
||||||
owner.setAddress(existingOwner.getAddress());
|
owner.setAddress(existingOwner.getAddress());
|
||||||
owner.setCity(existingOwner.getCity());
|
owner.setCity(existingOwner.getCity());
|
||||||
owner.setFirstName(existingOwner.getFirstName());
|
owner.setFirstName(existingOwner.getFirstName());
|
||||||
owner.setLastName(existingOwner.getLastName());
|
owner.setLastName(existingOwner.getLastName());
|
||||||
owner.setTelephone(existingOwner.getTelephone());
|
owner.setTelephone(existingOwner.getTelephone());
|
||||||
owners.add(owner);
|
|
||||||
}
|
}
|
||||||
return owners;
|
return owner;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createNewOwner(final NewOwnerForm owner) {
|
private void createNewOwner(final NewOwnerForm owner) {
|
||||||
|
|
|
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue