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.
feat: Add GET and DELETE endpoints for comments
- Loading branch information
Showing
1 changed file
with
35 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,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); | ||
} | ||
}); |