|
@@ -1,34 +1,49 @@
|
1
|
1
|
package io.zipcoder.crudapp;
|
2
|
2
|
|
|
3
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
4
|
+import org.springframework.http.HttpStatus;
|
|
5
|
+import org.springframework.http.ResponseEntity;
|
3
|
6
|
import org.springframework.web.bind.annotation.*;
|
4
|
7
|
|
|
8
|
+import java.util.ArrayList;
|
|
9
|
+import java.util.Iterator;
|
5
|
10
|
import java.util.List;
|
6
|
11
|
|
7
|
12
|
@RestController
|
8
|
13
|
public class PersonController {
|
9
|
14
|
|
|
15
|
+ @Autowired
|
|
16
|
+ PersonRepository personRepository;
|
|
17
|
+
|
10
|
18
|
@RequestMapping(value = "/people", method = RequestMethod.POST)
|
11
|
|
- public Person createPerson(Person p){
|
12
|
|
- return p;
|
|
19
|
+ public ResponseEntity<Person> createPerson(@RequestBody Person p){
|
|
20
|
+ personRepository.save(p);
|
|
21
|
+ return new ResponseEntity<>(p,HttpStatus.CREATED);
|
13
|
22
|
}
|
14
|
23
|
|
15
|
24
|
|
16
|
25
|
@RequestMapping(value = "/people/{id}", method = RequestMethod.GET)
|
17
|
|
- public Person getPerson(@PathVariable("id")int id){
|
18
|
|
- return null;
|
|
26
|
+ public ResponseEntity<Person> getPerson(@PathVariable("id")int id){
|
|
27
|
+ Person p = personRepository.findOne((long)id);
|
|
28
|
+ return new ResponseEntity<>(p, HttpStatus.OK);
|
19
|
29
|
}
|
20
|
30
|
|
21
|
31
|
@RequestMapping(value = "/people",method = RequestMethod.GET)
|
22
|
|
- public List<Person> getPersonList(){
|
23
|
|
- return null;
|
|
32
|
+ public ResponseEntity<Iterable<Person>> getPersonList(){
|
|
33
|
+ Iterable<Person> ppl = personRepository.findAll();
|
|
34
|
+ return new ResponseEntity<>(ppl, HttpStatus.OK);
|
24
|
35
|
}
|
25
|
36
|
|
26
|
37
|
@RequestMapping(value = "/people/{id}", method = RequestMethod.PUT)
|
27
|
|
- public Person updatePerson(Person p){
|
28
|
|
- return p;
|
|
38
|
+ public ResponseEntity<Person> updatePerson(@RequestBody Person p){
|
|
39
|
+ personRepository.save(p);
|
|
40
|
+ return new ResponseEntity<>(p, HttpStatus.OK);
|
29
|
41
|
}
|
30
|
42
|
|
31
|
43
|
@RequestMapping(value = "/people/{id}", method = RequestMethod.DELETE)
|
32
|
|
- public void deletePerson(@PathVariable("id")int id){}
|
|
44
|
+ public ResponseEntity<Person> deletePerson(@PathVariable("id")int id){
|
|
45
|
+ personRepository.delete((long)id);
|
|
46
|
+ return new ResponseEntity<>( HttpStatus.NO_CONTENT);
|
|
47
|
+ }
|
33
|
48
|
|
34
|
49
|
}
|