124 lines
4.1 KiB
Go
124 lines
4.1 KiB
Go
|
|
package music
|
||
|
|
|
||
|
|
import (
|
||
|
|
"arimelody-web/errors"
|
||
|
|
"arimelody-web/model"
|
||
|
|
"slices"
|
||
|
|
"strconv"
|
||
|
|
)
|
||
|
|
|
||
|
|
func (repo *MusicRepositoryMemory) GetAllTracks() ([]*model.Track, error) {
|
||
|
|
return repo.tracks, nil
|
||
|
|
}
|
||
|
|
func (repo *MusicRepositoryMemory) GetTrackCount() (int, error) {
|
||
|
|
return len(repo.tracks), nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (repo *MusicRepositoryMemory) GetTrackByID(id string) (*model.Track, error) {
|
||
|
|
index := slices.IndexFunc(repo.tracks, func(track *model.Track) bool {
|
||
|
|
return track.ID == id
|
||
|
|
})
|
||
|
|
if index == -1 { return nil, nil }
|
||
|
|
return repo.tracks[index], nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (repo *MusicRepositoryMemory) GetOrphanTracks() ([]*model.Track, error) {
|
||
|
|
return slices.DeleteFunc(repo.tracks, func(track *model.Track) bool {
|
||
|
|
return slices.ContainsFunc(repo.releases, func(release *model.Release) bool {
|
||
|
|
return slices.ContainsFunc(release.Tracks, func(releaseTrack *model.Track) bool {
|
||
|
|
return releaseTrack.ID == track.ID
|
||
|
|
})
|
||
|
|
})
|
||
|
|
}), nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (repo *MusicRepositoryMemory) GetTracksNotOnRelease(releaseID string) ([]*model.Track, error) {
|
||
|
|
release, err := repo.GetReleaseByID(releaseID)
|
||
|
|
if err != nil { return nil, err }
|
||
|
|
|
||
|
|
return slices.DeleteFunc(repo.tracks, func(track *model.Track) bool {
|
||
|
|
return slices.ContainsFunc(release.Tracks, func(releaseTrack *model.Track) bool {
|
||
|
|
return releaseTrack.ID == track.ID
|
||
|
|
})
|
||
|
|
}), nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (repo *MusicRepositoryMemory) GetTrackReleases(trackID string) ([]*model.Release, error) {
|
||
|
|
return slices.DeleteFunc(repo.releases, func(release *model.Release) bool {
|
||
|
|
return !slices.ContainsFunc(release.Tracks, func(track *model.Track) bool {
|
||
|
|
return track.ID == trackID
|
||
|
|
})
|
||
|
|
}), nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (repo *MusicRepositoryMemory) CreateTrack(
|
||
|
|
title string,
|
||
|
|
description string,
|
||
|
|
lyrics string,
|
||
|
|
previewURL string,
|
||
|
|
) (string, error) {
|
||
|
|
id := strconv.Itoa(len(repo.tracks))
|
||
|
|
repo.tracks = append(repo.tracks, &model.Track{
|
||
|
|
ID: id,
|
||
|
|
Title: title,
|
||
|
|
Description: description,
|
||
|
|
Lyrics: lyrics,
|
||
|
|
PreviewURL: previewURL,
|
||
|
|
})
|
||
|
|
return id, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (repo *MusicRepositoryMemory) UpdateTrack(track *model.Track) error {
|
||
|
|
repoTrack, err := repo.GetTrackByID(track.ID)
|
||
|
|
if err != nil { return err }
|
||
|
|
if repoTrack == nil { return errors.NewNotExistError("Track does not exist") }
|
||
|
|
|
||
|
|
repoTrack.Title = track.Title
|
||
|
|
repoTrack.Description = track.Description
|
||
|
|
repoTrack.Lyrics = track.Lyrics
|
||
|
|
repoTrack.PreviewURL = track.PreviewURL
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
func (repo *MusicRepositoryMemory) UpdateTrackTitle(id string, title string) error {
|
||
|
|
repoTrack, err := repo.GetTrackByID(id)
|
||
|
|
if err != nil { return err }
|
||
|
|
if repoTrack == nil { return errors.NewNotExistError("Track does not exist") }
|
||
|
|
repoTrack.Title = title
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
func (repo *MusicRepositoryMemory) UpdateTrackDescription(id string, description string) error {
|
||
|
|
repoTrack, err := repo.GetTrackByID(id)
|
||
|
|
if err != nil { return err }
|
||
|
|
if repoTrack == nil { return errors.NewNotExistError("Track does not exist") }
|
||
|
|
repoTrack.Description = description
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
func (repo *MusicRepositoryMemory) UpdateTrackLyrics(id string, lyrics string) error {
|
||
|
|
repoTrack, err := repo.GetTrackByID(id)
|
||
|
|
if err != nil { return err }
|
||
|
|
if repoTrack == nil { return errors.NewNotExistError("Track does not exist") }
|
||
|
|
repoTrack.Lyrics = lyrics
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
func (repo *MusicRepositoryMemory) UpdateTrackPreviewURL(id string, previewURL string) error {
|
||
|
|
repoTrack, err := repo.GetTrackByID(id)
|
||
|
|
if err != nil { return err }
|
||
|
|
if repoTrack == nil { return errors.NewNotExistError("Track does not exist") }
|
||
|
|
repoTrack.PreviewURL = previewURL
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (repo *MusicRepositoryMemory) DeleteTrack(id string) (string, error) {
|
||
|
|
var deletedID string
|
||
|
|
newTracks := []*model.Track{}
|
||
|
|
for _, track := range repo.tracks {
|
||
|
|
if track.ID == id {
|
||
|
|
deletedID = id
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
newTracks = append(newTracks, track)
|
||
|
|
}
|
||
|
|
repo.tracks = newTracks
|
||
|
|
return deletedID, nil
|
||
|
|
}
|