From f57e7775800943f0605fd9600e83f11ac81b9082 Mon Sep 17 00:00:00 2001 From: lzw-723 Date: Tue, 7 Apr 2026 20:49:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/controller/library.go | 30 ++++++++++++++++++++++ internal/model/dto.go | 4 +-- internal/service/library.go | 47 +++++++++++++++++++++++++++++++++- main.go | 3 +++ 4 files changed, 81 insertions(+), 3 deletions(-) diff --git a/internal/controller/library.go b/internal/controller/library.go index 66fb22f..e5aeb97 100644 --- a/internal/controller/library.go +++ b/internal/controller/library.go @@ -249,6 +249,21 @@ func (c *LibraryController) GetAlbums(w http.ResponseWriter, r *http.Request) { jsonResponse(w, albums, http.StatusOK) } +func (c *LibraryController) GetAlbumsByArtist(w http.ResponseWriter, r *http.Request) { + idParam := chi.URLParam(r, "id") + id, err := strconv.Atoi(idParam) + if err != nil { + jsonError(w, err.Error(), http.StatusBadRequest) + return + } + albums, err := c.service.GetAlbumsByArtistWithDetail(id) + if err != nil { + jsonError(w, err.Error(), http.StatusInternalServerError) + return + } + jsonResponse(w, albums, http.StatusOK) +} + func (c *LibraryController) GetAlbum(w http.ResponseWriter, r *http.Request) { idParam := chi.URLParam(r, "id") id, err := strconv.Atoi(idParam) @@ -263,3 +278,18 @@ func (c *LibraryController) GetAlbum(w http.ResponseWriter, r *http.Request) { } jsonResponse(w, album, http.StatusOK) } + +func (c *LibraryController) GetAlbumCover(w http.ResponseWriter, r *http.Request) { + idParam := chi.URLParam(r, "id") + id, err := strconv.Atoi(idParam) + if err != nil { + jsonError(w, err.Error(), http.StatusBadRequest) + return + } + coverPath, err := c.service.GetAlbumCover(id) + if err != nil { + jsonError(w, err.Error(), http.StatusInternalServerError) + return + } + http.ServeFile(w, r, coverPath) +} diff --git a/internal/model/dto.go b/internal/model/dto.go index 534e076..c77303d 100644 --- a/internal/model/dto.go +++ b/internal/model/dto.go @@ -20,7 +20,7 @@ type ArtistDetail struct { type AlbumDetail struct { ID int `json:"id"` - Title string `json:"title"` - Artist int `json:"artist"` + Title string `json:"title"` + Artist string `json:"artist"` Songs []int `json:"songs"` } diff --git a/internal/service/library.go b/internal/service/library.go index bf53f2d..363088f 100644 --- a/internal/service/library.go +++ b/internal/service/library.go @@ -1,13 +1,16 @@ package service import ( + "butterfliu/config" "butterfliu/internal/model" "butterfliu/internal/repository" "butterfliu/internal/scanner" "errors" "fmt" "log" + "path" "path/filepath" + "strconv" "strings" ) @@ -75,6 +78,10 @@ func (s *LibraryService) GetSongsByAlbum(id int) ([]repository.SongDetail, error return s.repo.GetSongsByAlbumWithDetails(id) } +func (s *LibraryService) GetSongIDsByAlbum(id int) ([]repository.SongDetail, error) { + return s.repo.GetSongsByAlbumWithDetails(id) +} + func (s *LibraryService) GetArtists() ([]model.Artist, error) { return s.repo.GetArtists() } @@ -104,6 +111,31 @@ func (s *LibraryService) GetAlbums() ([]model.Album, error) { return s.repo.GetAlbums() } +func (s *LibraryService) GetAlbumsByArtistWithDetail(artistID int) ([]model.AlbumDetail, error) { + albums, err := s.repo.GetAlbumsByArtist(artistID) + if err != nil { + return nil, err + } + details := []model.AlbumDetail{} + for _, a := range albums { + songs, err := s.repo.GetSongIDsByAlbum(a.ID) + if err != nil { + break + } + artist, err := s.repo.GetArtist(artistID) + if err != nil { + break + } + details = append(details, model.AlbumDetail{ + ID: a.ID, + Title: a.Title, + Artist: artist.Name, + Songs: songs, + }) + } + return details, nil +} + func (s *LibraryService) GetAlbum(id int) (model.AlbumDetail, error) { album, err := s.repo.GetAlbum(id) if err != nil { @@ -113,14 +145,27 @@ func (s *LibraryService) GetAlbum(id int) (model.AlbumDetail, error) { if err != nil { return model.AlbumDetail{}, err } + artist, err := s.repo.GetArtist(album.ArtistID) + if err != nil { + return model.AlbumDetail{}, err + } return model.AlbumDetail{ ID: id, Title: album.Title, Songs: songs, - Artist: album.ArtistID, + Artist: artist.Name, }, nil } +func (s *LibraryService) GetAlbumCover(id int) (string, error) { + songs, err := s.repo.GetSongIDsByAlbum(id) + if err != nil { + return "", err + } + conf := config.LoadConfig() + return path.Join(conf.GetCachePath("cover"), strconv.Itoa(songs[0])+".jpg"), nil +} + func (s *LibraryService) Scan(id int) (*ScanReport, error) { lib, err := s.repo.GetByID(id) if err != nil { diff --git a/main.go b/main.go index 971d113..9bfe410 100644 --- a/main.go +++ b/main.go @@ -56,12 +56,15 @@ func main() { r.Route("/api/artists", func(r chi.Router) { r.Get("/", libraryController.GetArtists) r.Get("/{id}", libraryController.GetArtist) + r.Get("/{id}", libraryController.GetArtist) r.Get("/{id}/songs", libraryController.GetSongsByArtist) + r.Get("/{id}/albums", libraryController.GetAlbumsByArtist) }) r.Route("/api/albums", func(r chi.Router) { r.Get("/", libraryController.GetAlbums) r.Get("/{id}", libraryController.GetAlbum) + r.Get("/{id}/cover", libraryController.GetAlbumCover) r.Get("/{id}/songs", libraryController.GetSongsByAlbum) })