|
@@ -0,0 +1,49 @@
|
|
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
|
+ @Autowired
|
|
12
|
+ PersonRepository personRepository;
|
|
13
|
+
|
|
14
|
+ @PostMapping("/people")
|
|
15
|
+ public ResponseEntity<Person> createPerson(@RequestBody Person person){
|
|
16
|
+ return new ResponseEntity<>(personRepository.save(person), HttpStatus.CREATED);
|
|
17
|
+
|
|
18
|
+ }
|
|
19
|
+
|
|
20
|
+ @GetMapping("/people/{id}")
|
|
21
|
+ public ResponseEntity<Person> getPerson(@PathVariable Long id){
|
|
22
|
+ return new ResponseEntity<>(personRepository.findOne(id), HttpStatus.OK);
|
|
23
|
+
|
|
24
|
+ }
|
|
25
|
+
|
|
26
|
+ @GetMapping("/people")
|
|
27
|
+ public ResponseEntity<Iterable<Person>> getPersonList(){
|
|
28
|
+ return new ResponseEntity<>(personRepository.findAll(), HttpStatus.OK);
|
|
29
|
+
|
|
30
|
+ }
|
|
31
|
+
|
|
32
|
+ @PutMapping("/people/{id}")
|
|
33
|
+ public ResponseEntity<Person> updatePerson(@PathVariable Long id, @RequestBody Person person){
|
|
34
|
+ Person personUpdate = personRepository.findOne(id);
|
|
35
|
+
|
|
36
|
+ personUpdate.setLastName(person.getLastName());
|
|
37
|
+ personUpdate.setFirstName(person.getFirstName());
|
|
38
|
+
|
|
39
|
+ // return new ResponseEntity<>(this.muffinRepository.save(foundMuffin), HttpStatus.OK);
|
|
40
|
+ return new ResponseEntity<>(this.personRepository.save(personUpdate),HttpStatus.OK );
|
|
41
|
+
|
|
42
|
+ }
|
|
43
|
+
|
|
44
|
+ @DeleteMapping("/people/{id}")
|
|
45
|
+ public void deletePerson(@PathVariable Long id){
|
|
46
|
+ personRepository.delete(id);
|
|
47
|
+
|
|
48
|
+ }
|
|
49
|
+}
|