|
@@ -0,0 +1,58 @@
|
|
1
|
+package io.zipcoder.crudapp;
|
|
2
|
+
|
|
3
|
+import org.springframework.http.HttpStatus;
|
|
4
|
+import org.springframework.http.ResponseEntity;
|
|
5
|
+import org.springframework.web.bind.annotation.*;
|
|
6
|
+
|
|
7
|
+@RestController
|
|
8
|
+public class PersonController {
|
|
9
|
+
|
|
10
|
+ PersonRepository personRepository;
|
|
11
|
+
|
|
12
|
+ public PersonController() {
|
|
13
|
+ }
|
|
14
|
+
|
|
15
|
+//
|
|
16
|
+// public ResponseEntity<Person> createPerson(@RequestParam("person") Person person) {
|
|
17
|
+// return new ResponseEntity<>(this.personRepository.save(person), HttpStatus.CREATED);
|
|
18
|
+// }
|
|
19
|
+
|
|
20
|
+ @PostMapping("/person")
|
|
21
|
+ public ResponseEntity<Person> createPerson(@RequestBody Person person) {
|
|
22
|
+ Person person1 = new Person(person.getFirstName(), person.getLastName(), person.getId());
|
|
23
|
+ return new ResponseEntity<>(this.personRepository.save(person1), HttpStatus.CREATED);
|
|
24
|
+ }
|
|
25
|
+
|
|
26
|
+ @GetMapping("/people/{id}")
|
|
27
|
+ public ResponseEntity<Person> getPerson(@PathVariable Long id) {
|
|
28
|
+ Person person = this.personRepository.findOne(id);
|
|
29
|
+ return new ResponseEntity<>(person, HttpStatus.OK);
|
|
30
|
+ }
|
|
31
|
+
|
|
32
|
+ @GetMapping("/people")
|
|
33
|
+ public ResponseEntity<Iterable<Person>> getPersonList() {
|
|
34
|
+ return new ResponseEntity<>(this.personRepository.findAll(), HttpStatus.OK);
|
|
35
|
+ }
|
|
36
|
+
|
|
37
|
+ @PutMapping("/people/{id}")
|
|
38
|
+ public ResponseEntity<Person> updatePerson(@PathVariable Long id, @RequestBody Person person) {
|
|
39
|
+ Person person1 = this.personRepository.findOne(id);
|
|
40
|
+
|
|
41
|
+ person1.setFirstName(person.getFirstName());
|
|
42
|
+ person1.setLastName(person.getLastName());
|
|
43
|
+ return new ResponseEntity<>(person1, HttpStatus.OK);
|
|
44
|
+ }
|
|
45
|
+
|
|
46
|
+ @DeleteMapping("/people/{id}")
|
|
47
|
+ public ResponseEntity<?> deletePerson(@PathVariable Long id) {
|
|
48
|
+ this.personRepository.delete(personRepository.findOne(id));
|
|
49
|
+ return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
|
50
|
+ }
|
|
51
|
+}
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|