|
@@ -0,0 +1,55 @@
|
|
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
|
+import java.util.ArrayList;
|
|
9
|
+import java.util.List;
|
|
10
|
+
|
|
11
|
+@RestController
|
|
12
|
+public class PersonController {
|
|
13
|
+
|
|
14
|
+ @Autowired
|
|
15
|
+ PersonRepository personRepository;
|
|
16
|
+
|
|
17
|
+ @RequestMapping(value = "/people", method = RequestMethod.POST)
|
|
18
|
+ public ResponseEntity<?> createPerson(@RequestBody Person p) {
|
|
19
|
+ Person p1 = personRepository.save(p);
|
|
20
|
+ return new ResponseEntity<>(p1, HttpStatus.CREATED);
|
|
21
|
+ }
|
|
22
|
+
|
|
23
|
+ @RequestMapping(value = "/people/{id}", method = RequestMethod.GET)
|
|
24
|
+ public ResponseEntity<?> getPerson(@PathVariable int id) {
|
|
25
|
+ Person p1 = personRepository.findOne(id);
|
|
26
|
+ if (p1 != null) {
|
|
27
|
+ return new ResponseEntity<>(p1, HttpStatus.OK);
|
|
28
|
+ }
|
|
29
|
+ return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
|
|
30
|
+ }
|
|
31
|
+
|
|
32
|
+ @RequestMapping(value = "/people", method = RequestMethod.GET)
|
|
33
|
+ public ResponseEntity<?> getPersonList() {
|
|
34
|
+ List<Person> people = new ArrayList<>();
|
|
35
|
+ personRepository.findAll().forEach(people::add);
|
|
36
|
+ return new ResponseEntity<>(people, HttpStatus.OK);
|
|
37
|
+ }
|
|
38
|
+
|
|
39
|
+ @RequestMapping(value = "/people/{id}", method = RequestMethod.PUT)
|
|
40
|
+ public ResponseEntity<?> updatePerson(Person p) {
|
|
41
|
+ if (getPerson(p.getId())!= null) {
|
|
42
|
+ p = personRepository.save(p);
|
|
43
|
+ return new ResponseEntity<>(p, HttpStatus.OK);
|
|
44
|
+ } else {
|
|
45
|
+ createPerson(p);
|
|
46
|
+ return new ResponseEntity<>(p, HttpStatus.CREATED);
|
|
47
|
+ }
|
|
48
|
+ }
|
|
49
|
+
|
|
50
|
+ @RequestMapping(value = "/people/{id}", method = RequestMethod.DELETE)
|
|
51
|
+ public ResponseEntity<?> deletePerson(int id) {
|
|
52
|
+ personRepository.delete(id);
|
|
53
|
+ return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
|
54
|
+ }
|
|
55
|
+}
|