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