|
@@ -0,0 +1,46 @@
|
|
1
|
+package io.zipcoder.crudapp;
|
|
2
|
+
|
|
3
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
4
|
+import org.springframework.web.bind.annotation.*;
|
|
5
|
+
|
|
6
|
+import java.util.ArrayList;
|
|
7
|
+import java.util.List;
|
|
8
|
+
|
|
9
|
+/**
|
|
10
|
+ * @author leon on 08/01/2019.
|
|
11
|
+ */
|
|
12
|
+@RestController
|
|
13
|
+public class PersonController {
|
|
14
|
+ @Autowired
|
|
15
|
+ private PersonRepository repository;
|
|
16
|
+
|
|
17
|
+ @PostMapping("/people")
|
|
18
|
+ Person createPerson(@RequestBody Person p) {
|
|
19
|
+ repository.save(p);
|
|
20
|
+ return p;
|
|
21
|
+ }
|
|
22
|
+
|
|
23
|
+ @GetMapping("/people")
|
|
24
|
+ Iterable<Person> getPersonList() {
|
|
25
|
+ return repository.findAll();
|
|
26
|
+ }
|
|
27
|
+
|
|
28
|
+ @GetMapping("/people/{id}")
|
|
29
|
+ Person getPerson(@PathVariable int id) {
|
|
30
|
+ return repository.findOne(id);
|
|
31
|
+ }
|
|
32
|
+
|
|
33
|
+ @PutMapping("/people/{id}")
|
|
34
|
+ Person updatePerson(@PathVariable int id, @RequestBody Person p) {
|
|
35
|
+ Person personToBeUpdated = getPerson(id);
|
|
36
|
+ personToBeUpdated.setFirstName(p.getFirstName());
|
|
37
|
+ personToBeUpdated.setLastName(p.getLastName());
|
|
38
|
+ repository.save(personToBeUpdated);
|
|
39
|
+ return personToBeUpdated;
|
|
40
|
+ }
|
|
41
|
+
|
|
42
|
+ @DeleteMapping("/people/{id}")
|
|
43
|
+ void DeletePerson(@PathVariable int id) {
|
|
44
|
+ repository.delete(id);
|
|
45
|
+ }
|
|
46
|
+}
|