Skip to content

Commit

Permalink
Merge pull request #35 from Wilcolab/copilot
Browse files Browse the repository at this point in the history
feat: Add GET and DELETE endpoints for comments
  • Loading branch information
OnFreund authored Aug 9, 2024
2 parents d04f535 + 5f569ce commit 1e724c3
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions backend/routes/api/comments.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
const router = require("express").Router();
const mongoose = require("mongoose");

const Comment = mongoose.model("Comment");

module.exports = router;

/**
* GET all comments.
* @route GET /api/comments
* @returns {Object} - JSON object containing the comments.
* @throws {Error} - If an error occurs while retrieving the comments.
*/

router.get("/", async (req, res) => {
try {
const comments = await Comment.find();
res.json({ comments });
} catch (err) {
console.error(err);
}
});

/**
* DELETE a comment by ID.
* @route DELETE /api/comments/:id
* @param {string} id - The ID of the comment to be deleted.
* @returns {Object} - JSON object indicating the success of the deletion.
* @throws {Error} - If an error occurs while deleting the comment.
*/

router.delete("/:id", async (req, res) => {
try {
await Comment.findByIdAndRemove(req.params.id);
res.json({ success: true });
} catch (err) {
console.error(err);
}
});

0 comments on commit 1e724c3

Please sign in to comment.