53 lines
1.4 KiB
Go
53 lines
1.4 KiB
Go
package controller
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"metazone.cc/mce/internal/common"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// ListRootComments 获取文章顶级评论 GET /api/posts/:id/comments
|
|
func (ctrl *CommentController) ListRootComments(c *gin.Context) {
|
|
postID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
common.Error(c, http.StatusBadRequest, "无效的文章ID")
|
|
return
|
|
}
|
|
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
|
|
|
comments, total, err := ctrl.commentService.ListRootComments(uint(postID), page, pageSize)
|
|
if err != nil {
|
|
common.Error(c, http.StatusInternalServerError, "获取评论失败")
|
|
return
|
|
}
|
|
|
|
common.Ok(c, gin.H{
|
|
"items": comments,
|
|
"total": total,
|
|
"page": page,
|
|
"total_pages": common.PageCount(total, pageSize),
|
|
})
|
|
}
|
|
|
|
// ListReplies 获取顶级评论的全部回复 GET /api/posts/:id/comments/:root_id/replies
|
|
func (ctrl *CommentController) ListReplies(c *gin.Context) {
|
|
rootID, err := strconv.ParseUint(c.Param("root_id"), 10, 64)
|
|
if err != nil {
|
|
common.Error(c, http.StatusBadRequest, "无效的评论ID")
|
|
return
|
|
}
|
|
|
|
replies, err := ctrl.commentService.ListReplies(uint(rootID))
|
|
if err != nil {
|
|
common.Error(c, http.StatusInternalServerError, "获取回复失败")
|
|
return
|
|
}
|
|
|
|
common.Ok(c, gin.H{"replies": replies})
|
|
}
|