- Post 新增 7 字段:Visibility/PostType/ReprintSource/Declaration/ReprintProhibited/PinType/PinnedAt - 公开列表过滤 private 文章(管理员可见全部) - 列表排序:全局置顶 > 按时间 - declarationLabel 模板函数(7 种创作声明) - Pin/Unpin API(admin+,category/global 两种置顶类型)
163 lines
4.3 KiB
Go
163 lines
4.3 KiB
Go
package admin
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"metazone.cc/mce/internal/common"
|
|
"metazone.cc/mce/internal/model"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// Approve 审核通过
|
|
func (ctrl *AdminPostController) Approve(c *gin.Context) {
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
common.Error(c, http.StatusBadRequest, "无效的帖子 ID")
|
|
return
|
|
}
|
|
|
|
if err := ctrl.postService.Approve(uint(id)); err != nil {
|
|
if errors.Is(err, common.ErrPostCannotApprove) || errors.Is(err, common.ErrPostNotFound) {
|
|
common.Error(c, http.StatusBadRequest, err.Error())
|
|
} else {
|
|
common.Error(c, http.StatusInternalServerError, "审核失败")
|
|
}
|
|
return
|
|
}
|
|
|
|
common.OkMessage(c, "审核通过")
|
|
}
|
|
|
|
// Reject 退回
|
|
func (ctrl *AdminPostController) Reject(c *gin.Context) {
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
common.Error(c, http.StatusBadRequest, "无效的帖子 ID")
|
|
return
|
|
}
|
|
|
|
var req model.PostRejectRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
common.Error(c, http.StatusBadRequest, "请填写退回理由")
|
|
return
|
|
}
|
|
|
|
if err := ctrl.postService.Reject(uint(id), req.Reason); err != nil {
|
|
if errors.Is(err, common.ErrPostCannotReject) || errors.Is(err, common.ErrPostNotFound) {
|
|
common.Error(c, http.StatusBadRequest, err.Error())
|
|
} else {
|
|
common.Error(c, http.StatusInternalServerError, "退回失败")
|
|
}
|
|
return
|
|
}
|
|
|
|
common.OkMessage(c, "已退回")
|
|
}
|
|
|
|
// Lock 锁定
|
|
func (ctrl *AdminPostController) Lock(c *gin.Context) {
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
common.Error(c, http.StatusBadRequest, "无效的帖子 ID")
|
|
return
|
|
}
|
|
|
|
var req model.PostLockRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
common.Error(c, http.StatusBadRequest, "请填写锁定理由")
|
|
return
|
|
}
|
|
|
|
if err := ctrl.postService.Lock(uint(id), req.Reason); err != nil {
|
|
if errors.Is(err, common.ErrPostNotFound) || errors.Is(err, common.ErrPostCannotLock) {
|
|
common.Error(c, http.StatusBadRequest, err.Error())
|
|
} else {
|
|
common.Error(c, http.StatusInternalServerError, "锁定失败")
|
|
}
|
|
return
|
|
}
|
|
|
|
common.OkMessage(c, "已锁定")
|
|
}
|
|
|
|
// Unlock 解锁
|
|
func (ctrl *AdminPostController) Unlock(c *gin.Context) {
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
common.Error(c, http.StatusBadRequest, "无效的帖子 ID")
|
|
return
|
|
}
|
|
|
|
if err := ctrl.postService.Unlock(uint(id)); err != nil {
|
|
if errors.Is(err, common.ErrPostCannotUnlock) || errors.Is(err, common.ErrPostNotFound) {
|
|
common.Error(c, http.StatusBadRequest, err.Error())
|
|
} else {
|
|
common.Error(c, http.StatusInternalServerError, "解锁失败")
|
|
}
|
|
return
|
|
}
|
|
|
|
common.OkMessage(c, "已解锁")
|
|
}
|
|
|
|
// Pin 置顶文章
|
|
func (ctrl *AdminPostController) Pin(c *gin.Context) {
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
common.Error(c, http.StatusBadRequest, "无效的帖子 ID")
|
|
return
|
|
}
|
|
|
|
var req struct {
|
|
PinType string `json:"pin_type" binding:"required"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil || (req.PinType != model.PinTypeCategory && req.PinType != model.PinTypeGlobal) {
|
|
common.Error(c, http.StatusBadRequest, "无效的置顶类型")
|
|
return
|
|
}
|
|
|
|
if err := ctrl.postService.Pin(uint(id), req.PinType); err != nil {
|
|
common.Error(c, http.StatusInternalServerError, "置顶失败")
|
|
return
|
|
}
|
|
common.OkMessage(c, "已置顶")
|
|
}
|
|
|
|
// Unpin 取消置顶
|
|
func (ctrl *AdminPostController) Unpin(c *gin.Context) {
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
common.Error(c, http.StatusBadRequest, "无效的帖子 ID")
|
|
return
|
|
}
|
|
|
|
if err := ctrl.postService.Unpin(uint(id)); err != nil {
|
|
common.Error(c, http.StatusInternalServerError, "取消置顶失败")
|
|
return
|
|
}
|
|
common.OkMessage(c, "已取消置顶")
|
|
}
|
|
|
|
// Restore 恢复软删除
|
|
func (ctrl *AdminPostController) Restore(c *gin.Context) {
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
common.Error(c, http.StatusBadRequest, "无效的帖子 ID")
|
|
return
|
|
}
|
|
|
|
if err := ctrl.postService.Restore(uint(id)); err != nil {
|
|
if errors.Is(err, common.ErrPostNotFound) {
|
|
common.Error(c, http.StatusBadRequest, err.Error())
|
|
} else {
|
|
common.Error(c, http.StatusInternalServerError, "恢复失败")
|
|
}
|
|
return
|
|
}
|
|
|
|
common.OkMessage(c, "已恢复")
|
|
}
|