From 4bde649538496f0de9662add2722c364c7827ffb Mon Sep 17 00:00:00 2001 From: On Freund Date: Wed, 7 Aug 2024 15:47:12 +0000 Subject: [PATCH] feat: Add GET and DELETE endpoints for comments --- backend/routes/api/comments.js | 38 ++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/backend/routes/api/comments.js b/backend/routes/api/comments.js index 47b0bc591..dd5416e27 100644 --- a/backend/routes/api/comments.js +++ b/backend/routes/api/comments.js @@ -3,3 +3,41 @@ const mongoose = require("mongoose"); const Comment = mongoose.model("Comment"); module.exports = router; + +/** + * Retrieves all comments. + * + * @route GET /api/comments + * @returns {Array} An array of comments. + * @throws {Error} If there is an error retrieving the comments. + */ + + +router.get("/", (req, res) => { + Comment.find() + .then((comments) => { + res.json(comments); + }) + .catch((err) => { + console.error(err); + res.sendStatus(500); + }); +} +/** + * Deletes a comment by ID. + * + * @route DELETE /api/comments/:id + * @param {string} id - The ID of the comment to delete. + * @returns {number} HTTP status code 204 if the comment is deleted successfully. + * @throws {Error} If there is an error deleting the comment. + */ + +router.delete("/:id", async (req, res) => { + try { + await Comment.findByIdAndDelete(req.params.id); + res.sendStatus(204); + } catch (err) { + console.error(err); + res.sendStatus(500); + } +});