Browse Source

completed lab

mpierse 5 years ago
parent
commit
ad6cb4dfc4

+ 35
- 0
src/main/java/io/zipcoder/crudapp/Person.java View File

@@ -1,12 +1,47 @@
1 1
 package io.zipcoder.crudapp;
2 2
 
3
+
4
+
3 5
 import javax.persistence.Entity;
6
+import javax.persistence.GeneratedValue;
7
+import javax.persistence.GenerationType;
8
+import javax.persistence.Id;
9
+import java.io.Serializable;
4 10
 
5 11
 @Entity
6 12
 public class Person {
7 13
 
14
+    @Id
15
+    @GeneratedValue(strategy = GenerationType.AUTO)
8 16
     Long id;
9 17
     String firstName;
10 18
     String lastName;
11 19
 
20
+    public Person() {
21
+    }
22
+
23
+    public Long getId() {
24
+        return id;
25
+    }
26
+
27
+    public void setId(Long id) {
28
+        this.id = id;
29
+    }
30
+
31
+    public String getFirstName() {
32
+        return firstName;
33
+    }
34
+
35
+    public void setFirstName(String firstName) {
36
+        this.firstName = firstName;
37
+    }
38
+
39
+    public String getLastName() {
40
+        return lastName;
41
+    }
42
+
43
+    public void setLastName(String lastName) {
44
+        this.lastName = lastName;
45
+    }
46
+
12 47
 }

+ 24
- 9
src/main/java/io/zipcoder/crudapp/PersonController.java View File

@@ -1,34 +1,49 @@
1 1
 package io.zipcoder.crudapp;
2 2
 
3
+import org.springframework.beans.factory.annotation.Autowired;
4
+import org.springframework.http.HttpStatus;
5
+import org.springframework.http.ResponseEntity;
3 6
 import org.springframework.web.bind.annotation.*;
4 7
 
8
+import java.util.ArrayList;
9
+import java.util.Iterator;
5 10
 import java.util.List;
6 11
 
7 12
 @RestController
8 13
 public class PersonController {
9 14
 
15
+    @Autowired
16
+    PersonRepository personRepository;
17
+
10 18
     @RequestMapping(value = "/people", method = RequestMethod.POST)
11
-    public Person createPerson(Person p){
12
-        return p;
19
+    public ResponseEntity<Person> createPerson(@RequestBody Person p){
20
+        personRepository.save(p);
21
+        return new ResponseEntity<>(p,HttpStatus.CREATED);
13 22
     }
14 23
 
15 24
 
16 25
     @RequestMapping(value = "/people/{id}", method = RequestMethod.GET)
17
-    public Person getPerson(@PathVariable("id")int id){
18
-            return null;
26
+    public ResponseEntity<Person> getPerson(@PathVariable("id")int id){
27
+       Person p = personRepository.findOne((long)id);
28
+       return new ResponseEntity<>(p, HttpStatus.OK);
19 29
     }
20 30
 
21 31
     @RequestMapping(value = "/people",method = RequestMethod.GET)
22
-    public List<Person> getPersonList(){
23
-        return null;
32
+    public ResponseEntity<Iterable<Person>> getPersonList(){
33
+        Iterable<Person> ppl = personRepository.findAll();
34
+        return new ResponseEntity<>(ppl, HttpStatus.OK);
24 35
     }
25 36
 
26 37
     @RequestMapping(value = "/people/{id}", method = RequestMethod.PUT)
27
-    public Person updatePerson(Person p){
28
-        return p;
38
+    public ResponseEntity<Person> updatePerson(@RequestBody Person p){
39
+        personRepository.save(p);
40
+        return new ResponseEntity<>(p, HttpStatus.OK);
29 41
     }
30 42
 
31 43
     @RequestMapping(value = "/people/{id}", method = RequestMethod.DELETE)
32
-    public void deletePerson(@PathVariable("id")int id){}
44
+    public ResponseEntity<Person> deletePerson(@PathVariable("id")int id){
45
+        personRepository.delete((long)id);
46
+        return new ResponseEntity<>( HttpStatus.NO_CONTENT);
47
+    }
33 48
 
34 49
 }

+ 9
- 0
src/main/java/io/zipcoder/crudapp/PersonRepository.java View File

@@ -0,0 +1,9 @@
1
+package io.zipcoder.crudapp;
2
+
3
+import org.springframework.data.repository.CrudRepository;
4
+
5
+public interface PersonRepository extends CrudRepository<Person, Long> {
6
+
7
+
8
+
9
+}

+ 5
- 0
src/main/java/io/zipcoder/crudapp/PersonService.java View File

@@ -0,0 +1,5 @@
1
+package io.zipcoder.crudapp;
2
+
3
+public class PersonService {
4
+
5
+}