|
@@ -0,0 +1,56 @@
|
|
1
|
+package io.zipcoder.crudapp.Controller;
|
|
2
|
+
|
|
3
|
+import io.zipcoder.crudapp.Model.Person;
|
|
4
|
+import io.zipcoder.crudapp.Repository.PersonRepository;
|
|
5
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
6
|
+import org.springframework.http.HttpStatus;
|
|
7
|
+import org.springframework.http.ResponseEntity;
|
|
8
|
+import org.springframework.web.bind.annotation.*;
|
|
9
|
+
|
|
10
|
+@RestController
|
|
11
|
+public class PersonController {
|
|
12
|
+
|
|
13
|
+ private final PersonRepository personRepository;
|
|
14
|
+
|
|
15
|
+ @Autowired
|
|
16
|
+ public PersonController(PersonRepository personRepository) {
|
|
17
|
+ this.personRepository = personRepository;
|
|
18
|
+ }
|
|
19
|
+
|
|
20
|
+ @PostMapping("/person")
|
|
21
|
+ public ResponseEntity<Person> createPerson(@RequestBody Person p) {
|
|
22
|
+ return new ResponseEntity<>(this.personRepository.save(p), HttpStatus.CREATED);
|
|
23
|
+ }
|
|
24
|
+
|
|
25
|
+ @GetMapping("/person/{id}")
|
|
26
|
+ public ResponseEntity<Person> getPerson(@PathVariable int id) {
|
|
27
|
+ if (this.personRepository.exists(id)) {
|
|
28
|
+ return new ResponseEntity<>(this.personRepository.findOne(id), HttpStatus.OK);
|
|
29
|
+ } else {
|
|
30
|
+ return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
|
31
|
+ }
|
|
32
|
+ }
|
|
33
|
+
|
|
34
|
+ @GetMapping("/person")
|
|
35
|
+ public ResponseEntity<Iterable<Person>> getPersonList() {
|
|
36
|
+ return new ResponseEntity<>(this.personRepository.findAll(), HttpStatus.OK);
|
|
37
|
+ }
|
|
38
|
+
|
|
39
|
+ @PutMapping("/person/{id}")
|
|
40
|
+ public ResponseEntity<Person> updatePerson(@PathVariable int id, @RequestBody Person p) {
|
|
41
|
+ if (!this.personRepository.exists(id)) {
|
|
42
|
+ return new ResponseEntity<>(this.personRepository.save(p), HttpStatus.CREATED);
|
|
43
|
+ } else {
|
|
44
|
+ Person person = this.personRepository.findOne(id);
|
|
45
|
+ person.setFirstName(p.getFirstName());
|
|
46
|
+ person.setLastName(p.getLastName());
|
|
47
|
+ return new ResponseEntity<>(this.personRepository.findOne(id), HttpStatus.OK);
|
|
48
|
+ }
|
|
49
|
+ }
|
|
50
|
+
|
|
51
|
+ @DeleteMapping("/person/{id}")
|
|
52
|
+ public void deletePerson(@PathVariable int id) {
|
|
53
|
+ this.personRepository.delete(id);
|
|
54
|
+ }
|
|
55
|
+
|
|
56
|
+}
|