You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+62-29
Original file line number
Diff line number
Diff line change
@@ -24,8 +24,7 @@ database are not yet implemented.
24
24
The application source contains the following directories:
25
25
26
26
-`src/main/java/org.example/models` - Contains the model classes that represent
27
-
the data in the database. Currently, there is only one model class, `User`,
28
-
which represents a user from the database `users` table.
27
+
the data in the database. *You will not need to create any new model classes.*
29
28
-`src/main/java/org.example/daos` - Contains the data access objects that
30
29
interact with the database. Currently, there is only one DAO class, `UserDao`,
31
30
which interacts with the `users` table.
@@ -63,7 +62,6 @@ directory. You will need to set the following properties:
63
62
-`spring.datasource.username` - The username for your mysql server.
64
63
-`spring.datasource.password` - The password for your mysql server.
65
64
66
-
67
65
### Running the Application
68
66
69
67
To run the application, you can right-click on the `SpringBootApplication`
@@ -109,21 +107,16 @@ until you implement the REST endpoints.
109
107
## Exercise
110
108
111
109
Your task is to implement the REST endpoints that allow users to interact with
112
-
the store. You will need to create a new model class, DAO class, and controller
110
+
the store. You will need to create a DAO class and a controller
113
111
class for each of the tables in the database.
114
112
115
113
Stop the application and follow the steps below to complete the exercise.
116
114
117
-
### Step 1: Create the Model Classes
118
-
119
-
In the `models` folder, create a new model class for each of the tables in the
120
-
database (other than the `users` and `roles` tables). Each model class should
121
-
contain a field for each column in the table, and should contain getters and
122
-
setters for each field. You should also create a constructor that takes all of
123
-
the fields as arguments.
115
+
### Step 1: Review the Model Classes
124
116
125
-
You can refer to the existing `User` model class for an example of what these
126
-
classes should look like.
117
+
In the `models` folder, notice that there is a class for each of the tables in
118
+
the database. You will need to use these classes to interact with the database
119
+
in the DAO classes and controller classes.
127
120
128
121
### Step 2: Create the DAO Classes
129
122
@@ -166,52 +159,93 @@ public class ProductDao {
166
159
}
167
160
```
168
161
162
+
Each DAO class will also need a mapping method that maps a `ResultSet` object
163
+
to a model object so that your `SELECT` queries can map the results to the
164
+
appropriate model objects. You can refer to the `UserDao` class for an example
165
+
of what this method should look like.
166
+
169
167
### Step 3: Create the Controller Classes
170
168
171
169
Create a new controller class for each of the tables in the database (other
172
170
than the `users` and `roles` tables). Each controller class should contain
173
171
methods providing REST endpoints for creating, reading, updating, and deleting
174
-
items in the table.
172
+
items in the table. These endpoints are detailed below.
175
173
176
174
You can refer to the existing `UserController` and `ProfileController` classes
177
175
for an example of what these classes should look like.
178
176
179
177
Note that each controller class injects its corresponding DAO class through
180
-
the constructor. You should do the same in your controller classes.
178
+
an `@Autowired` class member. You will need to do the same for your controller
179
+
classes.
181
180
182
181
For example:
183
182
184
183
```java
185
184
@RestController
186
185
@RequestMapping("/products")
187
186
publicclassProductController {
187
+
@Autowired
188
188
privateProductDao productDao;
189
189
190
-
publicProductController(ProductDaoproductDao) {
191
-
this.productDao = productDao;
192
-
}
193
-
194
190
// ...
195
191
}
196
192
```
193
+
> [!Note]
194
+
> Your "Get by Id" methods should return a 404 status code if the item is not
195
+
> found in the database. You can do this by throwing a `ResponseStatusException`
196
+
> with a `HttpStatus.NOT_FOUND` status code. (Refer to the `UserController` class
197
+
> for an example of how to do this.)
197
198
198
199
When you are finished, you should have implemented all of the REST endpoints
199
200
in the Postman collection, and they should all function properly and return
200
201
appropriate data.
201
202
202
-
## Testing / Verification
203
+
### Product Endpoints
204
+
205
+
-`GET /products` - Retrieves all products.
206
+
-`GET /products/{id}` - Retrieves a product by the id in the path, return a 404 NOT FOUND status code if the product is not found.
207
+
-`POST /products` - Creates a new product from the request body and returns the created product with a 201 CREATED http status code.
208
+
-`PUT /products/{id}` - Updates an existing product from the request body and returns the updated product, return a 404 NOT FOUND status code if the product is not found.
209
+
-`DELETE /products/{id}` - Deletes a product by the id in the path and returns the number of rows affected, return a 404 NOT FOUND status code if the product is not found.
210
+
211
+
### Order Endpoints
212
+
213
+
-`GET /orders` - Retrieves all orders.
214
+
-`GET /orders/{id}` - Retrieves an order by the id in the path, return a 404 NOT FOUND status code if the order is not found.
215
+
-`POST /orders` - Creates a new order from the request body and returns the created order with a 201 CREATED http status code.
216
+
-`PUT /orders/{id}` - Updates an existing order from the request body and returns the updated order, return a 404 NOT FOUND status code if the order is not found.
217
+
-`DELETE /orders/{id}` - Deletes an order by the id in the path and returns the number of rows affected, return a 404 NOT FOUND status code if the order is not found.
218
+
219
+
### Order Item Endpoints
220
+
221
+
-`GET /order-items` - Retrieves all order items.
222
+
-`GET /order-items/{id}` - Retrieves an order item by the id in the path, return a 404 NOT FOUND status code if the order item is not found.
223
+
-`POST /order-items` - Creates a new order item from the request body and returns the created order item with a CREATED http 201 status code.
224
+
-`PUT /order-items/{id}` - Updates an existing order item from the request body and returns the updated order item, return a 404 NOT FOUND status code if the order item is not found.
225
+
-`DELETE /order-items/{id}` - Deletes an order item by the id in the path and returns the number of rows affected, return a 404 NOT FOUND status code if the order item is not found.
226
+
227
+
## Testing with Postman
203
228
204
229
You can verify that the application is working correctly by running the
205
230
application and then launching Postman to test the REST endpoints. You should
206
231
be able to create, read, update, and delete items in the database using the
207
232
REST endpoints that you created.
208
233
234
+
## Testing with Included Unit Tests
235
+
236
+
You can also verify that the application is working correctly by running the
237
+
included unit tests. The unit tests are located in the `src/test/java` directory.
238
+
To run the unit tests, right-click on the `src/test/java` directory and select
239
+
"Run All Tests".
240
+
241
+
> [!warning]
242
+
> DO NOT modify the unit tests. Your project will be evaluated based on the
243
+
> unit tests all passing. Modifying the unit tests will result in a failing
244
+
> grade.
245
+
209
246
## Evaluation
210
247
211
-
Your project will be evaluated by the test proctor cloning your repository,
212
-
running the database script, running the application, and executing the
213
-
Postman requests. You will receive full credit if all of the requests execute
214
-
successfully and return the expected data.
248
+
Your project will be evaluated based on the unit tests all passing.
215
249
216
250
## Bonus Steps
217
251
@@ -222,13 +256,13 @@ controller classes to require that users be authenticated in order to access
222
256
the endpoints. You can use the `isAuthenticated()` expression to require that
223
257
users be authenticated.
224
258
225
-
### Bonus Step 2: Constrain Product Creation and Update using `Principal`
259
+
### Bonus Step 2: Constrain Order Creation and Update using `Principal`
226
260
227
261
Add a `Principal` argument to the create and update endpoints for the
228
-
`Product` controller that will allow you to get the username of the user and
229
-
overwrite the `username` field in the passed `Product` object. This will
262
+
`Order` controller that will allow you to get the username of the user and
263
+
overwrite the `username` field in the passed `Order` object. This will
230
264
guaranatee that the `username` field is always set to the username of the
0 commit comments