Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add comment routes for fetching and deleting comments #36

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions backend/routes/api/comments.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,50 @@
/**
* Express router providing comment related routes.
* @module routes/api/comments
*/

const router = require("express").Router();
const mongoose = require("mongoose");
const Comment = mongoose.model("Comment");

/**
* Route to get all comments.
* @name get/
* @function
* @memberof module:routes/api/comments
* @inner
* @param {Object} req - Express request object
* @param {Object} res - Express response object
* @returns {JSON} - A JSON object containing all comments
*/

/**
* Route to delete a comment by ID.
* @name delete/:id
* @function
* @memberof module:routes/api/comments
* @inner
* @param {Object} req - Express request object
* @param {Object} res - Express response object
* @returns {JSON} - A JSON object indicating success
*/

module.exports = router;

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

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