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-related API routes #3

Merged
merged 1 commit into from
Mar 6, 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
43 changes: 43 additions & 0 deletions backend/routes/api/comments.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,48 @@
/**
* Express router for handling comment-related API routes.
* @module routes/api/comments
*/

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

module.exports = router;

/**
* Route for retrieving all comments.
* @name GET /api/comments
* @function
* @async
* @param {Object} req - Express request object
* @param {Object} res - Express response object
* @param {Function} next - Express next middleware function
* @returns {Object} - JSON response containing the retrieved comments
*/
router.get("/", async (req, res, next) => {
try {
const comments = await Comment.find();
return res.json({ comments: comments });
} catch (error) {
next(error);
}
});

/**
* Route for deleting a comment by its ID.
* @name DELETE /api/comments/:id
* @function
* @async
* @param {Object} req - Express request object
* @param {Object} res - Express response object
* @param {Function} next - Express next middleware function
* @returns {Object} - HTTP response with status 200 if successful
*/
router.delete("/:id", async (req, res, next) => {
try {
await Comment.findByIdAndDelete(req.params.id);
return res.sendStatus(200);
} catch (error) {
next(error);
}
});
Loading