Ver código fonte

CRUDApplication

Person
PersonController
PersonRepository
PersonService

Working Part 2
Nick Satinover 5 anos atrás
pai
commit
9612056340

+ 1
- 1
src/main/java/io/zipcoder/crudapp/CRUDApplication.java Ver arquivo

@@ -5,7 +5,7 @@ import org.springframework.boot.SpringApplication;
5 5
 import org.springframework.boot.autoconfigure.SpringBootApplication;
6 6
 import org.springframework.boot.web.servlet.ServletRegistrationBean;
7 7
 import org.springframework.context.annotation.Bean;
8
-
8
+// jdbc:h2:~/test
9 9
 @SpringBootApplication
10 10
 public class CRUDApplication {
11 11
 

+ 7
- 7
src/main/java/io/zipcoder/crudapp/Person.java Ver arquivo

@@ -8,7 +8,7 @@ import javax.persistence.Id;
8 8
 @Entity
9 9
 public class Person {
10 10
     private String firstName;
11
-    private String lastname;
11
+    private String lastName;
12 12
     @Id
13 13
     @GeneratedValue(strategy = GenerationType.AUTO)
14 14
     private int id;
@@ -17,7 +17,7 @@ public class Person {
17 17
 
18 18
     public Person(String firstName, String lastName) {
19 19
         this.firstName = firstName;
20
-        this.lastname = lastName;
20
+        this.lastName = lastName;
21 21
     }
22 22
 
23 23
     public String getFirstName() {
@@ -28,12 +28,12 @@ public class Person {
28 28
         this.firstName = firstName;
29 29
     }
30 30
 
31
-    public String getLastname() {
32
-        return lastname;
31
+    public String getLastName() {
32
+        return lastName;
33 33
     }
34 34
 
35
-    public void setLastname(String lastname) {
36
-        this.lastname = lastname;
35
+    public void setLastName(String lastName) {
36
+        this.lastName = lastName;
37 37
     }
38 38
 
39 39
     public int getId() {
@@ -44,7 +44,7 @@ public class Person {
44 44
     public String toString() {
45 45
         return "Person{" +
46 46
                 "firstName='" + firstName + '\'' +
47
-                ", lastname='" + lastname + '\'' +
47
+                ", lastName='" + lastName + '\'' +
48 48
                 ", id=" + id +
49 49
                 '}';
50 50
     }

+ 30
- 11
src/main/java/io/zipcoder/crudapp/PersonController.java Ver arquivo

@@ -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
  */

+ 47
- 1
src/main/java/io/zipcoder/crudapp/PersonService.java Ver arquivo

@@ -3,11 +3,57 @@ package io.zipcoder.crudapp;
3 3
 import org.springframework.beans.factory.annotation.Autowired;
4 4
 import org.springframework.stereotype.Service;
5 5
 
6
+import java.util.List;
7
+import java.util.concurrent.CopyOnWriteArrayList;
8
+
6 9
 @Service
7 10
 public class PersonService {
8 11
 
9 12
     @Autowired
10 13
     PersonRepository personRepository;
11 14
 
12
-    
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
+
13 59
 }

+ 1
- 1
src/main/resources/application.properties Ver arquivo

@@ -1,3 +1,3 @@
1 1
 spring.profiles.active=h2
2 2
 
3
-spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
3
+spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect