Files
butterfliu/internal/controller/song.go
lzw-723 a8d976b97e
All checks were successful
Go CI / test-and-build (push) Successful in 43s
清理代码库
2026-04-06 13:17:57 +08:00

44 lines
968 B
Go

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) 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)
}