From 0a31d73c343b4903b4263a5eb5a1ae072325f447 Mon Sep 17 00:00:00 2001 From: On Freund Date: Tue, 26 Mar 2024 14:17:03 +0000 Subject: [PATCH] Add GET and DELETE routes for comments --- backend/routes/api/comments.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/backend/routes/api/comments.js b/backend/routes/api/comments.js index 47b0bc591..e7c77ee61 100644 --- a/backend/routes/api/comments.js +++ b/backend/routes/api/comments.js @@ -2,4 +2,34 @@ const router = require("express").Router(); const mongoose = require("mongoose"); const Comment = mongoose.model("Comment"); +/** + * GET all comments + * @route GET /api/comments + * @returns {Array} Array of comments + */ +router.get("/", (req, res) => { + Comment.find() + .then((comments) => { + res.json(comments); + }) + .catch((err) => { + res.status(500).json({ error: "Failed to retrieve comments" }); + }); +}); + +/** + * DELETE a comment by ID + * @route DELETE /api/comments/:id + * @param {string} req.params.id - The ID of the comment to delete + * @returns {Object} Empty response with status 204 if successful, or status 500 if an error occurs + */ +router.delete("/:id", async (req, res) => { + try { + await Comment.findByIdAndDelete(req.params.id); + res.status(204).send(); + } catch (err) { + res.status(500).send(); + } +}); + module.exports = router;