Admin POST/PUT 复用 admin.cms.write;C 端 GET /home/feed-slots;首页无 active 槽隐藏推荐区、失败回退展示;无新 migration。 Co-authored-by: Cursor <cursoragent@cursor.com>
206 lines
6.4 KiB
Go
206 lines
6.4 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.POST("/banners", middleware.RequireAdminPermission(h.Svc, admin.PermCMSWrite), h.CreateBanner)
|
|
g.PUT("/banners/:id", middleware.RequireAdminPermission(h.Svc, admin.PermCMSWrite), h.UpdateBanner)
|
|
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)
|
|
g.POST("/feed-slots", middleware.RequireAdminPermission(h.Svc, admin.PermCMSWrite), h.CreateFeedSlot)
|
|
g.PUT("/feed-slots/:id", middleware.RequireAdminPermission(h.Svc, admin.PermCMSWrite), h.UpdateFeedSlot)
|
|
}
|
|
|
|
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) CreateBanner(c *gin.Context) {
|
|
adminID, ok := middleware.AdminIDFromContext(c)
|
|
if !ok {
|
|
response.Fail(c, http.StatusUnauthorized, 40101, "admin auth required")
|
|
return
|
|
}
|
|
var body admin.BannerWriteBody
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 40050, "invalid body")
|
|
return
|
|
}
|
|
row, err := h.Svc.CreateBanner(c.Request.Context(), adminID, body)
|
|
if errors.Is(err, admin.ErrInvalidBanner) {
|
|
response.Fail(c, http.StatusBadRequest, 40051, "invalid banner")
|
|
return
|
|
}
|
|
if errors.Is(err, admin.ErrBannerConflict) {
|
|
response.Fail(c, http.StatusConflict, 40910, "banner code conflict")
|
|
return
|
|
}
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50044, "create banner failed")
|
|
return
|
|
}
|
|
response.OK(c, row)
|
|
}
|
|
|
|
func (h *AdminHandler) UpdateBanner(c *gin.Context) {
|
|
adminID, ok := middleware.AdminIDFromContext(c)
|
|
if !ok {
|
|
response.Fail(c, http.StatusUnauthorized, 40101, "admin auth required")
|
|
return
|
|
}
|
|
id, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 40002, "invalid id")
|
|
return
|
|
}
|
|
var body admin.BannerWriteBody
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 40050, "invalid body")
|
|
return
|
|
}
|
|
row, err := h.Svc.UpdateBanner(c.Request.Context(), adminID, id, body)
|
|
if errors.Is(err, admin.ErrBannerNotFound) {
|
|
response.Fail(c, http.StatusNotFound, 40410, "banner not found")
|
|
return
|
|
}
|
|
if errors.Is(err, admin.ErrInvalidBanner) {
|
|
response.Fail(c, http.StatusBadRequest, 40051, "invalid banner")
|
|
return
|
|
}
|
|
if errors.Is(err, admin.ErrBannerConflict) {
|
|
response.Fail(c, http.StatusConflict, 40910, "banner code conflict")
|
|
return
|
|
}
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50045, "update 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)
|
|
}
|
|
|
|
func (h *AdminHandler) CreateFeedSlot(c *gin.Context) {
|
|
adminID, ok := middleware.AdminIDFromContext(c)
|
|
if !ok {
|
|
response.Fail(c, http.StatusUnauthorized, 40101, "admin auth required")
|
|
return
|
|
}
|
|
var body admin.FeedSlotWriteBody
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 40052, "invalid body")
|
|
return
|
|
}
|
|
row, err := h.Svc.CreateFeedSlot(c.Request.Context(), adminID, body)
|
|
if errors.Is(err, admin.ErrInvalidFeedSlot) {
|
|
response.Fail(c, http.StatusBadRequest, 40053, "invalid feed slot")
|
|
return
|
|
}
|
|
if errors.Is(err, admin.ErrFeedSlotConflict) {
|
|
response.Fail(c, http.StatusConflict, 40911, "feed slot code conflict")
|
|
return
|
|
}
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50047, "create feed slot failed")
|
|
return
|
|
}
|
|
response.OK(c, row)
|
|
}
|
|
|
|
func (h *AdminHandler) UpdateFeedSlot(c *gin.Context) {
|
|
adminID, ok := middleware.AdminIDFromContext(c)
|
|
if !ok {
|
|
response.Fail(c, http.StatusUnauthorized, 40101, "admin auth required")
|
|
return
|
|
}
|
|
id, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 40002, "invalid id")
|
|
return
|
|
}
|
|
var body admin.FeedSlotWriteBody
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 40052, "invalid body")
|
|
return
|
|
}
|
|
row, err := h.Svc.UpdateFeedSlot(c.Request.Context(), adminID, id, body)
|
|
if errors.Is(err, admin.ErrFeedSlotNotFound) {
|
|
response.Fail(c, http.StatusNotFound, 40411, "feed slot not found")
|
|
return
|
|
}
|
|
if errors.Is(err, admin.ErrInvalidFeedSlot) {
|
|
response.Fail(c, http.StatusBadRequest, 40053, "invalid feed slot")
|
|
return
|
|
}
|
|
if errors.Is(err, admin.ErrFeedSlotConflict) {
|
|
response.Fail(c, http.StatusConflict, 40911, "feed slot code conflict")
|
|
return
|
|
}
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50048, "update feed slot failed")
|
|
return
|
|
}
|
|
response.OK(c, row)
|
|
}
|