|
@@ -0,0 +1,78 @@
|
|
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
|
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
|
|
8
|
+
|
|
9
|
+import java.net.URI;
|
|
10
|
+import java.util.ArrayList;
|
|
11
|
+import java.util.List;
|
|
12
|
+import java.util.stream.Collectors;
|
|
13
|
+import java.util.stream.StreamSupport;
|
|
14
|
+
|
|
15
|
+/*
|
|
16
|
+Create a `PersonController` class with `Person createPerson(Person p)`, `
|
|
17
|
+Person getPerson(int id)`, `List<Person> getPersonList()`, `Person updatePerson(Person p)`,
|
|
18
|
+ and `void DeletePerson(int id)` methods, and let it track a list of Person objects.
|
|
19
|
+
|
|
20
|
+Add the `@RestController` annotation to your `PersonController`
|
|
21
|
+class, and using the "Endpoints" list in the Reference section below,
|
|
22
|
+add the appropriate `@RequestMapping` annotations to each of your methods.
|
|
23
|
+Endpoints should be at `/people` and `/people/{id}` as appropriate.
|
|
24
|
+You will have to use `@PathVariable` for id numbers in the URI and `@RequestBody`
|
|
25
|
+for Person objects sent in the requests.
|
|
26
|
+
|
|
27
|
+ */
|
|
28
|
+@RestController
|
|
29
|
+public class PersonController {
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+ @Autowired
|
|
33
|
+ PersonRepository personRepository;
|
|
34
|
+
|
|
35
|
+ @RequestMapping(value = "/person", method = RequestMethod.POST )
|
|
36
|
+
|
|
37
|
+ public ResponseEntity <?> createPerson(@RequestBody Person p){
|
|
38
|
+
|
|
39
|
+ URI newPollUri = ServletUriComponentsBuilder
|
|
40
|
+ .fromCurrentRequest()
|
|
41
|
+ .path("/{id}")
|
|
42
|
+ .buildAndExpand(p .getId())
|
|
43
|
+ .toUri();
|
|
44
|
+ return new ResponseEntity<> (null, HttpStatus.CREATED) ;
|
|
45
|
+
|
|
46
|
+ }
|
|
47
|
+
|
|
48
|
+ @RequestMapping(value = "/person{id}", method = RequestMethod.GET)
|
|
49
|
+
|
|
50
|
+ public ResponseEntity<?> getPerson(@PathVariable Integer id){
|
|
51
|
+
|
|
52
|
+ Person person=personRepository.findOne(id);
|
|
53
|
+
|
|
54
|
+ return new ResponseEntity<>(person ,HttpStatus.OK ) ;
|
|
55
|
+ }
|
|
56
|
+
|
|
57
|
+ @RequestMapping(value = "/person", method = RequestMethod.GET)
|
|
58
|
+ public ResponseEntity<Iterable <Person > >getPersonList( ){
|
|
59
|
+ Iterable<Person> person = personRepository .findAll();
|
|
60
|
+
|
|
61
|
+ Iterable<Person > personIterable = StreamSupport.stream(person .spliterator(),false)
|
|
62
|
+
|
|
63
|
+ .collect(Collectors.toList() );
|
|
64
|
+ return new ResponseEntity<>(personIterable , HttpStatus.OK ) ;
|
|
65
|
+ }
|
|
66
|
+
|
|
67
|
+ @RequestMapping(value = "/person/{id}", method = RequestMethod.PUT)
|
|
68
|
+ public ResponseEntity<?> updatePerson(@RequestBody Person person,@PathVariable Integer id){
|
|
69
|
+ Person p=personRepository.save(person ) ;
|
|
70
|
+ return new ResponseEntity<>(HttpStatus.OK);
|
|
71
|
+ }
|
|
72
|
+ @RequestMapping(value = "/person{id}", method = RequestMethod.DELETE )
|
|
73
|
+ public void deletePerson( @PathVariable Integer id){
|
|
74
|
+ personRepository .delete(id);
|
|
75
|
+
|
|
76
|
+ }
|
|
77
|
+
|
|
78
|
+}
|