Files
mce/internal/controller/favorite_item_controller.go
Victor_Jay 97adf54d6d fix: golangci-lint 零告警通过 (57→0) + gofumpt/goimports 全量格式化
## CI 修复 (P0/P1)

P0 — 编译阻塞:
  - interfaces.go: postUseCase ISP 接口移除不用的 Create/Update/Delete

P1 — 必须修复:
  - ST1000: 为 13 个包添加包注释 (common/config/model/service/...)
  - ST1005: redis_store.go 全部错误消息改为小写开头
  - errcheck (19处): defer Close()→闭包忽略, notifier.Create→_=, r.Run→检查error
  - errorlint (9处): switch-on-error→errors.Is 链, ==→errors.Is
  - ST1020/ST1022: 导出符号注释以符号名开头

P2 — 安全评审:
  - gosec (12处): G203/G301/G304/G306 添加 nolint 注释并附理由

P3 — 清理:
  - unused: 移除 hasUnicode/energyStore/current/energyAdminUseCase
  - gofumpt + goimports 全量格式化 (35+ 文件)
2026-06-22 02:27:35 +08:00

145 lines
3.6 KiB
Go

package controller
import (
"errors"
"net/http"
"strconv"
"metazone.cc/mce/internal/common"
"github.com/gin-gonic/gin"
)
// ListFolderItems 获取收藏夹内文章列表 GET /api/folders/:id/posts
func (fc *FavoriteController) ListFolderItems(c *gin.Context) {
uid, _, _ := common.GetGinUser(c) // 允许未登录查看公开收藏夹
folderID, 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"))
result, err := fc.svc.ListFolderItems(uid, uint(folderID), page, pageSize)
if err != nil {
if errors.Is(err, common.ErrPermissionDenied) {
common.Error(c, http.StatusForbidden, "该收藏夹未公开")
return
}
common.Error(c, http.StatusInternalServerError, "获取失败")
return
}
common.Ok(c, result)
}
// AddToFolder 收藏文章 POST /api/folders/:id/posts
func (fc *FavoriteController) AddToFolder(c *gin.Context) {
uid, _, ok := common.GetGinUser(c)
if !ok {
common.Error(c, http.StatusUnauthorized, "请先登录")
return
}
folderID, err := strconv.ParseUint(c.Param("id"), 10, 64)
if err != nil {
common.Error(c, http.StatusBadRequest, "无效的收藏夹 ID")
return
}
var req struct {
PostID uint `json:"post_id"`
}
if err := c.ShouldBindJSON(&req); err != nil || req.PostID == 0 {
common.Error(c, http.StatusBadRequest, "参数错误")
return
}
if err := fc.svc.AddFavorite(uid, req.PostID, func() *uint { v := uint(folderID); return &v }()); err != nil {
common.Error(c, http.StatusBadRequest, err.Error())
return
}
common.OkMessage(c, "收藏成功")
}
// RemoveFromFolder 取消收藏 DELETE /api/folders/:id/posts/:postId
func (fc *FavoriteController) RemoveFromFolder(c *gin.Context) {
uid, _, ok := common.GetGinUser(c)
if !ok {
common.Error(c, http.StatusUnauthorized, "请先登录")
return
}
folderID, err := strconv.ParseUint(c.Param("id"), 10, 64)
if err != nil {
common.Error(c, http.StatusBadRequest, "无效的收藏夹 ID")
return
}
postID, err := strconv.ParseUint(c.Param("postId"), 10, 64)
if err != nil {
common.Error(c, http.StatusBadRequest, "无效的文章 ID")
return
}
if err := fc.svc.RemoveFavorite(uid, uint(postID)); err != nil {
common.Error(c, http.StatusBadRequest, err.Error())
return
}
_ = folderID // 移除不依赖具体收藏夹
common.OkMessage(c, "已取消收藏")
}
// GetPostStatus 查询文章收藏状态 GET /api/posts/:id/folder-status
func (fc *FavoriteController) GetPostStatus(c *gin.Context) {
uid, _, ok := common.GetGinUser(c)
if !ok {
common.Ok(c, gin.H{"favorited": false})
return
}
postID, err := strconv.ParseUint(c.Param("id"), 10, 64)
if err != nil {
common.Error(c, http.StatusBadRequest, "无效的文章 ID")
return
}
status, err := fc.svc.GetPostFavoriteStatus(uid, uint(postID))
if err != nil {
common.Error(c, http.StatusInternalServerError, "查询失败")
return
}
common.Ok(c, status)
}
// ToggleFavorite 切换收藏 POST /api/posts/:id/favorite
func (fc *FavoriteController) ToggleFavorite(c *gin.Context) {
uid, _, ok := common.GetGinUser(c)
if !ok {
common.Error(c, http.StatusUnauthorized, "请先登录")
return
}
postID, err := strconv.ParseUint(c.Param("id"), 10, 64)
if err != nil {
common.Error(c, http.StatusBadRequest, "无效的文章 ID")
return
}
isFavored, err := fc.svc.ToggleFavorite(uid, uint(postID))
if err != nil {
common.Error(c, http.StatusInternalServerError, "操作失败")
return
}
common.Ok(c, gin.H{
"favorited": isFavored,
})
}