|
@@ -1,39 +1,55 @@
|
1
|
1
|
package io.zipcoder.crudapp;
|
2
|
2
|
|
|
3
|
+import org.springframework.beans.factory.annotation.Autowired;
|
3
|
4
|
import org.springframework.web.bind.annotation.*;
|
4
|
5
|
|
5
|
6
|
import java.util.List;
|
|
7
|
+import java.util.stream.Collectors;
|
|
8
|
+import java.util.stream.StreamSupport;
|
6
|
9
|
|
7
|
10
|
@RestController
|
8
|
|
-public class PersonController {
|
|
11
|
+public class PersonController{
|
|
12
|
+
|
|
13
|
+// @Autowired
|
|
14
|
+// PersonService personService;
|
|
15
|
+ @Autowired
|
|
16
|
+ PersonRepository personRepository;
|
9
|
17
|
|
10
|
18
|
// var URI = "http://localhost:8080/people/";
|
11
|
|
- @PostMapping("/people/{firstName}/{lastName}")
|
12
|
|
- Person createPerson(@PathVariable String firstName, @PathVariable String lastName){ //(Person p)
|
13
|
|
- Person person = new Person(firstName, lastName);
|
14
|
|
- return null;
|
|
19
|
+ @PostMapping("/people")
|
|
20
|
+ Person createPerson(@RequestBody Person person){ //(Person p)
|
|
21
|
+ personRepository.save(person);
|
|
22
|
+ return person;
|
15
|
23
|
}
|
16
|
24
|
|
17
|
25
|
@GetMapping("/people/{id}")
|
18
|
|
- Person getPerson(@PathVariable int requestedId){
|
19
|
|
- int id = requestedId;
|
20
|
|
- return null;
|
|
26
|
+ Person getPerson(@PathVariable int id){
|
|
27
|
+ int newId = id;
|
|
28
|
+ return personRepository.findOne(newId);
|
21
|
29
|
}
|
22
|
30
|
|
23
|
31
|
@GetMapping("/people")
|
24
|
32
|
List<Person> getPersonList(){
|
25
|
|
- return null;
|
|
33
|
+ List<Person> list = StreamSupport
|
|
34
|
+ .stream(
|
|
35
|
+ personRepository
|
|
36
|
+ .findAll()
|
|
37
|
+ .spliterator(), false
|
|
38
|
+ )
|
|
39
|
+ .collect(Collectors.toList());
|
|
40
|
+ return list;
|
26
|
41
|
}
|
27
|
42
|
|
28
|
43
|
@PostMapping("/people/{id}")
|
29
|
44
|
Person updatePerson(Person p){
|
30
|
|
- //update person then return
|
|
45
|
+ Person newPerson = personRepository.findOne(p.getId());
|
|
46
|
+ newPerson = p;
|
31
|
47
|
return p;
|
32
|
48
|
}
|
33
|
49
|
|
34
|
50
|
@DeleteMapping("/people{id}")
|
35
|
51
|
void deletePerson(@PathVariable int id){
|
36
|
|
-
|
|
52
|
+ personRepository.delete(id);
|
37
|
53
|
}
|
38
|
54
|
}
|
39
|
55
|
/*
|
|
@@ -47,4 +63,7 @@ PUT /people/{id} - Update the person with id number {id}
|
47
|
63
|
Response: 200 OK if updated, 201 Created if a new entity was created
|
48
|
64
|
DELETE /people/{id} - delete the person with id number {id}
|
49
|
65
|
Response: 204 No Content
|
|
66
|
+
|
|
67
|
+# Enabling H2 Console
|
|
68
|
+spring.h2.console.enabled=true
|
50
|
69
|
*/
|