|
@@ -0,0 +1,47 @@
|
|
1
|
+package io.zipcoder.crudapp;
|
|
2
|
+
|
|
3
|
+import io.zipcoder.crudapp.Person;
|
|
4
|
+import io.zipcoder.crudapp.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
|
+import java.util.List;
|
|
11
|
+
|
|
12
|
+@RestController
|
|
13
|
+public class PersonController {
|
|
14
|
+
|
|
15
|
+ @Autowired
|
|
16
|
+ private PersonRepository personRepository;
|
|
17
|
+
|
|
18
|
+ @PostMapping(path = "/person")
|
|
19
|
+ @ResponseBody
|
|
20
|
+ public ResponseEntity<Person> createPerson(@RequestBody Person p){
|
|
21
|
+ return new ResponseEntity<>(personRepository.save(p), HttpStatus.CREATED);
|
|
22
|
+ }
|
|
23
|
+
|
|
24
|
+ @GetMapping(path = "/person/{id}")
|
|
25
|
+ @ResponseBody
|
|
26
|
+ public ResponseEntity<Person> getPerson(@PathVariable int id){
|
|
27
|
+ return new ResponseEntity<>(personRepository.findOne(id), HttpStatus.OK);
|
|
28
|
+ }
|
|
29
|
+
|
|
30
|
+ @GetMapping(path = "/person")
|
|
31
|
+ public ResponseEntity<List<Person>> getPersonList(){
|
|
32
|
+ return new ResponseEntity<> ((List<Person>) personRepository.findAll(), HttpStatus.OK);
|
|
33
|
+ }
|
|
34
|
+
|
|
35
|
+ @PutMapping(path = "/person/{id}")
|
|
36
|
+ public ResponseEntity<Person> updatePerson(@RequestBody Person p, @PathVariable int id){
|
|
37
|
+ id = p.getId();
|
|
38
|
+ return new ResponseEntity<>(personRepository.save(p), HttpStatus.OK);
|
|
39
|
+ }
|
|
40
|
+
|
|
41
|
+ @DeleteMapping(path = "/person/{id}")
|
|
42
|
+ public ResponseEntity<Boolean> deletePerson(@PathVariable int id){
|
|
43
|
+ personRepository.delete(id);
|
|
44
|
+ return new ResponseEntity<>(true, HttpStatus.OK);
|
|
45
|
+ }
|
|
46
|
+
|
|
47
|
+}
|