package adminhandler import ( "errors" "io" "net/http" "strconv" "github.com/gin-gonic/gin" musiclogic "spark-server/internal/module/music/logic" "spark-server/internal/pkg/errs" ) // MusicHandler serves popadmin music CRUD (Java-style ApiResponse). type MusicHandler struct { svc *musiclogic.Service } func NewMusic(svc *musiclogic.Service) *MusicHandler { return &MusicHandler{svc: svc} } func (h *MusicHandler) ready(c *gin.Context) bool { if h == nil || h.svc == nil || !h.svc.Ready() { Fail(c, http.StatusServiceUnavailable, "music 服务未就绪") return false } return true } func (h *MusicHandler) List(c *gin.Context) { if !h.ready(c) { return } list, err := h.svc.ListAdmin(c.Request.Context()) if err != nil { Fail(c, http.StatusInternalServerError, err.Error()) return } settings, err := h.svc.Settings(c.Request.Context()) if err != nil { Fail(c, http.StatusInternalServerError, err.Error()) return } OK(c, gin.H{"items": list, "settings": settings}) } func (h *MusicHandler) GetSettings(c *gin.Context) { if !h.ready(c) { return } out, err := h.svc.Settings(c.Request.Context()) if err != nil { Fail(c, http.StatusInternalServerError, err.Error()) return } OK(c, out) } func (h *MusicHandler) PutSettings(c *gin.Context) { if !h.ready(c) { return } var body struct { MemberPreviewSeconds int `json:"memberPreviewSeconds"` } if err := c.ShouldBindJSON(&body); err != nil { Fail(c, http.StatusBadRequest, "无效 JSON") return } out, err := h.svc.UpdateSettings(c.Request.Context(), body.MemberPreviewSeconds) if err != nil { failMusic(c, err) return } OK(c, out) } func (h *MusicHandler) Create(c *gin.Context) { if !h.ready(c) { return } var in musiclogic.UpsertInput if err := c.ShouldBindJSON(&in); err != nil { Fail(c, http.StatusBadRequest, "无效 JSON") return } out, err := h.svc.Create(c.Request.Context(), in) if err != nil { failMusic(c, err) return } OK(c, out) } func (h *MusicHandler) Update(c *gin.Context) { if !h.ready(c) { return } id, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil || id <= 0 { Fail(c, http.StatusBadRequest, "无效 id") return } var in musiclogic.UpsertInput if err := c.ShouldBindJSON(&in); err != nil { Fail(c, http.StatusBadRequest, "无效 JSON") return } out, err := h.svc.Update(c.Request.Context(), id, in) if err != nil { failMusic(c, err) return } OK(c, out) } func (h *MusicHandler) Delete(c *gin.Context) { if !h.ready(c) { return } id, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil || id <= 0 { Fail(c, http.StatusBadRequest, "无效 id") return } if err := h.svc.Delete(c.Request.Context(), id); err != nil { failMusic(c, err) return } OK(c, nil) } func (h *MusicHandler) UploadCover(c *gin.Context) { h.upload(c, true) } func (h *MusicHandler) UploadAudio(c *gin.Context) { h.upload(c, false) } func (h *MusicHandler) upload(c *gin.Context, cover bool) { if !h.ready(c) { return } id, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil || id <= 0 { Fail(c, http.StatusBadRequest, "无效 id") return } file, hdr, err := c.Request.FormFile("file") if err != nil { Fail(c, http.StatusBadRequest, "请用 multipart 字段 file 上传") return } defer file.Close() body, err := io.ReadAll(io.LimitReader(file, 51<<20)) if err != nil { Fail(c, http.StatusBadRequest, "读取文件失败") return } ct := hdr.Header.Get("Content-Type") var out any if cover { out, err = h.svc.UploadCover(c.Request.Context(), id, hdr.Filename, body, ct) } else { out, err = h.svc.UploadAudio(c.Request.Context(), id, hdr.Filename, body, ct) } if err != nil { failMusic(c, err) return } OK(c, out) } func failMusic(c *gin.Context, err error) { if err == nil { return } if errors.Is(err, musiclogic.ErrNotReady) { Fail(c, http.StatusServiceUnavailable, err.Error()) return } if app, ok := errs.AsAppError(err); ok { switch app.Code { case errs.ErrNotFound.Code: Fail(c, http.StatusNotFound, app.Msg) case errs.ErrInvalidArgument.Code: Fail(c, http.StatusBadRequest, app.Msg) case errs.ErrAvailable.Code: Fail(c, http.StatusServiceUnavailable, app.Msg) default: Fail(c, http.StatusBadRequest, app.Msg) } return } Fail(c, http.StatusInternalServerError, err.Error()) }