Step 10
Here is the handler for the HTTP DELETE request through /api/notes/:courseId
endpoint:
router.delete("/api/notes/:id", (req, res) => {
const id = req.params.id;
notes
.delete(id)
.then((note) =>
note
? res.json({ data: note })
: errorHandler(res, 404, "Resource not found")
)
.catch((err) => {
errorHandler(res, 400, err);
});
});
Notice, we account for the case where a given ID does not exist in our database:
- The
notes.delete
operation will returnnull
- We capture this by responding with a
404
error:note ? res.json({ data: note }) : errorHandler(res, 404, "Resource not found")
Similarly, here is the handler for the HTTP PUT request through /api/notes/:courseId
endpoint:
router.put("/api/notes/:id", (req, res) => {
const id = req.params.id;
const content = req.body.content;
const author = req.body.author;
notes
.update(id, content, author)
.then((note) =>
note
? res.json({ data: note })
: errorHandler(res, 404, "Resource not found")
)
.catch((err) => errorHandler(res, 400, err));
});
We have now implemented all the route handlers.