arimelody-web/repository/music/artist_memory.go

135 lines
4.1 KiB
Go

package music
import (
"arimelody-web/errors"
"arimelody-web/model"
"slices"
)
func (repo *MusicRepositoryMemory) GetAllArtists() ([]*model.Artist, error) {
return repo.artists, nil
}
func (repo *MusicRepositoryMemory) GetArtistCount() (int, error) {
return len(repo.artists), nil
}
func (repo *MusicRepositoryMemory) GetArtistByID(id string) (*model.Artist, error) {
index := slices.IndexFunc(repo.artists, func(artist *model.Artist) bool {
return artist.ID == id
})
if index == -1 { return nil, nil }
return repo.artists[index], nil
}
func (repo *MusicRepositoryMemory) GetArtistsNotOnRelease(releaseID string) ([]*model.Artist, error) {
release, err := repo.GetReleaseByID(releaseID)
if err != nil { return nil, err }
artists, err := repo.GetAllArtists()
if err != nil { return nil, err }
artistsNotOnRelease := []*model.Artist{}
for _, artist := range artists {
if !slices.ContainsFunc(release.Credits, func(credit *model.Credit) bool {
return credit.Artist.ID == artist.ID
}) {
artistsNotOnRelease = append(artistsNotOnRelease, artist)
}
}
return artistsNotOnRelease, nil
}
func (repo *MusicRepositoryMemory) GetArtistCredits(artistID string, showHidden bool) ([]*model.Credit, error) {
releases, err := repo.GetAllReleases(!showHidden, 0)
if err != nil { return nil, err }
credits := []*model.Credit{}
for _, release := range releases {
credits = append(credits, slices.DeleteFunc(
release.Credits,
func(credit *model.Credit) bool {
return credit.Artist.ID != artistID
},
)...)
}
return credits, nil
}
func (repo *MusicRepositoryMemory) CreateArtist(
id string,
name string,
website string,
avatar string,
) error {
if artist, err := repo.GetArtistByID(id); err != nil {
if !errors.IsNotExistError(err) { return err }
} else {
if artist != nil { return errors.NewValidationError("Artist with this ID already exists") }
}
repo.artists = append(repo.artists, &model.Artist{
ID: id,
Name: name,
Website: website,
Avatar: avatar,
})
return nil
}
func (repo *MusicRepositoryMemory) UpdateArtist(artist *model.Artist) error {
repoArtist, err := repo.GetArtistByID(artist.ID)
if err != nil { return err }
if repoArtist == nil { return errors.NewNotExistError("Artist does not exist") }
repoArtist.Name = artist.Name
repoArtist.Website = artist.Website
repoArtist.Avatar = artist.Avatar
return nil
}
func (repo *MusicRepositoryMemory) UpdateArtistID(oldID string, newID string) error {
artist, err := repo.GetArtistByID(oldID)
if err != nil { return err }
if artist == nil { return errors.NewNotExistError("Artist does not exist") }
artist.ID = newID
return nil
}
func (repo *MusicRepositoryMemory) UpdateArtistName(id string, name string) error {
artist, err := repo.GetArtistByID(id)
if err != nil { return err }
if artist == nil { return errors.NewNotExistError("Artist does not exist") }
artist.Name = name
return nil
}
func (repo *MusicRepositoryMemory) UpdateArtistWebsite(id string, website string) error {
artist, err := repo.GetArtistByID(id)
if err != nil { return err }
if artist == nil { return errors.NewNotExistError("Artist does not exist") }
artist.Website = website
return nil
}
func (repo *MusicRepositoryMemory) UpdateArtistAvatar(id string, avatar string) error {
artist, err := repo.GetArtistByID(id)
if err != nil { return err }
if artist == nil { return errors.NewNotExistError("Artist does not exist") }
artist.Avatar = avatar
return nil
}
func (repo *MusicRepositoryMemory) DeleteArtist(id string) (string, error) {
var deletedID string
newArtists := []*model.Artist{}
for _, artist := range repo.artists {
if artist.ID == id {
deletedID = id
continue
}
newArtists = append(newArtists, artist)
}
repo.artists = newArtists
return deletedID, nil
}