Skip to content

Commit

Permalink
feat: Add GET and DELETE endpoints for comments
Browse files Browse the repository at this point in the history
  • Loading branch information
OnFreund committed Jun 23, 2024
1 parent e8d18f5 commit 259d8d0
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions backend/routes/api/comments.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,40 @@
/**
* Express router for handling comments.
* @type {import("express").Router}
*/
const router = require("express").Router();
const mongoose = require("mongoose");
const Comment = mongoose.model("Comment");

module.exports = router;

/**
* GET /api/comments
* Retrieves all comments.
* @param {import("express").Request} req - The request object.
* @param {import("express").Response} res - The response object.
*/
router.get("/", (req, res) => {
Comment.find()
.then(comments => {
res.json({ comments });
})
.catch(err => {
console.log(err);
});
});

/**
* DELETE /api/comments/:id
* Deletes a comment by ID.
* @param {import("express").Request} req - The request object.
* @param {import("express").Response} res - The response object.
*/
router.delete("/:id", async (req, res) => {
try {
await Comment.findByIdAndRemove(req.params.id);
res.json({ success: true });
} catch (err) {
console.log(err);
}
});

0 comments on commit 259d8d0

Please sign in to comment.