Bladeren bron

part 2 done

Whitney Martinez 5 jaren geleden
bovenliggende
commit
db0a06d8eb

+ 9
- 9
README.md Bestand weergeven

@@ -4,26 +4,26 @@ This repository provides a starter project for a CRUD application using Spring B
4 4
 
5 5
 ## Exercise
6 6
 
7
-Using this project as a starter, you will create a Person class as an entity to persist to H2 with an autogenerated id, and provide access to CRUD operations on those entities using a Spring Rest Controller supporting `GET`, `PUT`, `POST`, and `DELETE` operations. Spring Boot will handle the actual HTTP traffic and database components for you, you just need to build the last few pieces of the puzzle described in this lab.
7
+Using this project as a starter, you will create a Model.Person class as an entity to persist to H2 with an autogenerated id, and provide access to CRUD operations on those entities using a Spring Rest Controller supporting `GET`, `PUT`, `POST`, and `DELETE` operations. Spring Boot will handle the actual HTTP traffic and database components for you, you just need to build the last few pieces of the puzzle described in this lab.
8 8
 
9 9
 As you are working on this lab, you can demo the behavior of your `GET` endpoints using your browser. For `PUT`, `POST`, and `DELETE` you will need a more robust tool, such as [Postman](https://www.getpostman.com/) or [curl](https://curl.haxx.se/).
10 10
 
11 11
 ### Part 1:
12 12
 
13
-Create a `Person` class with fields for first name, last name, and an id number.
13
+Create a `Model.Person` class with fields for first name, last name, and an id number.
14 14
 
15
-Create a `PersonController` class with `Person createPerson(Person p)`, `Person getPerson(int id)`, `List<Person> getPersonList()`, `Person updatePerson(Person p)`, and `void DeletePerson(int id)` methods, and let it track a list of Person objects.
15
+Create a `PersonController` class with `Model.Person createPerson(Model.Person p)`, `Model.Person getPerson(int id)`, `List<Model.Person> getPersonList()`, `Model.Person updatePerson(Model.Person p)`, and `void DeletePerson(int id)` methods, and let it track a list of Model.Person objects.
16 16
 
17
-Add the `@RestController` annotation to your `PersonController` class, and using the "Endpoints" list in the Reference section below, add the appropriate `@RequestMapping` annotations to each of your methods. Endpoints should be at `/people` and `/people/{id}` as appropriate. You will have to use `@PathVariable` for id numbers in the URI and `@RequestBody` for Person objects sent in the requests.
17
+Add the `@RestController` annotation to your `PersonController` class, and using the "Endpoints" list in the Reference section below, add the appropriate `@RequestMapping` annotations to each of your methods. Endpoints should be at `/people` and `/people/{id}` as appropriate. You will have to use `@PathVariable` for id numbers in the URI and `@RequestBody` for Model.Person objects sent in the requests.
18 18
 
19 19
 
20 20
 ### Part 2: 
21 21
 
22
-Add the `@Entity` and `@Id` annotations to your Person class as shown in the Reference section. These tell Spring how to convert your Person objects to database entities when you pass them to a repository.
22
+Add the `@Entity` and `@Id` annotations to your Model.Person class as shown in the Reference section. These tell Spring how to convert your Model.Person objects to database entities when you pass them to a repository.
23 23
 
24
-Create a `PersonRepository` interface that extends the `CrudRepository` interface. Be sure to specify the `Person` type parameter on `CrudRepository<>`. You will not need to implement this interface as Spring automatically generates an implementation at runtime.
24
+Create a `PersonRepository` interface that extends the `CrudRepository` interface. Be sure to specify the `Model.Person` type parameter on `CrudRepository<>`. You will not need to implement this interface as Spring automatically generates an implementation at runtime.
25 25
 
26
-Update your controller logic to use the `PersonRepository` instead of manually tracking Person objects in a list. You will need a `PersonRepository` field marked with the `@Autowired` annotation -- again, Spring will provide an implementation here automatically. You will need to use the `findAll()`, `findOne(id)`, `save(Person)` and `delete(id)` methods of `PersonRepository` to fetch and save Person objects.
26
+Update your controller logic to use the `PersonRepository` instead of manually tracking Model.Person objects in a list. You will need a `PersonRepository` field marked with the `@Autowired` annotation -- again, Spring will provide an implementation here automatically. You will need to use the `findAll()`, `findOne(id)`, `save(Model.Person)` and `delete(id)` methods of `PersonRepository` to fetch and save Model.Person objects.
27 27
 
28 28
 ### Part 3:
29 29
 
@@ -46,11 +46,11 @@ Now that your CRUD application is working, it's time to make sure the correct HT
46 46
 - `DELETE /people/{id}` - delete the person with id number `{id}`
47 47
   - Response: `204 No Content`
48 48
 
49
-**Person class and  ID configuration for H2**:
49
+**Model.Person class and  ID configuration for H2**:
50 50
 
51 51
 ```Java
52 52
 @Entity
53
-public class Person {
53
+public class Model.Person {
54 54
 
55 55
     @Id
56 56
     @GeneratedValue(strategy = GenerationType.AUTO)

+ 62
- 0
src/main/java/Controllers/PersonController.java Bestand weergeven

@@ -0,0 +1,62 @@
1
+package Controllers;
2
+
3
+import Model.Person;
4
+import Repository.PersonRepository;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.http.HttpStatus;
7
+import org.springframework.http.RequestEntity;
8
+import org.springframework.http.ResponseEntity;
9
+import org.springframework.web.bind.annotation.*;
10
+
11
+@RestController
12
+public class PersonController {
13
+
14
+    private final PersonRepository personRepository;
15
+    @Autowired
16
+    public PersonController(PersonRepository personRepository) {
17
+        this.personRepository = personRepository;
18
+    }
19
+
20
+    @RequestMapping(value = "/person",method = RequestMethod.GET)
21
+    public ResponseEntity<Iterable<Person>> getPersonList(){
22
+        Iterable<Person> listOfPeople = personRepository.findAll();
23
+        return new ResponseEntity<>(listOfPeople, HttpStatus.OK);
24
+    }
25
+
26
+    @RequestMapping(value = "/person",method = RequestMethod.POST)
27
+    public ResponseEntity<Person> createPerson(@RequestBody Person p){
28
+        return new ResponseEntity<>(this.personRepository.save(p),HttpStatus.CREATED);
29
+    }
30
+
31
+    @RequestMapping(value = "/person/{id}", method = RequestMethod.GET)
32
+    public ResponseEntity<Person> getPerson(@PathVariable Long id){
33
+
34
+        if(this.personRepository.exists(id)){
35
+            return new ResponseEntity<>(this.personRepository.findOne(id), HttpStatus.OK);
36
+        }else{
37
+            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
38
+        }
39
+
40
+    }
41
+
42
+    @RequestMapping(value = "/person/{id}", method = RequestMethod.PUT)
43
+    public ResponseEntity<Person> updatePerson(@PathVariable Long id,@RequestBody Person p){
44
+
45
+        if(!this.personRepository.exists(id)){
46
+            return new ResponseEntity<>(this.personRepository.findOne(id), HttpStatus.OK);
47
+
48
+        }else{
49
+            Person people = personRepository.save(p);
50
+            people.setFirstName(p.getFirstName());
51
+            people.setLastName(p.getLastName());
52
+            return new ResponseEntity<>(this.personRepository.findOne(id),HttpStatus.OK);
53
+
54
+        }
55
+
56
+    }
57
+
58
+    @RequestMapping(value = "/person/{id}",method = RequestMethod.DELETE)
59
+    public void DeletePerson(@PathVariable Long id) {
60
+    }
61
+
62
+}

+ 37
- 0
src/main/java/Model/Person.java Bestand weergeven

@@ -0,0 +1,37 @@
1
+package Model;
2
+
3
+import javax.persistence.*;
4
+
5
+@Entity
6
+public class Person {
7
+
8
+    @Id
9
+    @GeneratedValue
10
+    Long id;
11
+    String firstName;
12
+    String lastName;
13
+
14
+    public Long getId() {
15
+        return id;
16
+    }
17
+
18
+    public void setId(Long id) {
19
+        this.id = id;
20
+    }
21
+
22
+    public String getFirstName() {
23
+        return firstName;
24
+    }
25
+
26
+    public void setFirstName(String firstName) {
27
+        this.firstName = firstName;
28
+    }
29
+
30
+    public String getLastName() {
31
+        return lastName;
32
+    }
33
+
34
+    public void setLastName(String lastName) {
35
+        this.lastName = lastName;
36
+    }
37
+}

+ 9
- 0
src/main/java/Repository/PersonRepository.java Bestand weergeven

@@ -0,0 +1,9 @@
1
+package Repository;
2
+
3
+import Model.Person;
4
+import org.springframework.beans.factory.annotation.Autowired;
5
+import org.springframework.data.repository.CrudRepository;
6
+
7
+
8
+public interface PersonRepository extends CrudRepository <Person,Long> {
9
+}

+ 3
- 3
src/main/resources/static/Requests.html Bestand weergeven

@@ -31,11 +31,11 @@
31 31
       </section>
32 32
       <section>
33 33
         <h2>GET person by id</h2>
34
-        <label for="getPersonId">Person ID:</label><input type="number" name="getPersonId" value="1" id="getPersonId"><br>
34
+        <label for="getPersonId">Model.Person ID:</label><input type="number" name="getPersonId" value="1" id="getPersonId"><br>
35 35
         <button type="button" name="submitGet" onclick="getPerson()">Submit GET</button>
36 36
       </section>
37 37
       <section>
38
-        <h2>POST new Person:</h2>
38
+        <h2>POST new Model.Person:</h2>
39 39
 
40 40
         <label for="firstName">First Name:</label><input type="text" name="firstName" value="foo" id="postPersonFirstName"><br>
41 41
         <label for="lastName">Last Name:</label><input type="text" name="lastName" value="bar" id="postPersonLastName"><br>
@@ -52,7 +52,7 @@
52 52
 
53 53
       <section>
54 54
         <h2>DELETE person by ID</h2>
55
-        <label for="delPersonId">Person ID:</label><input type="number" name="delPersonId" value="1" id="deletePersonId"><br>
55
+        <label for="delPersonId">Model.Person ID:</label><input type="number" name="delPersonId" value="1" id="deletePersonId"><br>
56 56
         <button type="button" name="submitDelete" onclick="deletePerson()">Submit DELETE</button>
57 57
       </section>
58 58
     </section>