mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-22 07:15:49 +00:00
Added update owner form.
This commit is contained in:
parent
7805a7f8ef
commit
535564e614
2 changed files with 30 additions and 3 deletions
|
@ -1,4 +1,7 @@
|
||||||
package org.springframework.samples.petclinic.owner.rest;
|
package org.springframework.samples.petclinic.owner.rest;
|
||||||
|
|
||||||
|
import org.springframework.samples.petclinic.owner.Owner;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author Awadhesh Kumar
|
* @author Awadhesh Kumar
|
||||||
|
@ -14,5 +17,10 @@ public class ExistingOwnerForm extends NewOwnerForm{
|
||||||
public void setId(int id) {
|
public void setId(int id) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Owner NewOwner() {
|
||||||
|
Owner owner=super.NewOwner();
|
||||||
|
owner.setId(this.id);
|
||||||
|
return owner;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,9 +12,12 @@ 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.ui.Model;
|
||||||
import org.springframework.validation.BindingResult;
|
import org.springframework.validation.BindingResult;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
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.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PutMapping;
|
||||||
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;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
@ -24,7 +27,7 @@ import org.springframework.web.bind.annotation.RestController;
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@RestController("OwnerRestController")
|
@RestController("OwnerRestController")
|
||||||
@RequestMapping("/api/owner")
|
@RequestMapping("/api/owners")
|
||||||
public class OwnerController {
|
public class OwnerController {
|
||||||
private final OwnerRepository owners;
|
private final OwnerRepository owners;
|
||||||
|
|
||||||
|
@ -35,7 +38,7 @@ public class OwnerController {
|
||||||
this.visits = visits;
|
this.visits = visits;
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/new")
|
@PostMapping("")
|
||||||
public ResponseEntity<Object> createOwner(@Valid @RequestBody NewOwnerForm owner, BindingResult result) {
|
public ResponseEntity<Object> createOwner(@Valid @RequestBody NewOwnerForm owner, BindingResult result) {
|
||||||
if (result.hasErrors()) {
|
if (result.hasErrors()) {
|
||||||
return new ResponseEntity<Object>(result.getAllErrors(),HttpStatus.BAD_REQUEST);
|
return new ResponseEntity<Object>(result.getAllErrors(),HttpStatus.BAD_REQUEST);
|
||||||
|
@ -64,6 +67,22 @@ public class OwnerController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PutMapping("")
|
||||||
|
public ResponseEntity<Object> updateOwner(@Valid @RequestBody ExistingOwnerForm existingOwnerForm,BindingResult result) {
|
||||||
|
if (result.hasErrors()) {
|
||||||
|
return new ResponseEntity<Object>(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<ExistingOwnerForm> getAllOwners(NewOwnerForm ownerForm) {
|
private Collection<ExistingOwnerForm> getAllOwners(NewOwnerForm ownerForm) {
|
||||||
Collection<Owner>existingOwners=owners.findByLastName(ownerForm.getLastName());
|
Collection<Owner>existingOwners=owners.findByLastName(ownerForm.getLastName());
|
||||||
Collection<ExistingOwnerForm>owners=new ArrayList<>();
|
Collection<ExistingOwnerForm>owners=new ArrayList<>();
|
||||||
|
|
Loading…
Reference in a new issue