Converted all collections from repositories to *collections

This commit is contained in:
Deluan 2016-03-08 20:33:09 -05:00
parent e9ab07e4d3
commit 60d4cb5d9f
16 changed files with 53 additions and 49 deletions

View File

@ -34,8 +34,8 @@ func (c *GetIndexesController) Get() {
LastModified: fmt.Sprint(utils.ToMillis(lastModified)),
}
res.Index = make([]responses.Index, len(indexes))
for i, idx := range indexes {
res.Index = make([]responses.Index, len(*indexes))
for i, idx := range *indexes {
res.Index[i].Name = idx.Id
res.Index[i].Artists = make([]responses.Artist, len(idx.Artists))
for j, a := range idx.Artists {

View File

@ -18,8 +18,8 @@ func (c *GetMusicFoldersController) Prepare() {
func (c *GetMusicFoldersController) Get() {
mediaFolderList, _ := c.browser.MediaFolders()
folders := make([]responses.MusicFolder, len(mediaFolderList))
for i, f := range mediaFolderList {
folders := make([]responses.MusicFolder, len(*mediaFolderList))
for i, f := range *mediaFolderList {
folders[i].Id = f.Id
folders[i].Name = f.Name
}

View File

@ -27,7 +27,7 @@ type AlbumRepository interface {
BaseRepository
Put(m *Album) error
Get(id string) (*Album, error)
FindByArtist(artistId string) (Albums, error)
GetAll(QueryOptions) (Albums, error)
FindByArtist(artistId string) (*Albums, error)
GetAll(QueryOptions) (*Albums, error)
PurgeInactive(active *Albums) error
}

View File

@ -13,7 +13,8 @@ type ArtistIndex struct {
}
type ArtistInfos []ArtistInfo
func (a ArtistInfos) Len() int { return len(a) }
func (a ArtistInfos) Len() int { return len(a) }
func (a ArtistInfos) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ArtistInfos) Less(i, j int) bool {
return utils.NoArticle(a[i].Artist) < utils.NoArticle(a[j].Artist)
@ -25,5 +26,5 @@ type ArtistIndexRepository interface {
BaseRepository
Put(m *ArtistIndex) error
Get(id string) (*ArtistIndex, error)
GetAll() (ArtistIndexes, error)
GetAll() (*ArtistIndexes, error)
}

View File

@ -47,6 +47,6 @@ type MediaFileRepository interface {
BaseRepository
Put(m *MediaFile) error
Get(id string) (*MediaFile, error)
FindByAlbum(albumId string) (MediaFiles, error)
FindByAlbum(albumId string) (*MediaFiles, error)
PurgeInactive(active *MediaFiles) error
}

View File

@ -9,5 +9,5 @@ type MediaFolder struct {
type MediaFolders []MediaFolder
type MediaFolderRepository interface {
GetAll() (MediaFolders, error)
GetAll() (*MediaFolders, error)
}

View File

@ -17,8 +17,8 @@ var (
)
type Browser interface {
MediaFolders() (domain.MediaFolders, error)
Indexes(ifModifiedSince time.Time) (domain.ArtistIndexes, time.Time, error)
MediaFolders() (*domain.MediaFolders, error)
Indexes(ifModifiedSince time.Time) (*domain.ArtistIndexes, time.Time, error)
Directory(id string) (*DirectoryInfo, error)
}
@ -36,17 +36,17 @@ type browser struct {
mfileRepo domain.MediaFileRepository
}
func (b browser) MediaFolders() (domain.MediaFolders, error) {
func (b browser) MediaFolders() (*domain.MediaFolders, error) {
return b.folderRepo.GetAll()
}
func (b browser) Indexes(ifModifiedSince time.Time) (domain.ArtistIndexes, time.Time, error) {
func (b browser) Indexes(ifModifiedSince time.Time) (*domain.ArtistIndexes, time.Time, error) {
l, err := b.propRepo.DefaultGet(consts.LastScan, "-1")
ms, _ := strconv.ParseInt(l, 10, 64)
lastModified := utils.ToTime(ms)
if err != nil {
return domain.ArtistIndexes{}, time.Time{}, errors.New(fmt.Sprintf("Error retrieving LastScan property: %v", err))
return &domain.ArtistIndexes{}, time.Time{}, errors.New(fmt.Sprintf("Error retrieving LastScan property: %v", err))
}
if lastModified.After(ifModifiedSince) {
@ -54,7 +54,7 @@ func (b browser) Indexes(ifModifiedSince time.Time) (domain.ArtistIndexes, time.
return indexes, lastModified, err
}
return domain.ArtistIndexes{}, lastModified, nil
return &domain.ArtistIndexes{}, lastModified, nil
}
type Child struct {
@ -107,11 +107,11 @@ func (c browser) Directory(id string) (*DirectoryInfo, error) {
return dir, nil
}
func (c browser) buildArtistDir(a *domain.Artist, albums []domain.Album) *DirectoryInfo {
func (c browser) buildArtistDir(a *domain.Artist, albums *domain.Albums) *DirectoryInfo {
dir := &DirectoryInfo{Id: a.Id, Name: a.Name}
dir.Children = make([]Child, len(albums))
for i, al := range albums {
dir.Children = make([]Child, len(*albums))
for i, al := range *albums {
dir.Children[i].Id = al.Id
dir.Children[i].Title = al.Name
dir.Children[i].IsDir = true
@ -129,11 +129,11 @@ func (c browser) buildArtistDir(a *domain.Artist, albums []domain.Album) *Direct
return dir
}
func (c browser) buildAlbumDir(al *domain.Album, tracks []domain.MediaFile) *DirectoryInfo {
func (c browser) buildAlbumDir(al *domain.Album, tracks *domain.MediaFiles) *DirectoryInfo {
dir := &DirectoryInfo{Id: al.Id, Name: al.Name}
dir.Children = make([]Child, len(tracks))
for i, mf := range tracks {
dir.Children = make([]Child, len(*tracks))
for i, mf := range *tracks {
dir.Children[i].Id = mf.Id
dir.Children[i].Title = mf.Title
dir.Children[i].IsDir = false
@ -176,7 +176,7 @@ func (c browser) isAlbum(id string) bool {
return found
}
func (c browser) retrieveArtist(id string) (a *domain.Artist, as []domain.Album, err error) {
func (c browser) retrieveArtist(id string) (a *domain.Artist, as *domain.Albums, err error) {
a, err = c.artistRepo.Get(id)
if err != nil {
err = fmt.Errorf("Error reading Artist %s from DB: %v", id, err)
@ -189,7 +189,7 @@ func (c browser) retrieveArtist(id string) (a *domain.Artist, as []domain.Album,
return
}
func (c browser) retrieveAlbum(id string) (al *domain.Album, mfs []domain.MediaFile, err error) {
func (c browser) retrieveAlbum(id string) (al *domain.Album, mfs *domain.MediaFiles, err error) {
al, err = c.albumRepo.Get(id)
if err != nil {
err = fmt.Errorf("Error reading Album %s from DB: %v", id, err)

View File

@ -22,8 +22,7 @@ type listGenerator struct {
func (g listGenerator) query(qo domain.QueryOptions, offset int, size int) (*domain.Albums, error) {
qo.Offset = offset
qo.Size = size
als, err := g.albumRepo.GetAll(qo)
return &als, err
return g.albumRepo.GetAll(qo)
}
func (g listGenerator) GetNewest(offset int, size int) (*domain.Albums, error) {

View File

@ -29,16 +29,16 @@ func (r *albumRepository) Get(id string) (*domain.Album, error) {
return rec.(*domain.Album), err
}
func (r *albumRepository) FindByArtist(artistId string) (domain.Albums, error) {
func (r *albumRepository) FindByArtist(artistId string) (*domain.Albums, error) {
var as = make(domain.Albums, 0)
err := r.loadChildren("artist", artistId, &as, domain.QueryOptions{SortBy: "Year"})
return as, err
return &as, err
}
func (r *albumRepository) GetAll(options domain.QueryOptions) (domain.Albums, error) {
func (r *albumRepository) GetAll(options domain.QueryOptions) (*domain.Albums, error) {
var as = make(domain.Albums, 0)
err := r.loadAll(&as, options)
return as, err
return &as, err
}
func (r *albumRepository) PurgeInactive(active *domain.Albums) error {

View File

@ -2,8 +2,9 @@ package persistence
import (
"errors"
"github.com/deluan/gosonic/domain"
"sort"
"github.com/deluan/gosonic/domain"
)
type artistIndexRepository struct {
@ -30,10 +31,10 @@ func (r *artistIndexRepository) Get(id string) (*domain.ArtistIndex, error) {
return rec.(*domain.ArtistIndex), err
}
func (r *artistIndexRepository) GetAll() (domain.ArtistIndexes, error) {
func (r *artistIndexRepository) GetAll() (*domain.ArtistIndexes, error) {
var indices = make(domain.ArtistIndexes, 0)
err := r.loadAll(&indices, domain.QueryOptions{Alpha: true})
return indices, err
return &indices, err
}
var _ domain.ArtistIndexRepository = (*artistIndexRepository)(nil)

View File

@ -1,11 +1,12 @@
package persistence
import (
"strconv"
"testing"
"github.com/deluan/gosonic/domain"
"github.com/deluan/gosonic/tests"
. "github.com/smartystreets/goconvey/convey"
"strconv"
"testing"
)
func TestIndexRepository(t *testing.T) {
@ -56,7 +57,7 @@ func TestIndexRepository(t *testing.T) {
So(indices, ShouldHaveLength, 4)
})
Convey("And the values should be retrieved", func() {
for _, e := range indices {
for _, e := range *indices {
So(e.Id, ShouldBeIn, []string{"1", "2", "3", "4"})
}
})

View File

@ -36,11 +36,11 @@ func (r *mediaFileRepository) Get(id string) (*domain.MediaFile, error) {
return mf, nil
}
func (r *mediaFileRepository) FindByAlbum(albumId string) (domain.MediaFiles, error) {
func (r *mediaFileRepository) FindByAlbum(albumId string) (*domain.MediaFiles, error) {
var mfs = make(domain.MediaFiles, 0)
err := r.loadChildren("album", albumId, &mfs)
sort.Sort(mfs)
return mfs, err
return &mfs, err
}
func (r *mediaFileRepository) PurgeInactive(active *domain.MediaFiles) error {

View File

@ -13,11 +13,11 @@ func NewMediaFolderRepository() domain.MediaFolderRepository {
return &mediaFolderRepository{}
}
func (*mediaFolderRepository) GetAll() (domain.MediaFolders, error) {
func (*mediaFolderRepository) GetAll() (*domain.MediaFolders, error) {
mediaFolder := domain.MediaFolder{Id: "0", Name: "iTunes Library", Path: beego.AppConfig.String("musicFolder")}
result := make(domain.MediaFolders, 1)
result[0] = mediaFolder
return result, nil
return &result, nil
}
var _ domain.MediaFolderRepository = (*mediaFolderRepository)(nil)
var _ domain.MediaFolderRepository = (*mediaFolderRepository)(nil)

View File

@ -48,15 +48,15 @@ func (m *MockAlbum) Get(id string) (*domain.Album, error) {
return m.data[id], nil
}
func (m *MockAlbum) GetAll(qo domain.QueryOptions) (domain.Albums, error) {
func (m *MockAlbum) GetAll(qo domain.QueryOptions) (*domain.Albums, error) {
m.Options = qo
if m.err {
return nil, errors.New("Error!")
}
return m.all, nil
return &m.all, nil
}
func (m *MockAlbum) FindByArtist(artistId string) (domain.Albums, error) {
func (m *MockAlbum) FindByArtist(artistId string) (*domain.Albums, error) {
if m.err {
return nil, errors.New("Error!")
}
@ -69,5 +69,5 @@ func (m *MockAlbum) FindByArtist(artistId string) (domain.Albums, error) {
}
}
return res, nil
return &res, nil
}

View File

@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/deluan/gosonic/domain"
)
@ -29,9 +30,9 @@ func (m *MockArtistIndex) SetData(j string, length int) {
}
}
func (m *MockArtistIndex) GetAll() (domain.ArtistIndexes, error) {
func (m *MockArtistIndex) GetAll() (*domain.ArtistIndexes, error) {
if m.err {
return nil, errors.New("Error!")
}
return m.data, nil
return &m.data, nil
}

View File

@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/deluan/gosonic/domain"
)
@ -49,7 +50,7 @@ func (m *MockMediaFile) Get(id string) (*domain.MediaFile, error) {
return mf, nil
}
func (m *MockMediaFile) FindByAlbum(artistId string) (domain.MediaFiles, error) {
func (m *MockMediaFile) FindByAlbum(artistId string) (*domain.MediaFiles, error) {
if m.err {
return nil, errors.New("Error!")
}
@ -62,5 +63,5 @@ func (m *MockMediaFile) FindByAlbum(artistId string) (domain.MediaFiles, error)
}
}
return res, nil
return &res, nil
}