Add comment update functionality with UI integration

This commit is contained in:
2025-06-20 13:49:36 +08:00
parent 9e0e83ecd9
commit 46186e95df
5 changed files with 151 additions and 4 deletions

View File

@@ -14,6 +14,7 @@ func AddCommentRoutes(router fiber.Router) {
api.Post("/:resourceID", createComment)
api.Get("/:resourceID", listComments)
api.Get("/user/:username", listCommentsWithUser)
api.Put("/:commentID", updateComment)
}
func createComment(c fiber.Ctx) error {
@@ -89,3 +90,28 @@ func listCommentsWithUser(c fiber.Ctx) error {
Message: "Comments retrieved successfully",
})
}
func updateComment(c fiber.Ctx) error {
userID, ok := c.Locals("uid").(uint)
if !ok {
return model.NewRequestError("You must be logged in to update comment")
}
commentIDStr := c.Params("commentID")
commentID, err := strconv.Atoi(commentIDStr)
if err != nil {
return model.NewRequestError("Invalid comment ID")
}
content := c.FormValue("content")
if content == "" {
return model.NewRequestError("Content cannot be empty")
}
comment, err := service.UpdateComment(uint(commentID), userID, content)
if err != nil {
return err
}
return c.JSON(model.Response[model.CommentView]{
Success: true,
Data: *comment,
Message: "Comment updated successfully",
})
}