Admin delete comment function

This commit is contained in:
Rykkel
2024-08-12 22:11:04 +08:00
parent 0f005a1bb6
commit c025163c55
3 changed files with 151 additions and 4 deletions

View File

@@ -126,7 +126,7 @@ router.get("/", async (req, res) => {
if (tag) {
condition.include[0].where = { tag: tag };
}
// You can add condition for other columns here
// e.g. condition.columnName = value;
@@ -325,6 +325,31 @@ router.get("/:id/getComments", async (req, res) => {
res.json(comments);
});
router.delete("/comments/:id", async (req, res) => {
let id = req.params.id;
console.log("Deleting comment with ID: ", id)
// Check id not found
let comment = await Comment.findByPk(id);
if (!comment) {
res.sendStatus(404);
return;
}
let num = await Comment.destroy({
// destroy() deletes data based on the where condition, and returns the number of rows affected
where: { id: id },
});
if (num == 1) {
// destroy() returns no. of rows affected, that's why if num == 1
res.json({
message: "Comment was deleted successfully.",
});
} else {
res.status(400).json({
message: `Cannot delete comment with id ${id}.`,
});
}
});
router.get("/tags/all", async (req, res) => {
// Check id not found
let tags = await Tag.findAll();