|
@@ -0,0 +1,64 @@
|
|
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.List;
|
|
9
|
+
|
|
10
|
+@RestController
|
|
11
|
+public class PersonController {
|
|
12
|
+
|
|
13
|
+ @Autowired
|
|
14
|
+ PersonRepository personRepository;
|
|
15
|
+
|
|
16
|
+ public PersonController() {
|
|
17
|
+ }
|
|
18
|
+
|
|
19
|
+ /*@PostMapping
|
|
20
|
+ public ResponseEntity<Person> createPerson(@RequestParam("person") Person person){
|
|
21
|
+ return new ResponseEntity<>(this.personRepository.save(person), HttpStatus.CREATED);
|
|
22
|
+ }*/
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+ @PostMapping("/person")
|
|
26
|
+ public ResponseEntity<Person> createPerson(@RequestBody Person person){
|
|
27
|
+ Person person1 = new Person(person.getFirstName(), person.getLastName(), (int) person.getId());
|
|
28
|
+
|
|
29
|
+ return new ResponseEntity<>(this.personRepository.save(person1), HttpStatus.CREATED);
|
|
30
|
+ // return new ResponseEntity<>(this.personRepository.save(person), HttpStatus.CREATED);
|
|
31
|
+ }
|
|
32
|
+
|
|
33
|
+ /* public ResponseEntity<Muffin> create(@RequestParam("muffin") Muffin muffin) {
|
|
34
|
+ return new ResponseEntity<>(this.muffinRepository.save(muffin), HttpStatus.CREATED);
|
|
35
|
+ }
|
|
36
|
+*/
|
|
37
|
+
|
|
38
|
+ @GetMapping("/people/{id}")
|
|
39
|
+ public ResponseEntity<Person> getPerson(@PathVariable Long id){
|
|
40
|
+ Person person = this.personRepository.findOne(id);
|
|
41
|
+ return new ResponseEntity<>(person, HttpStatus.OK);
|
|
42
|
+
|
|
43
|
+ }
|
|
44
|
+
|
|
45
|
+ @GetMapping("/people")
|
|
46
|
+ public ResponseEntity<Iterable<Person>> getPersonList(){
|
|
47
|
+ return new ResponseEntity<>(this.personRepository.findAll(), HttpStatus.OK);
|
|
48
|
+ }
|
|
49
|
+
|
|
50
|
+ @PutMapping("/people/{id}")
|
|
51
|
+ public ResponseEntity<Person> updatePerson(@PathVariable Long id, @RequestBody Person person){
|
|
52
|
+ Person person1 = this.personRepository.findOne(id);
|
|
53
|
+
|
|
54
|
+ person1.setFirstName(person.getFirstName());
|
|
55
|
+ person1.setLastName(person.getLastName());
|
|
56
|
+ return new ResponseEntity<>(person1, HttpStatus.OK);
|
|
57
|
+ }
|
|
58
|
+
|
|
59
|
+ @DeleteMapping("/people/{id}")
|
|
60
|
+ public ResponseEntity<?> deletePerson(@PathVariable long id){
|
|
61
|
+ this.personRepository.delete(personRepository.findOne(id));
|
|
62
|
+ return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
|
63
|
+ }
|
|
64
|
+}
|