Files
backend/internal/handler/admin_comment_handler.go
lafay 2f584c1f2c
All checks were successful
Build Backend / build (push) Successful in 5m10s
Build Backend / build-docker (push) Successful in 2m22s
This is a breaking change that renames the entire project from "Carrot BBS" to "WithYou".
The Go module name has been changed from `carrot_bbs` to `with_you`, which requires
all import paths to be updated throughout the codebase and by external consumers.

BREAKING CHANGE: Module name changed from carrot_bbs to with_you. All imports
and dependencies must be updated from carrot_bbs/* to with_you/*.
2026-04-22 16:01:59 +08:00

204 lines
5.6 KiB
Go

package handler
import (
"strconv"
"with_you/internal/dto"
"with_you/internal/model"
"with_you/internal/pkg/response"
"with_you/internal/service"
"github.com/gin-gonic/gin"
)
// AdminCommentHandler 管理端评论处理器
type AdminCommentHandler struct {
adminCommentService service.AdminCommentService
}
// NewAdminCommentHandler 创建管理端评论处理器
func NewAdminCommentHandler(adminCommentService service.AdminCommentService) *AdminCommentHandler {
return &AdminCommentHandler{
adminCommentService: adminCommentService,
}
}
// GetCommentList 获取评论列表
// GET /api/v1/admin/comments
func (h *AdminCommentHandler) GetCommentList(c *gin.Context) {
// 解析分页参数
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
if page <= 0 {
page = 1
}
if pageSize <= 0 || pageSize > 100 {
pageSize = 20
}
// 获取筛选参数
keyword := c.Query("keyword")
postID := c.Query("post_id")
authorID := c.Query("author_id")
status := c.Query("status")
startDate := c.Query("start_date")
endDate := c.Query("end_date")
// 验证状态参数
if status != "" && status != string(model.CommentStatusDraft) &&
status != string(model.CommentStatusPending) &&
status != string(model.CommentStatusPublished) &&
status != string(model.CommentStatusRejected) &&
status != string(model.CommentStatusDeleted) {
response.BadRequest(c, "invalid status value")
return
}
comments, total, err := h.adminCommentService.GetCommentList(c.Request.Context(), page, pageSize, keyword, postID, authorID, status, startDate, endDate)
if err != nil {
response.InternalServerError(c, "failed to get comment list")
return
}
response.Paginated(c, comments, total, page, pageSize)
}
// GetCommentDetail 获取评论详情
// GET /api/v1/admin/comments/:id
func (h *AdminCommentHandler) GetCommentDetail(c *gin.Context) {
commentID := c.Param("id")
if commentID == "" {
response.BadRequest(c, "comment id is required")
return
}
commentDetail, err := h.adminCommentService.GetCommentDetail(c.Request.Context(), commentID)
if err != nil {
response.NotFound(c, "comment not found")
return
}
response.Success(c, commentDetail)
}
// DeleteComment 删除评论
// DELETE /api/v1/admin/comments/:id
func (h *AdminCommentHandler) DeleteComment(c *gin.Context) {
commentID := c.Param("id")
if commentID == "" {
response.BadRequest(c, "comment id is required")
return
}
err := h.adminCommentService.DeleteComment(c.Request.Context(), commentID)
if err != nil {
response.HandleError(c, err, "failed to delete comment")
return
}
response.Success(c, dto.SuccessResponse{
Success: true,
Message: "comment deleted successfully",
})
}
// BatchDeleteComments 批量删除评论
// POST /api/v1/admin/comments/batch-delete
func (h *AdminCommentHandler) BatchDeleteComments(c *gin.Context) {
var req dto.AdminBatchDeleteCommentsRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.BadRequest(c, "invalid request body: "+err.Error())
return
}
result, err := h.adminCommentService.BatchDeleteComments(c.Request.Context(), req.IDs)
if err != nil {
response.InternalServerError(c, "failed to batch delete comments")
return
}
response.Success(c, result)
}
func (h *AdminCommentHandler) ModerateComment(c *gin.Context) {
commentID := c.Param("id")
if commentID == "" {
response.BadRequest(c, "comment id is required")
return
}
var req dto.AdminModerateCommentRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.BadRequest(c, "invalid request body: "+err.Error())
return
}
if req.Status != string(model.CommentStatusPublished) && req.Status != string(model.CommentStatusRejected) {
response.BadRequest(c, "invalid status value, must be 'published' or 'rejected'")
return
}
reviewedBy := c.GetString("user_id")
commentDetail, err := h.adminCommentService.ModerateComment(c.Request.Context(), commentID, model.CommentStatus(req.Status), req.Reason, reviewedBy)
if err != nil {
response.HandleError(c, err, "failed to moderate comment")
return
}
response.Success(c, commentDetail)
}
func (h *AdminCommentHandler) BatchModerateComments(c *gin.Context) {
var req dto.AdminBatchModerateCommentsRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.BadRequest(c, "invalid request body: "+err.Error())
return
}
if req.Status != string(model.CommentStatusPublished) && req.Status != string(model.CommentStatusRejected) {
response.BadRequest(c, "invalid status value, must be 'published' or 'rejected'")
return
}
reviewedBy := c.GetString("user_id")
result, err := h.adminCommentService.BatchModerateComments(c.Request.Context(), req.IDs, model.CommentStatus(req.Status), reviewedBy)
if err != nil {
response.InternalServerError(c, "failed to batch moderate comments")
return
}
response.Success(c, result)
}
// GetPostComments 获取帖子的评论列表
// GET /api/v1/admin/posts/:postId/comments
func (h *AdminCommentHandler) GetPostComments(c *gin.Context) {
postID := c.Param("postId")
if postID == "" {
response.BadRequest(c, "post id is required")
return
}
// 解析分页参数
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
if page <= 0 {
page = 1
}
if pageSize <= 0 || pageSize > 100 {
pageSize = 20
}
comments, total, err := h.adminCommentService.GetCommentsByPostID(c.Request.Context(), postID, page, pageSize)
if err != nil {
response.InternalServerError(c, "failed to get post comments")
return
}
response.Paginated(c, comments, total, page, pageSize)
}