|
@@ -0,0 +1,71 @@
|
|
1
|
+package io.zipcoder.crudapp;
|
|
2
|
+
|
|
3
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
4
|
+import org.springframework.http.HttpStatus;
|
|
5
|
+import org.springframework.http.ResponseEntity;
|
|
6
|
+import org.springframework.web.bind.annotation.*;
|
|
7
|
+
|
|
8
|
+@RestController
|
|
9
|
+public class PersonController {
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+ @Autowired
|
|
13
|
+ PersonRepository personRepository;
|
|
14
|
+
|
|
15
|
+ public PersonController() {
|
|
16
|
+
|
|
17
|
+ }
|
|
18
|
+
|
|
19
|
+ @RequestMapping(value = "/people", method = RequestMethod.POST)
|
|
20
|
+ public ResponseEntity<Person> createPerson(@RequestBody Person p) {
|
|
21
|
+
|
|
22
|
+ return new ResponseEntity<>(personRepository.save(p), HttpStatus.CREATED);
|
|
23
|
+ }
|
|
24
|
+
|
|
25
|
+ @RequestMapping(value = "/people/{id}", method = RequestMethod.GET)
|
|
26
|
+ public ResponseEntity<Person> getPerson(@PathVariable Integer id) {
|
|
27
|
+
|
|
28
|
+ Person p = personRepository.findOne(id);
|
|
29
|
+
|
|
30
|
+ if (p == null) {
|
|
31
|
+
|
|
32
|
+ return new ResponseEntity<>(p, HttpStatus.NOT_FOUND);
|
|
33
|
+ }
|
|
34
|
+
|
|
35
|
+ return new ResponseEntity<>(p, HttpStatus.OK);
|
|
36
|
+ }
|
|
37
|
+
|
|
38
|
+ @RequestMapping(value = "/people", method = RequestMethod.GET)
|
|
39
|
+ public ResponseEntity<Iterable<Person>> getPersonList() {
|
|
40
|
+
|
|
41
|
+ return new ResponseEntity<>(personRepository.findAll(), HttpStatus.OK);
|
|
42
|
+ }
|
|
43
|
+
|
|
44
|
+ @RequestMapping(value = "/people/{id}", method = RequestMethod.PUT)
|
|
45
|
+ public ResponseEntity<Person> updatePerson(@PathVariable Integer id,@RequestBody Person updatedPerson) {
|
|
46
|
+
|
|
47
|
+ Person p = personRepository.findOne(id);
|
|
48
|
+
|
|
49
|
+ if (p != null) {
|
|
50
|
+
|
|
51
|
+ return new ResponseEntity<>(updatedPerson, HttpStatus.OK);
|
|
52
|
+ }
|
|
53
|
+
|
|
54
|
+ updatedPerson.setIdNumber(id);
|
|
55
|
+
|
|
56
|
+ personRepository.save(updatedPerson);
|
|
57
|
+
|
|
58
|
+ return new ResponseEntity<>(updatedPerson, HttpStatus.CREATED);
|
|
59
|
+
|
|
60
|
+ }
|
|
61
|
+
|
|
62
|
+ @RequestMapping(value = "/people/{id}", method = RequestMethod.DELETE)
|
|
63
|
+ public ResponseEntity<?> DeletePerson(@PathVariable Integer id) {
|
|
64
|
+
|
|
65
|
+ personRepository.delete(id);
|
|
66
|
+
|
|
67
|
+ return new ResponseEntity<>(null, HttpStatus.NO_CONTENT);
|
|
68
|
+ }
|
|
69
|
+
|
|
70
|
+}
|
|
71
|
+
|