|
@@ -0,0 +1,61 @@
|
|
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 javax.xml.ws.Response;
|
|
9
|
+import java.util.ArrayList;
|
|
10
|
+import java.util.Iterator;
|
|
11
|
+import java.util.List;
|
|
12
|
+
|
|
13
|
+@RestController
|
|
14
|
+public class PersonController {
|
|
15
|
+
|
|
16
|
+ private Person person = new Person();
|
|
17
|
+ private List<Person> personList = new ArrayList<>();
|
|
18
|
+
|
|
19
|
+ @Autowired
|
|
20
|
+ PersonRepository personRepository;
|
|
21
|
+
|
|
22
|
+ @PostMapping(value = "/people")
|
|
23
|
+ public ResponseEntity<?> createPerson(@RequestBody Person p){
|
|
24
|
+ this.personRepository.save(p);
|
|
25
|
+
|
|
26
|
+ return new ResponseEntity<>(this.personRepository.save(p), HttpStatus.CREATED);
|
|
27
|
+ }
|
|
28
|
+
|
|
29
|
+ @GetMapping(value = "/people/{id}")
|
|
30
|
+ public ResponseEntity<?> getPerson(@PathVariable int id){
|
|
31
|
+
|
|
32
|
+ return new ResponseEntity<>(personRepository.findOne(id), HttpStatus.OK);
|
|
33
|
+ }
|
|
34
|
+
|
|
35
|
+ @GetMapping(value = "/people")
|
|
36
|
+ public ResponseEntity<?> getPersonList(){
|
|
37
|
+
|
|
38
|
+ Iterator<Person> personIterator = personRepository.findAll().iterator();
|
|
39
|
+
|
|
40
|
+ ArrayList<Person> newPersonList = new ArrayList<>();
|
|
41
|
+
|
|
42
|
+ while(personIterator.hasNext()){
|
|
43
|
+ newPersonList.add(personIterator.next());
|
|
44
|
+ }
|
|
45
|
+
|
|
46
|
+ return new ResponseEntity<>(personRepository.findAll(), HttpStatus.OK);
|
|
47
|
+ }
|
|
48
|
+
|
|
49
|
+ @PutMapping("/people/{id}")
|
|
50
|
+ public ResponseEntity<?> updatePerson(@RequestBody Person p){
|
|
51
|
+
|
|
52
|
+ return new ResponseEntity<>(personRepository.save(p), HttpStatus.OK);
|
|
53
|
+ }
|
|
54
|
+
|
|
55
|
+ @DeleteMapping("/people/{id}")
|
|
56
|
+ public ResponseEntity<?> deletePerson(@PathVariable int id){
|
|
57
|
+ personRepository.delete(id);
|
|
58
|
+ return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
|
59
|
+ }
|
|
60
|
+
|
|
61
|
+}
|