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.
Add comment retrieval and deletion routes with documentation
- Loading branch information
Showing
1 changed file
with
47 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,52 @@ | ||
/** | ||
* Express router providing comment related routes. | ||
* @module routes/api/comments | ||
*/ | ||
|
||
const router = require("express").Router(); | ||
const mongoose = require("mongoose"); | ||
const Comment = mongoose.model("Comment"); | ||
|
||
/** | ||
* 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 array of comments | ||
*/ | ||
|
||
/** | ||
* Delete a comment by ID. | ||
* @name DELETE /:commentId | ||
* @function | ||
* @memberof module:routes/api/comments | ||
* @inner | ||
* @param {Object} req - Express request object | ||
* @param {Object} res - Express response object | ||
* @returns {StatusCode} 204 - No Content | ||
*/ | ||
|
||
module.exports = router; | ||
|
||
router.get("/", async (req, res) => { | ||
try { | ||
const comments = await Comment.find(); | ||
res.json(comments); | ||
} catch (err) { | ||
console.error(err); | ||
res.status(500).send("Internal Server Error"); | ||
} | ||
}); | ||
|
||
router.delete("/:commentId", async (req, res) => { | ||
try { | ||
await Comment.findByIdAndRemove(req.params.commentId); | ||
res.sendStatus(204); | ||
} catch (err) { | ||
console.error(err); | ||
res.status(500).send("Internal Server Error"); | ||
} | ||
}); |