Tag management function

This commit is contained in:
Rykkel
2024-08-12 00:32:10 +08:00
parent 19c8599b2f
commit 672eca530b
5 changed files with 198 additions and 3 deletions

View File

@@ -258,7 +258,7 @@ router.delete("/:id", async (req, res) => {
where: { id: id },
});
if (num == 1) {
// destry() returns no. of rows affected, that's why if num == 1
// destroy() returns no. of rows affected, that's why if num == 1
res.json({
message: "Post was deleted successfully.",
});
@@ -317,4 +317,39 @@ router.get("/:id/getComments", async (req, res) => {
res.json(comments);
});
router.get("/tags/all", async (req, res) => {
// Check id not found
let tags = await Tag.findAll();
if (!tags) {
res.sendStatus(404);
return;
}
res.json(tags);
})
router.delete("/tags/:id", async (req, res) => {
let id = req.params.id;
// Check id not found
let tag = await Tag.findByPk(id);
if (!tag) {
res.sendStatus(404);
return;
}
let num = await Tag.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: "Tag was deleted successfully.",
});
} else {
res.status(400).json({
message: `Cannot delete tag with id ${id}.`,
});
}
})
module.exports = router;