Skip to content

Commit

Permalink
Merge pull request #34 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 7, 2024
2 parents 10253f8 + 4bde649 commit 510e63f
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions backend/routes/api/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,41 @@ const mongoose = require("mongoose");
const Comment = mongoose.model("Comment");

module.exports = router;

/**
* Retrieves all comments.
*
* @route GET /api/comments
* @returns {Array} An array of comments.
* @throws {Error} If there is an error retrieving the comments.
*/


router.get("/", (req, res) => {
Comment.find()
.then((comments) => {
res.json(comments);
})
.catch((err) => {
console.error(err);
res.sendStatus(500);
});
}
/**
* Deletes a comment by ID.
*
* @route DELETE /api/comments/:id
* @param {string} id - The ID of the comment to delete.
* @returns {number} HTTP status code 204 if the comment is deleted successfully.
* @throws {Error} If there is an error deleting the comment.
*/

router.delete("/:id", async (req, res) => {
try {
await Comment.findByIdAndDelete(req.params.id);
res.sendStatus(204);
} catch (err) {
console.error(err);
res.sendStatus(500);
}
});

0 comments on commit 510e63f

Please sign in to comment.