Storm MediaFileRepository complete.

This commit is contained in:
Deluan
2020-01-10 18:37:52 -05:00
committed by Deluan Quintão
parent c608e917db
commit dbeba1dea9
7 changed files with 139 additions and 25 deletions
+8 -15
View File
@@ -60,18 +60,19 @@ func (r *albumRepository) FindByArtist(artistId string) (domain.Albums, error) {
if err != nil {
return nil, err
}
return r.toDomainList(albums)
return r.toAlbums(albums)
}
func (r *albumRepository) GetAll(options domain.QueryOptions) (domain.Albums, error) {
all, err := r.getAll(&options)
var all []_Album
err := r.getAll(&all, &options)
if err != nil {
return nil, err
}
return r.toDomainList(all)
return r.toAlbums(all)
}
func (r *albumRepository) toDomainList(all []_Album) (domain.Albums, error) {
func (r *albumRepository) toAlbums(all []_Album) (domain.Albums, error) {
result := make(domain.Albums, len(all))
for i, a := range all {
result[i] = domain.Album(a)
@@ -80,7 +81,8 @@ func (r *albumRepository) toDomainList(all []_Album) (domain.Albums, error) {
}
func (r *albumRepository) GetAllIds() ([]string, error) {
all, err := r.getAll(&domain.QueryOptions{})
var all []_Album
err := r.getAll(&all, &domain.QueryOptions{})
if err != nil {
return nil, err
}
@@ -91,15 +93,6 @@ func (r *albumRepository) GetAllIds() ([]string, error) {
return result, nil
}
func (r *albumRepository) getAll(options *domain.QueryOptions) (all []_Album, err error) {
if options.SortBy != "" {
err = Db().AllByIndex(options.SortBy, &all, stormOptions(options))
} else {
err = Db().All(&all, stormOptions(options))
}
return
}
func (r *albumRepository) PurgeInactive(active domain.Albums) ([]string, error) {
activeIDs := make([]string, len(active))
for i, album := range active {
@@ -115,7 +108,7 @@ func (r *albumRepository) GetStarred(options domain.QueryOptions) (domain.Albums
if err != nil {
return nil, err
}
return r.toDomainList(starred)
return r.toAlbums(starred)
}
var _ domain.AlbumRepository = (*albumRepository)(nil)
+112
View File
@@ -0,0 +1,112 @@
package storm
import (
"time"
"github.com/asdine/storm/q"
"github.com/cloudsonic/sonic-server/domain"
)
type _MediaFile struct {
ID string ``
Path string ``
Title string ``
Album string ``
Artist string ``
ArtistID string ``
AlbumArtist string ``
AlbumID string `storm:"index"`
HasCoverArt bool ``
TrackNumber int ``
DiscNumber int ``
Year int ``
Size string ``
Suffix string ``
Duration int ``
BitRate int ``
Genre string ``
Compilation bool ``
PlayCount int ``
PlayDate time.Time ``
Rating int ``
Starred bool `storm:"index"`
StarredAt time.Time ``
CreatedAt time.Time ``
UpdatedAt time.Time ``
}
type mediaFileRepository struct {
stormRepository
}
func NewMediaFileRepository() domain.MediaFileRepository {
r := &mediaFileRepository{}
r.init(&_MediaFile{})
return r
}
func (r *mediaFileRepository) Put(m *domain.MediaFile) error {
tm := _MediaFile(*m)
return Db().Save(&tm)
}
func (r *mediaFileRepository) Get(id string) (*domain.MediaFile, error) {
tm := &_MediaFile{}
err := r.getByID(id, tm)
if err != nil {
return nil, err
}
a := domain.MediaFile(*tm)
return &a, nil
}
func (r *mediaFileRepository) toMediaFiles(all []_MediaFile) (domain.MediaFiles, error) {
result := make(domain.MediaFiles, len(all))
for i, m := range all {
result[i] = domain.MediaFile(m)
}
return result, nil
}
func (r *mediaFileRepository) FindByAlbum(albumId string) (domain.MediaFiles, error) {
var mfs []_MediaFile
err := r.execute(q.Eq("AlbumID", albumId), &mfs)
if err != nil {
return nil, err
}
return r.toMediaFiles(mfs)
}
func (r *mediaFileRepository) GetStarred(options domain.QueryOptions) (domain.MediaFiles, error) {
var starred []_MediaFile
err := r.execute(q.Eq("Starred", true), &starred, &options)
if err != nil {
return nil, err
}
return r.toMediaFiles(starred)
}
func (r *mediaFileRepository) GetAllIds() ([]string, error) {
var all []_MediaFile
err := r.getAll(&all, &domain.QueryOptions{})
if err != nil {
return nil, err
}
result := make([]string, len(all))
for i, m := range all {
result[i] = domain.MediaFile(m).ID
}
return result, nil
}
func (r *mediaFileRepository) PurgeInactive(active domain.MediaFiles) ([]string, error) {
activeIDs := make([]string, len(active))
for i, mediaFile := range active {
activeIDs[i] = mediaFile.ID
}
return r.purgeInactive(activeIDs)
}
var _ domain.MediaFileRepository = (*mediaFileRepository)(nil)
var _ = domain.MediaFile(_MediaFile{})
+10 -1
View File
@@ -74,7 +74,7 @@ func (r *stormRepository) purgeInactive(ids []string) (deleted []string, err err
return deleted, nil
}
func (r *stormRepository) execute(matcher q.Matcher, result *[]_Album, options ...*domain.QueryOptions) error {
func (r *stormRepository) execute(matcher q.Matcher, result interface{}, options ...*domain.QueryOptions) error {
query := Db().Select(matcher)
if len(options) > 0 {
query = addQueryOptions(query, options[0])
@@ -86,6 +86,15 @@ func (r *stormRepository) execute(matcher q.Matcher, result *[]_Album, options .
return err
}
func (r *stormRepository) getAll(all interface{}, options *domain.QueryOptions) (err error) {
if options.SortBy != "" {
err = Db().AllByIndex(options.SortBy, all, stormOptions(options))
} else {
err = Db().All(all, stormOptions(options))
}
return
}
func stormOptions(options *domain.QueryOptions) func(*index.Options) {
return func(opts *index.Options) {
opts.Reverse = options.Desc
+1
View File
@@ -6,4 +6,5 @@ var Set = wire.NewSet(
NewPropertyRepository,
NewArtistRepository,
NewAlbumRepository,
NewMediaFileRepository,
)