User edit/delete comment function

This commit is contained in:
Rykkel
2024-08-13 10:07:24 +08:00
parent 0e9e7121b0
commit 82f564f8ce
3 changed files with 175 additions and 5 deletions

View File

@@ -301,6 +301,34 @@ router.post("/:id/comments", async (req, res) => {
}
});
router.put("/comments/:id", async (req, res) => {
let { id } = req.params;
let data = req.body;
// Validate request body
let validationSchema = yup.object({
content: yup.string().trim().min(3).max(500).required(),
userId: yup.string().required(),
postId: yup.string().required(),
});
try {
console.log("Validating schema...");
data = await validationSchema.validate(data, { abortEarly: false });
// Ensure comment exists
let comment = await Comment.findByPk(id);
if (!comment) {
res.sendStatus(404); // Comment not found
return;
}
let result = await comment.update(data);
res.json({ message: "Comment updated successfully", comment });
} catch (err) {
res.status(400).json({ errors: err.errors });
}
})
router.get("/:id/getComments", async (req, res) => {
let id = req.params.id;