- Service 层:energy_service → energize/operations/admin/query 四文件 - Service 层:post_service → helpers/audit + 核心 CRUD 保留 - Service 层:favorite_service → items + 核心文件夹操作保留 - Service 层:auth_service → password + 核心注册/登录保留 - Service 层:notification_service → notify + 核心列表/已读保留 - Service 层:audit_service → review + 核心列表/详情保留 - Controller 层:studio_controller → page/write/api/post_api/action 五文件 - Controller 层:post_controller → page/api/upload 三文件 - Controller 层:comment_controller → list/write/action/upload 四文件 - 清理未使用导入(notification_service.go 移除 fmt)
64 lines
1.4 KiB
Go
64 lines
1.4 KiB
Go
package controller
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"metazone.cc/metalab/internal/common"
|
|
"metazone.cc/metalab/internal/model"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// Delete DELETE /api/comments/:id
|
|
func (ctrl *CommentController) Delete(c *gin.Context) {
|
|
uid, role, ok := common.GetGinUser(c)
|
|
if !ok {
|
|
common.Error(c, http.StatusUnauthorized, "请先登录")
|
|
return
|
|
}
|
|
|
|
commentID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
common.Error(c, http.StatusBadRequest, "无效的评论ID")
|
|
return
|
|
}
|
|
|
|
// 权限检查由 service 层统一处理
|
|
isAdmin := model.HasMinRole(role, model.RoleAdmin)
|
|
if err := ctrl.commentService.Delete(uint(commentID), uid, isAdmin); err != nil {
|
|
if errors.Is(err, common.ErrPermissionDenied) {
|
|
common.Error(c, http.StatusForbidden, err.Error())
|
|
return
|
|
}
|
|
common.Error(c, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
common.OkMessage(c, "删除成功")
|
|
}
|
|
|
|
// SearchUsers GET /api/users/search?q=keyword
|
|
func (ctrl *CommentController) SearchUsers(c *gin.Context) {
|
|
_, _, ok := common.GetGinUser(c)
|
|
if !ok {
|
|
common.Error(c, http.StatusUnauthorized, "请先登录")
|
|
return
|
|
}
|
|
|
|
q := c.Query("q")
|
|
if q == "" {
|
|
common.Ok(c, []model.UserSearchResult{})
|
|
return
|
|
}
|
|
|
|
users, err := ctrl.commentService.SearchUsers(q)
|
|
if err != nil {
|
|
common.Error(c, http.StatusInternalServerError, "搜索用户失败")
|
|
return
|
|
}
|
|
|
|
common.Ok(c, users)
|
|
}
|