拆分LibraryRepository
All checks were successful
Go CI / test-and-build (push) Successful in 11s

This commit is contained in:
2026-04-08 14:42:52 +08:00
parent 9637ad2476
commit 55c017f60d
8 changed files with 414 additions and 377 deletions

View File

@@ -23,19 +23,35 @@ type ScanReport struct {
}
type LibraryService struct {
repo *repository.LibraryRepository
libRepo *repository.LibraryRepository
artistRepo *repository.ArtistRepository
albumRepo *repository.AlbumRepository
songRepo *repository.SongRepository
mediaRepo *repository.MediaRepository
}
func NewLibraryService(repo *repository.LibraryRepository) *LibraryService {
return &LibraryService{repo: repo}
func NewLibraryService(
libRepo *repository.LibraryRepository,
artistRepo *repository.ArtistRepository,
albumRepo *repository.AlbumRepository,
songRepo *repository.SongRepository,
mediaRepo *repository.MediaRepository,
) *LibraryService {
return &LibraryService{
libRepo: libRepo,
artistRepo: artistRepo,
albumRepo: albumRepo,
songRepo: songRepo,
mediaRepo: mediaRepo,
}
}
func (s *LibraryService) GetAll() ([]model.Library, error) {
return s.repo.GetAll()
return s.libRepo.GetAll()
}
func (s *LibraryService) GetByID(id int) (model.Library, error) {
return s.repo.GetByID(id)
return s.libRepo.GetByID(id)
}
func (s *LibraryService) Create(name, path string) (model.Library, error) {
@@ -45,57 +61,53 @@ func (s *LibraryService) Create(name, path string) (model.Library, error) {
if path = strings.TrimSpace(path); path == "" {
return model.Library{}, errors.New("library path cannot be empty")
}
return s.repo.Create(name, path)
return s.libRepo.Create(name, path)
}
func (s *LibraryService) UpdateName(id int, name string) error {
if name = strings.TrimSpace(name); name == "" {
return errors.New("library name cannot be empty")
}
return s.repo.UpdateName(id, name)
return s.libRepo.UpdateName(id, name)
}
func (s *LibraryService) UpdatePath(id int, path string) error {
if path = strings.TrimSpace(path); path == "" {
return errors.New("library path cannot be empty")
}
return s.repo.UpdatePath(id, path)
return s.libRepo.UpdatePath(id, path)
}
func (s *LibraryService) Delete(id int) error {
return s.repo.Delete(id)
return s.libRepo.Delete(id)
}
func (s *LibraryService) GetSongs(id int) ([]repository.SongDetail, error) {
return s.repo.GetSongsByLibraryWithDetails(id)
func (s *LibraryService) GetSongs(id int) ([]model.SongDetail, error) {
return s.mediaRepo.GetSongsByLibraryWithDetails(id)
}
func (s *LibraryService) GetSongsByArtist(id int) ([]repository.SongDetail, error) {
return s.repo.GetSongsByArtistWithDetails(id)
func (s *LibraryService) GetSongsByArtist(id int) ([]model.SongDetail, error) {
return s.mediaRepo.GetSongsByArtistWithDetails(id)
}
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) GetSongsByAlbum(id int) ([]model.SongDetail, error) {
return s.mediaRepo.GetSongsByAlbumWithDetails(id)
}
func (s *LibraryService) GetArtists() ([]model.Artist, error) {
return s.repo.GetArtists()
return s.artistRepo.GetAll()
}
func (s *LibraryService) GetArtist(id int) (model.ArtistDetail, error) {
artist, err := s.repo.GetArtist(id)
artist, err := s.artistRepo.Get(id)
if err != nil {
return model.ArtistDetail{}, err
}
albums, err := s.repo.GetAlbumIDsByArtist(id)
albums, err := s.albumRepo.GetIDsByArtist(id)
if err != nil {
return model.ArtistDetail{}, err
}
songs, err := s.repo.GetSongIDsByArtist(id)
songs, err := s.artistRepo.GetSongIDsByArtist(id)
if err != nil {
return model.ArtistDetail{}, err
}
@@ -108,21 +120,21 @@ func (s *LibraryService) GetArtist(id int) (model.ArtistDetail, error) {
}
func (s *LibraryService) GetAlbums() ([]model.Album, error) {
return s.repo.GetAlbums()
return s.albumRepo.GetAll()
}
func (s *LibraryService) GetAlbumsByArtistWithDetail(artistID int) ([]model.AlbumDetail, error) {
albums, err := s.repo.GetAlbumsByArtist(artistID)
albums, err := s.albumRepo.GetByArtist(artistID)
if err != nil {
return nil, err
}
details := []model.AlbumDetail{}
for _, a := range albums {
songs, err := s.repo.GetSongIDsByAlbum(a.ID)
songs, err := s.albumRepo.GetSongIDs(a.ID)
if err != nil {
break
}
artist, err := s.repo.GetArtist(artistID)
artist, err := s.artistRepo.Get(artistID)
if err != nil {
break
}
@@ -137,15 +149,15 @@ func (s *LibraryService) GetAlbumsByArtistWithDetail(artistID int) ([]model.Albu
}
func (s *LibraryService) GetAlbum(id int) (model.AlbumDetail, error) {
album, err := s.repo.GetAlbum(id)
album, err := s.albumRepo.Get(id)
if err != nil {
return model.AlbumDetail{}, err
}
songs, err := s.repo.GetSongIDsByAlbum(id)
songs, err := s.albumRepo.GetSongIDs(id)
if err != nil {
return model.AlbumDetail{}, err
}
artist, err := s.repo.GetArtist(album.ArtistID)
artist, err := s.artistRepo.Get(album.ArtistID)
if err != nil {
return model.AlbumDetail{}, err
}
@@ -158,16 +170,19 @@ func (s *LibraryService) GetAlbum(id int) (model.AlbumDetail, error) {
}
func (s *LibraryService) GetAlbumCover(id int) (string, error) {
songs, err := s.repo.GetSongIDsByAlbum(id)
songs, err := s.albumRepo.GetSongIDs(id)
if err != nil {
return "", err
}
if len(songs) == 0 {
return "", errors.New("no songs found for album")
}
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)
lib, err := s.libRepo.GetByID(id)
if err != nil {
return nil, fmt.Errorf("library %d not found: %w", id, err)
}
@@ -207,7 +222,7 @@ func (s *LibraryService) addScannedSong(song scanner.ScannedSong, libraryID int,
return fmt.Errorf("album: %w", err)
}
if _, err := s.repo.CreateSong(song.Title, artist.ID, album.ID, song.Duration, mediaFile.ID); err != nil {
if _, err := s.songRepo.Create(song.Title, artist.ID, album.ID, song.Duration, mediaFile.ID); err != nil {
return fmt.Errorf("song: %w", err)
}
@@ -216,25 +231,25 @@ func (s *LibraryService) addScannedSong(song scanner.ScannedSong, libraryID int,
}
func (s *LibraryService) getOrCreateMediaFile(path string, libraryID int) (model.MediaFile, error) {
mf, err := s.repo.GetMediaFileByPath(path)
mf, err := s.mediaRepo.GetByPath(path)
if err == nil {
return mf, nil
}
return s.repo.CreateMediaFile(path, libraryID)
return s.mediaRepo.Create(path, libraryID)
}
func (s *LibraryService) getOrCreateArtist(name string) (model.Artist, error) {
a, err := s.repo.GetArtistByName(name)
a, err := s.artistRepo.GetByName(name)
if err == nil {
return a, nil
}
return s.repo.CreateArtist(name)
return s.artistRepo.Create(name)
}
func (s *LibraryService) getOrCreateAlbum(title string, artistID int) (model.Album, error) {
al, err := s.repo.GetAlbumByTitleAndArtist(title, artistID)
al, err := s.albumRepo.GetByTitleAndArtist(title, artistID)
if err == nil {
return al, nil
}
return s.repo.CreateAlbum(title, artistID)
return s.albumRepo.Create(title, artistID)
}