From 87aefd98de1904b130863ed87a055b704130d79a Mon Sep 17 00:00:00 2001 From: On Freund Date: Tue, 9 Apr 2024 12:32:41 +0000 Subject: [PATCH] Add GET and DELETE routes for comments --- backend/routes/api/comments.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/backend/routes/api/comments.js b/backend/routes/api/comments.js index 47b0bc591..77e0aa18a 100644 --- a/backend/routes/api/comments.js +++ b/backend/routes/api/comments.js @@ -3,3 +3,31 @@ const mongoose = require("mongoose"); const Comment = mongoose.model("Comment"); module.exports = router; + +router.get("/", async (req, res) => { + const comments = await Comment.find(); + res.json(comments); + } +); + +router.delete("/:id", async (req, res) => { + try { + /** + * Represents a comment object. + * @typedef {Object} Comment + * @property {string} id - The unique identifier of the comment. + * @property {string} content - The content of the comment. + * @property {string} author - The author of the comment. + * @property {Date} createdAt - The date and time when the comment was created. + * @property {Date} updatedAt - The date and time when the comment was last updated. + */ + const comment = await Comment.findById(req.params.id); + if (!comment) { + return res.status(404).json({ message: "Comment not found" }); + } + await comment.remove(); + res.json({ message: "Comment removed" }); + } catch (error) { + res.status(500).json({ message: "Internal server error" }); + } +});