Files
digital-psychology/apps/api/internal/handler/admin_cms.go
T
jackyu66gitandCursor e32466357d feat(ECR-025): OpsCMS FeedSlot 只读并 Closed
栏目位目录(ops_feed_slots + /cms),复用 admin.cms.read。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-08 03:08:40 +08:00

76 lines
2.3 KiB
Go

package handler
import (
"errors"
"net/http"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/yuxingu/digital-psychology/apps/api/internal/middleware"
"github.com/yuxingu/digital-psychology/apps/api/internal/service/admin"
"github.com/yuxingu/digital-psychology/apps/api/pkg/response"
)
func (h *AdminHandler) registerCMS(authed *gin.RouterGroup) {
g := authed.Group("/cms")
g.GET("/banners", middleware.RequireAdminPermission(h.Svc, admin.PermCMSRead), h.ListBanners)
g.GET("/banners/:id", middleware.RequireAdminPermission(h.Svc, admin.PermCMSRead), h.GetBanner)
g.GET("/feed-slots", middleware.RequireAdminPermission(h.Svc, admin.PermCMSRead), h.ListFeedSlots)
g.GET("/feed-slots/:id", middleware.RequireAdminPermission(h.Svc, admin.PermCMSRead), h.GetFeedSlot)
}
func (h *AdminHandler) ListBanners(c *gin.Context) {
items, err := h.Svc.ListBanners(c.Request.Context())
if err != nil {
response.Fail(c, http.StatusInternalServerError, 50040, "list banners failed")
return
}
response.OK(c, gin.H{"items": items})
}
func (h *AdminHandler) GetBanner(c *gin.Context) {
id, err := uuid.Parse(c.Param("id"))
if err != nil {
response.Fail(c, http.StatusBadRequest, 40002, "invalid id")
return
}
row, err := h.Svc.GetBanner(c.Request.Context(), id)
if errors.Is(err, admin.ErrBannerNotFound) {
response.Fail(c, http.StatusNotFound, 40410, "banner not found")
return
}
if err != nil {
response.Fail(c, http.StatusInternalServerError, 50041, "get banner failed")
return
}
response.OK(c, row)
}
func (h *AdminHandler) ListFeedSlots(c *gin.Context) {
items, err := h.Svc.ListFeedSlots(c.Request.Context())
if err != nil {
response.Fail(c, http.StatusInternalServerError, 50042, "list feed slots failed")
return
}
response.OK(c, gin.H{"items": items})
}
func (h *AdminHandler) GetFeedSlot(c *gin.Context) {
id, err := uuid.Parse(c.Param("id"))
if err != nil {
response.Fail(c, http.StatusBadRequest, 40002, "invalid id")
return
}
row, err := h.Svc.GetFeedSlot(c.Request.Context(), id)
if errors.Is(err, admin.ErrFeedSlotNotFound) {
response.Fail(c, http.StatusNotFound, 40411, "feed slot not found")
return
}
if err != nil {
response.Fail(c, http.StatusInternalServerError, 50043, "get feed slot failed")
return
}
response.OK(c, row)
}