83 lines
3.4 KiB
Go
83 lines
3.4 KiB
Go
|
|
package music
|
||
|
|
|
||
|
|
import (
|
||
|
|
"arimelody-web/errors"
|
||
|
|
"arimelody-web/model"
|
||
|
|
"arimelody-web/service/validator"
|
||
|
|
)
|
||
|
|
|
||
|
|
func (s *MusicService) GetAllArtists() ([]*model.Artist, error) {
|
||
|
|
return s.repo.GetAllArtists()
|
||
|
|
}
|
||
|
|
func (s *MusicService) GetArtistCount() (int, error) {
|
||
|
|
return s.repo.GetArtistCount()
|
||
|
|
}
|
||
|
|
func (s *MusicService) GetArtistByID(id string) (*model.Artist, error) {
|
||
|
|
artist, err := s.repo.GetArtistByID(id)
|
||
|
|
if err != nil { return nil, err }
|
||
|
|
if artist == nil { return nil, errors.NewNotExistError("Artist does not exist") }
|
||
|
|
return artist, nil
|
||
|
|
}
|
||
|
|
func (s *MusicService) GetArtistsNotOnRelease(releaseID string) ([]*model.Artist, error) {
|
||
|
|
return s.repo.GetArtistsNotOnRelease(releaseID)
|
||
|
|
}
|
||
|
|
func (s *MusicService) GetArtistCredits(artistID string, showHidden bool) ([]*model.Credit, error) {
|
||
|
|
return s.repo.GetArtistCredits(artistID, showHidden)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *MusicService) CreateArtist(
|
||
|
|
id string,
|
||
|
|
name string,
|
||
|
|
website string,
|
||
|
|
avatar string,
|
||
|
|
) error {
|
||
|
|
if len(id) == 0 { return errors.NewValidationError("Artist ID cannot be empty") }
|
||
|
|
if !validator.ValidateID(id) { return errors.NewValidationError("Artist ID contains invalid characters") }
|
||
|
|
if len(name) == 0 { return errors.NewValidationError("Artist name cannot be empty") }
|
||
|
|
|
||
|
|
if err := s.repo.CreateArtist(id, name, website, avatar); err != nil { return err }
|
||
|
|
s.log.Printf("Created new artist '%s' (%s)", name, id)
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *MusicService) UpdateArtist(artist *model.Artist) error {
|
||
|
|
if len(artist.ID) == 0 { return errors.NewValidationError("Artist ID cannot be empty") }
|
||
|
|
if !validator.ValidateID(artist.ID) { return errors.NewValidationError("Artist ID contains invalid characters") }
|
||
|
|
if len(artist.Name) == 0 { return errors.NewValidationError("Artist name cannot be empty") }
|
||
|
|
if err := s.repo.UpdateArtist(artist); err != nil { return err }
|
||
|
|
s.log.Printf("Updated artist %s", artist.ID)
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *MusicService) UpdateArtistID(oldID string, newID string) error {
|
||
|
|
if len(newID) == 0 { return errors.NewValidationError("Artist ID cannot be empty") }
|
||
|
|
if !validator.ValidateID(newID) { return errors.NewValidationError("Artist ID contains invalid characters") }
|
||
|
|
if err := s.repo.UpdateArtistID(oldID, newID); err != nil { return err }
|
||
|
|
s.log.Printf("Updated artist ID %s to %s", oldID, newID)
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
func (s *MusicService) UpdateArtistName(id string, name string) error {
|
||
|
|
if len(name) == 0 { return errors.NewValidationError("Artist name cannot be empty") }
|
||
|
|
if err := s.repo.UpdateArtistName(id, name); err != nil { return err }
|
||
|
|
s.log.Printf("Updated artist %s name to %s", id, name)
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
func (s *MusicService) UpdateArtistWebsite(id string, website string) error {
|
||
|
|
if err := s.repo.UpdateArtistWebsite(id, website); err != nil { return err }
|
||
|
|
s.log.Printf("Updated artist %s website to %s", id, website)
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
func (s *MusicService) UpdateArtistAvatar(id string, avatar string) error {
|
||
|
|
if err := s.repo.UpdateArtistAvatar(id, avatar); err != nil { return err }
|
||
|
|
s.log.Printf("Updated artist %s avatar to %s", id, avatar)
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *MusicService) DeleteArtist(id string) error {
|
||
|
|
deletedID, err := s.repo.DeleteArtist(id)
|
||
|
|
if err != nil { return err }
|
||
|
|
if deletedID == "" { return errors.NewNotExistError("Artist does not exist") }
|
||
|
|
s.log.Printf("Deleted artist %s", id)
|
||
|
|
return nil
|
||
|
|
}
|