完善接口
All checks were successful
Go CI / test-and-build (push) Successful in 11s

This commit is contained in:
2026-04-07 20:49:15 +08:00
parent 064ea6c674
commit f57e777580
4 changed files with 81 additions and 3 deletions

View File

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

View File

@@ -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"`
}

View File

@@ -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 {