generated from trywilco/Anythink-Market-Base
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from Wilcolab/copilot
feat: Add GET and DELETE endpoints for comments
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,39 @@ | ||
const router = require("express").Router(); | ||
const mongoose = require("mongoose"); | ||
|
||
const Comment = mongoose.model("Comment"); | ||
|
||
module.exports = router; | ||
|
||
/** | ||
* GET all comments. | ||
* @route GET /api/comments | ||
* @returns {Object} - JSON object containing the comments. | ||
* @throws {Error} - If an error occurs while retrieving the comments. | ||
*/ | ||
|
||
router.get("/", async (req, res) => { | ||
try { | ||
const comments = await Comment.find(); | ||
res.json({ comments }); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
}); | ||
|
||
/** | ||
* DELETE a comment by ID. | ||
* @route DELETE /api/comments/:id | ||
* @param {string} id - The ID of the comment to be deleted. | ||
* @returns {Object} - JSON object indicating the success of the deletion. | ||
* @throws {Error} - If an error occurs while deleting the comment. | ||
*/ | ||
|
||
router.delete("/:id", async (req, res) => { | ||
try { | ||
await Comment.findByIdAndRemove(req.params.id); | ||
res.json({ success: true }); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
}); |