Added rest controller for owner.

This commit is contained in:
Awadhesh Kumar 2020-04-27 09:17:07 +05:30
parent 339646cfcd
commit 1752f1789b
2 changed files with 112 additions and 0 deletions

View file

@ -0,0 +1,70 @@
package org.springframework.samples.petclinic.rest;
import javax.validation.constraints.Digits;
import javax.validation.constraints.NotEmpty;
import org.springframework.samples.petclinic.owner.Owner;
public class NewOwnerForm {
@NotEmpty
private String firstName;
@NotEmpty
private String lastName;
@NotEmpty
private String address;
@NotEmpty
private String city;
@NotEmpty
@Digits(fraction = 0, integer = 10)
private String telephone;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public Owner NewOwner() {
Owner owner=new Owner();
owner.setFirstName(this.firstName);
owner.setLastName(this.lastName);
owner.setAddress(this.address);
owner.setCity(this.city);
owner.setTelephone(this.telephone);
return owner;
}
}

View file

@ -0,0 +1,42 @@
package org.springframework.samples.petclinic.rest;
import javax.validation.Valid;
import org.springframework.http.HttpStatus;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController("OwnerRestController")
@RequestMapping("/api/owner")
public class OwnerController {
private final OwnerRepository owners;
private VisitRepository visits;
public OwnerController(OwnerRepository clinicService, VisitRepository visits) {
this.owners = clinicService;
this.visits = visits;
}
@PostMapping("/new")
public ResponseEntity<Object> processCreationForm(@Valid @RequestBody NewOwnerForm owner, BindingResult result) {
if (result.hasErrors()) {
return new ResponseEntity<Object>(result.getAllErrors(),HttpStatus.BAD_REQUEST);
} else {
createNewOwner(owner);
return new ResponseEntity<Object>("Owner created",HttpStatus.OK);
}
}
private void createNewOwner(final NewOwnerForm owner) {
Owner newOwner=owner.NewOwner();
this.owners.save(newOwner);
}
}