package controller import ( "butterfliu/internal/service" "net/http" "strconv" "github.com/go-chi/chi/v5" ) type SongController struct { service *service.SongService } func NewSongController(service *service.SongService) *SongController { return &SongController{service: service} } func (c *SongController) GetAllWithDetails(w http.ResponseWriter, r *http.Request) { songs, err := c.service.GetAllWithDetails() if err != nil { jsonError(w, err.Error(), http.StatusInternalServerError) return } jsonResponse(w, songs, http.StatusOK) } func (c *SongController) GetByIDWithDetails(w http.ResponseWriter, r *http.Request) { paramID := chi.URLParam(r, "id") id, err := strconv.Atoi(paramID) if err != nil { http.Error(w, "无效的歌曲 ID", http.StatusBadRequest) return } song, err := c.service.GetByIDWithDetails(id) if err != nil { jsonError(w, err.Error(), http.StatusInternalServerError) return } jsonResponse(w, song, http.StatusOK) } func (c *SongController) Stream(w http.ResponseWriter, r *http.Request) { paramID := chi.URLParam(r, "id") id, err := strconv.Atoi(paramID) if err != nil { http.Error(w, "无效的歌曲 ID", http.StatusBadRequest) return } mediaFile, err := c.service.GetMediaFile(id) if err != nil { http.Error(w, "未找到该媒体文件", http.StatusNotFound) return } http.ServeFile(w, r, mediaFile.Path) } func (c *SongController) Cover(w http.ResponseWriter, r *http.Request) { paramID := chi.URLParam(r, "id") id, err := strconv.Atoi(paramID) if err != nil { http.Error(w, "无效的歌曲 ID", http.StatusBadRequest) return } coverPath, err := c.service.GetCover(id) if err != nil { http.Error(w, "获取封面异常", http.StatusNotFound) return } http.ServeFile(w, r, coverPath) }