From 991fd2acb7e630768a0a4a8e932cc1f41739ad6f Mon Sep 17 00:00:00 2001 From: On Freund Date: Fri, 13 Dec 2024 13:18:39 +0000 Subject: [PATCH] Add comment routes for fetching and deleting comments --- backend/routes/api/comments.js | 45 ++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/backend/routes/api/comments.js b/backend/routes/api/comments.js index 47b0bc591..3a6ef63d3 100644 --- a/backend/routes/api/comments.js +++ b/backend/routes/api/comments.js @@ -1,5 +1,50 @@ +/** + * Express router providing comment related routes. + * @module routes/api/comments + */ + const router = require("express").Router(); const mongoose = require("mongoose"); const Comment = mongoose.model("Comment"); +/** + * Route to get all comments. + * @name get/ + * @function + * @memberof module:routes/api/comments + * @inner + * @param {Object} req - Express request object + * @param {Object} res - Express response object + * @returns {JSON} - A JSON object containing all comments + */ + +/** + * Route to delete a comment by ID. + * @name delete/:id + * @function + * @memberof module:routes/api/comments + * @inner + * @param {Object} req - Express request object + * @param {Object} res - Express response object + * @returns {JSON} - A JSON object indicating success + */ + module.exports = router; + +router.get("/", async (req, res) => { + try { + const comments = await Comment.find(); + res.json({ comments }); + } catch (err) { + console.log(err); + } +}); + +router.delete("/:id", async (req, res) => { + try { + await Comment.findByIdAndDelete(req.params.id); + res.json({ success: true }); + } catch (err) { + console.log(err); + } +}); \ No newline at end of file