This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user