PUT Course
Let's include an endpoint to update a course.
Update Course | |
---|---|
HTTP Method | PUT |
API Endpoint | /api/courses/:offeringName |
Request Path Parameter | offeringName |
Request Query Parameter | |
Request Body | JSON object (course attributes) |
Response Body | JSON object (updated course) |
Response Status | 200 |
Notice the path (endpoint) is similar to the DELETE request. Additionally, the client is expected to provide the attributes (offeringName & title) for updating a course in the body of the request, like in a POST request. By convention, the client provides all the attributes even if only one is being updated. Also by convention, we return the updated course (with status code 200).
As part of HW5 you should write unit tests for testing this API endpoint!
Here are a few Postman requests to guide your efforts in writing the tests:
Finally, here is the route method that must be added to ApiServer.main
:
put("/api/courses/:offeringName", (req, res) -> {
try {
String offeringName = req.params("offeringName");
Course course = gson.fromJson(req.body(), Course.class);
if (!course.getOfferingName().equals(offeringName)) {
throw new ApiError("offering name does not match the resource identifier", 400);
}
course = courseDao.update(course.getOfferingName(), course.getTitle());
if (course == null) {
throw new ApiError("Resource not found", 404);
}
return gson.toJson(course);
} catch (DaoException | JsonSyntaxException ex) {
throw new ApiError(ex.getMessage(), 500);
}
});
Once you have written unit tests, run them in IntelliJ to ensure they pass. Moreover, run the Postman requests to ensure the API behaves as expected.