Parcourir la source

PersonController - Complete

Nick Satinover il y a 5 ans
Parent
révision
de215cf767

+ 23
- 39
src/main/java/io/zipcoder/crudapp/PersonController.java Voir le fichier

@@ -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
+}

+ 59
- 59
src/main/java/io/zipcoder/crudapp/PersonService.java Voir le fichier

@@ -1,59 +1,59 @@
1
-package io.zipcoder.crudapp;
2
-
3
-import org.springframework.beans.factory.annotation.Autowired;
4
-import org.springframework.stereotype.Service;
5
-
6
-import java.util.List;
7
-import java.util.concurrent.CopyOnWriteArrayList;
8
-
9
-@Service
10
-public class PersonService {
11
-
12
-    @Autowired
13
-    PersonRepository personRepository;
14
-
15
-    private List<Person> personList = new CopyOnWriteArrayList<Person>();
16
-
17
-    public Person addPerson(Person person){
18
-        personList.add(person);
19
-        return person;
20
-    }
21
-
22
-    public List<Person> getPersonList(){
23
-        return personList;
24
-    }
25
-
26
-    public Person getPerson(int id){
27
-        return personList
28
-                .stream()
29
-                .filter(person -> person.getId() == id)
30
-                .findFirst()
31
-                .get();
32
-    }
33
-
34
-    public Person updatePerson(int id, Person person){
35
-         personList
36
-                .stream()
37
-                .forEach(p -> {
38
-                    if (p.getId() == id){
39
-                        p.setFirstName(person.getLastName());
40
-                        p.setLastName(person.getLastName());
41
-                    }});
42
-         return personList
43
-                 .stream()
44
-                 .filter(person1 -> person1.getId() == id)
45
-                 .findFirst()
46
-                 .get();
47
-        }
48
-
49
-    public void deletePerson(int id){
50
-        personList
51
-                .stream()
52
-                .forEach(person -> {
53
-                    if (person.getId() == id){
54
-                        personList.remove(person);
55
-                    }
56
-                } );
57
-    }
58
-
59
-}
1
+//package io.zipcoder.crudapp;
2
+//
3
+//import org.springframework.beans.factory.annotation.Autowired;
4
+//import org.springframework.stereotype.Service;
5
+//
6
+//import java.util.List;
7
+//import java.util.concurrent.CopyOnWriteArrayList;
8
+//
9
+//@Service
10
+//public class PersonService {
11
+//
12
+//    @Autowired
13
+//    PersonRepository personRepository;
14
+//
15
+//    private List<Person> personList = new CopyOnWriteArrayList<Person>();
16
+//
17
+//    public Person addPerson(Person person){
18
+//        personList.add(person);
19
+//        return person;
20
+//    }
21
+//
22
+//    public List<Person> getPersonList(){
23
+//        return personList;
24
+//    }
25
+//
26
+//    public Person getPerson(int id){
27
+//        return personList
28
+//                .stream()
29
+//                .filter(person -> person.getId() == id)
30
+//                .findFirst()
31
+//                .get();
32
+//    }
33
+//
34
+//    public Person updatePerson(int id, Person person){
35
+//         personList
36
+//                .stream()
37
+//                .forEach(p -> {
38
+//                    if (p.getId() == id){
39
+//                        p.setFirstName(person.getLastName());
40
+//                        p.setLastName(person.getLastName());
41
+//                    }});
42
+//         return personList
43
+//                 .stream()
44
+//                 .filter(person1 -> person1.getId() == id)
45
+//                 .findFirst()
46
+//                 .get();
47
+//        }
48
+//
49
+//    public void deletePerson(int id){
50
+//        personList
51
+//                .stream()
52
+//                .forEach(person -> {
53
+//                    if (person.getId() == id){
54
+//                        personList.remove(person);
55
+//                    }
56
+//                } );
57
+//    }
58
+//
59
+//}