Skip to content

Commit

Permalink
Merge pull request #17 from Wilcolab/copilot
Browse files Browse the repository at this point in the history
Add GET and DELETE routes for comments
  • Loading branch information
OnFreund authored Mar 26, 2024
2 parents e3f03bc + 0a31d73 commit b406763
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions backend/routes/api/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit b406763

Please sign in to comment.