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