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