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 GET and DELETE routes for comments #19

Merged
merged 1 commit into from
Apr 9, 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
28 changes: 28 additions & 0 deletions backend/routes/api/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,31 @@ const mongoose = require("mongoose");
const Comment = mongoose.model("Comment");

module.exports = router;

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

router.delete("/:id", async (req, res) => {
try {
/**
* Represents a comment object.
* @typedef {Object} Comment
* @property {string} id - The unique identifier of the comment.
* @property {string} content - The content of the comment.
* @property {string} author - The author of the comment.
* @property {Date} createdAt - The date and time when the comment was created.
* @property {Date} updatedAt - The date and time when the comment was last updated.
*/
const comment = await Comment.findById(req.params.id);
if (!comment) {
return res.status(404).json({ message: "Comment not found" });
}
await comment.remove();
res.json({ message: "Comment removed" });
} catch (error) {
res.status(500).json({ message: "Internal server error" });
}
});
Loading