feat(admin): add comprehensive admin management system
- Add admin handlers and services for users, posts, comments, groups, and dashboard - Implement role permission management with ability to view and update permissions - Add database seeding for roles and Casbin permission policies - Upgrade Casbin from v2 to v3 with GORM adapter for persistent policy storage - Add admin-specific repository methods with filtering and pagination - Register new admin API routes under /api/v1/admin/*
This commit is contained in:
151
internal/handler/admin_comment_handler.go
Normal file
151
internal/handler/admin_comment_handler.go
Normal file
@@ -0,0 +1,151 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"carrot_bbs/internal/dto"
|
||||
"carrot_bbs/internal/model"
|
||||
"carrot_bbs/internal/pkg/response"
|
||||
"carrot_bbs/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)
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
Reference in New Issue
Block a user