More slices instead of pointers of slice

This commit is contained in:
Deluan 2016-03-19 22:58:20 -04:00
parent bd07c74acd
commit 27b7b7ce08
5 changed files with 40 additions and 40 deletions

View File

@ -16,17 +16,17 @@ type AlbumListController struct {
types map[string]strategy types map[string]strategy
} }
type strategy func(offset int, size int) (*domain.Albums, error) type strategy func(offset int, size int) (domain.Albums, error)
func (c *AlbumListController) Prepare() { func (c *AlbumListController) Prepare() {
utils.ResolveDependencies(&c.listGen) utils.ResolveDependencies(&c.listGen)
c.types = map[string]strategy{ c.types = map[string]strategy{
"random": func(o int, s int) (*domain.Albums, error) { return c.listGen.GetRandom(o, s) }, "random": func(o int, s int) (domain.Albums, error) { return c.listGen.GetRandom(o, s) },
"newest": func(o int, s int) (*domain.Albums, error) { return c.listGen.GetNewest(o, s) }, "newest": func(o int, s int) (domain.Albums, error) { return c.listGen.GetNewest(o, s) },
"recent": func(o int, s int) (*domain.Albums, error) { return c.listGen.GetRecent(o, s) }, "recent": func(o int, s int) (domain.Albums, error) { return c.listGen.GetRecent(o, s) },
"frequent": func(o int, s int) (*domain.Albums, error) { return c.listGen.GetFrequent(o, s) }, "frequent": func(o int, s int) (domain.Albums, error) { return c.listGen.GetFrequent(o, s) },
"highest": func(o int, s int) (*domain.Albums, error) { return c.listGen.GetHighest(o, s) }, "highest": func(o int, s int) (domain.Albums, error) { return c.listGen.GetHighest(o, s) },
} }
} }
@ -48,9 +48,9 @@ func (c *AlbumListController) GetAlbumList() {
c.SendError(responses.ERROR_GENERIC, "Internal Error") c.SendError(responses.ERROR_GENERIC, "Internal Error")
} }
albumList := make([]responses.Child, len(*albums)) albumList := make([]responses.Child, len(albums))
for i, al := range *albums { for i, al := range albums {
albumList[i].Id = al.Id albumList[i].Id = al.Id
albumList[i].Title = al.Name albumList[i].Title = al.Name
albumList[i].Parent = al.ArtistId albumList[i].Parent = al.ArtistId
@ -80,9 +80,9 @@ func (c *AlbumListController) GetStarred() {
response := c.NewEmpty() response := c.NewEmpty()
response.Starred = &responses.Starred{} response.Starred = &responses.Starred{}
response.Starred.Album = make([]responses.Child, len(*albums)) response.Starred.Album = make([]responses.Child, len(albums))
for i, entry := range *albums { for i, entry := range albums {
response.Starred.Album[i] = c.ToChild(entry) response.Starred.Album[i] = c.ToChild(entry)
} }
@ -98,8 +98,8 @@ func (c *AlbumListController) GetNowPlaying() {
response := c.NewEmpty() response := c.NewEmpty()
response.NowPlaying = &responses.NowPlaying{} response.NowPlaying = &responses.NowPlaying{}
response.NowPlaying.Entry = make([]responses.NowPlayingEntry, len(*npInfos)) response.NowPlaying.Entry = make([]responses.NowPlayingEntry, len(npInfos))
for i, entry := range *npInfos { for i, entry := range npInfos {
response.NowPlaying.Entry[i].Child = c.ToChild(entry) response.NowPlaying.Entry[i].Child = c.ToChild(entry)
response.NowPlaying.Entry[i].UserName = entry.UserName response.NowPlaying.Entry[i].UserName = entry.UserName
response.NowPlaying.Entry[i].MinutesAgo = entry.MinutesAgo response.NowPlaying.Entry[i].MinutesAgo = entry.MinutesAgo

View File

@ -28,8 +28,8 @@ type AlbumRepository interface {
Put(m *Album) error Put(m *Album) error
Get(id string) (*Album, error) Get(id string) (*Album, error)
FindByArtist(artistId string) (*Albums, error) FindByArtist(artistId string) (*Albums, error)
GetAll(QueryOptions) (*Albums, error) GetAll(QueryOptions) (Albums, error)
PurgeInactive(active Albums) ([]string, error) PurgeInactive(active Albums) ([]string, error)
GetAllIds() (*[]string, error) GetAllIds() ([]string, error)
GetStarred(QueryOptions) (*Albums, error) GetStarred(QueryOptions) (*Albums, error)
} }

View File

@ -10,13 +10,13 @@ import (
// TODO Use Entries instead of Albums // TODO Use Entries instead of Albums
type ListGenerator interface { type ListGenerator interface {
GetNewest(offset int, size int) (*domain.Albums, error) GetNewest(offset int, size int) (domain.Albums, error)
GetRecent(offset int, size int) (*domain.Albums, error) GetRecent(offset int, size int) (domain.Albums, error)
GetFrequent(offset int, size int) (*domain.Albums, error) GetFrequent(offset int, size int) (domain.Albums, error)
GetHighest(offset int, size int) (*domain.Albums, error) GetHighest(offset int, size int) (domain.Albums, error)
GetRandom(offset int, size int) (*domain.Albums, error) GetRandom(offset int, size int) (domain.Albums, error)
GetStarred() (*Entries, error) GetStarred() (Entries, error)
GetNowPlaying() (*Entries, error) GetNowPlaying() (Entries, error)
} }
func NewListGenerator(alr domain.AlbumRepository, mfr domain.MediaFileRepository, npr NowPlayingRepository) ListGenerator { func NewListGenerator(alr domain.AlbumRepository, mfr domain.MediaFileRepository, npr NowPlayingRepository) ListGenerator {
@ -29,53 +29,53 @@ type listGenerator struct {
npRepo NowPlayingRepository npRepo NowPlayingRepository
} }
func (g listGenerator) query(qo domain.QueryOptions, offset int, size int) (*domain.Albums, error) { func (g listGenerator) query(qo domain.QueryOptions, offset int, size int) (domain.Albums, error) {
qo.Offset = offset qo.Offset = offset
qo.Size = size qo.Size = size
return g.albumRepo.GetAll(qo) return g.albumRepo.GetAll(qo)
} }
func (g listGenerator) GetNewest(offset int, size int) (*domain.Albums, error) { func (g listGenerator) GetNewest(offset int, size int) (domain.Albums, error) {
qo := domain.QueryOptions{SortBy: "CreatedAt", Desc: true, Alpha: true} qo := domain.QueryOptions{SortBy: "CreatedAt", Desc: true, Alpha: true}
return g.query(qo, offset, size) return g.query(qo, offset, size)
} }
func (g listGenerator) GetRecent(offset int, size int) (*domain.Albums, error) { func (g listGenerator) GetRecent(offset int, size int) (domain.Albums, error) {
qo := domain.QueryOptions{SortBy: "PlayDate", Desc: true, Alpha: true} qo := domain.QueryOptions{SortBy: "PlayDate", Desc: true, Alpha: true}
return g.query(qo, offset, size) return g.query(qo, offset, size)
} }
func (g listGenerator) GetFrequent(offset int, size int) (*domain.Albums, error) { func (g listGenerator) GetFrequent(offset int, size int) (domain.Albums, error) {
qo := domain.QueryOptions{SortBy: "PlayCount", Desc: true} qo := domain.QueryOptions{SortBy: "PlayCount", Desc: true}
return g.query(qo, offset, size) return g.query(qo, offset, size)
} }
func (g listGenerator) GetHighest(offset int, size int) (*domain.Albums, error) { func (g listGenerator) GetHighest(offset int, size int) (domain.Albums, error) {
qo := domain.QueryOptions{SortBy: "Rating", Desc: true} qo := domain.QueryOptions{SortBy: "Rating", Desc: true}
return g.query(qo, offset, size) return g.query(qo, offset, size)
} }
func (g listGenerator) GetRandom(offset int, size int) (*domain.Albums, error) { func (g listGenerator) GetRandom(offset int, size int) (domain.Albums, error) {
ids, err := g.albumRepo.GetAllIds() ids, err := g.albumRepo.GetAllIds()
if err != nil { if err != nil {
return nil, err return nil, err
} }
size = utils.MinInt(size, len(*ids)) size = utils.MinInt(size, len(ids))
perm := rand.Perm(size) perm := rand.Perm(size)
r := make(domain.Albums, size) r := make(domain.Albums, size)
for i := 0; i < size; i++ { for i := 0; i < size; i++ {
v := perm[i] v := perm[i]
al, err := g.albumRepo.Get((*ids)[v]) al, err := g.albumRepo.Get((ids)[v])
if err != nil { if err != nil {
return nil, err return nil, err
} }
r[i] = *al r[i] = *al
} }
return &r, nil return r, nil
} }
func (g listGenerator) GetStarred() (*Entries, error) { func (g listGenerator) GetStarred() (Entries, error) {
albums, err := g.albumRepo.GetStarred(domain.QueryOptions{}) albums, err := g.albumRepo.GetStarred(domain.QueryOptions{})
if err != nil { if err != nil {
return nil, err return nil, err
@ -86,10 +86,10 @@ func (g listGenerator) GetStarred() (*Entries, error) {
entries[i] = FromAlbum(&al) entries[i] = FromAlbum(&al)
} }
return &entries, nil return entries, nil
} }
func (g listGenerator) GetNowPlaying() (*Entries, error) { func (g listGenerator) GetNowPlaying() (Entries, error) {
npInfo, err := g.npRepo.GetAll() npInfo, err := g.npRepo.GetAll()
if err != nil { if err != nil {
return nil, err return nil, err
@ -107,5 +107,5 @@ func (g listGenerator) GetNowPlaying() (*Entries, error) {
entries[i].PlayerName = np.PlayerName entries[i].PlayerName = np.PlayerName
} }
return &entries, nil return entries, nil
} }

View File

@ -35,13 +35,13 @@ func (r *albumRepository) FindByArtist(artistId string) (*domain.Albums, error)
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) var as = make(domain.Albums, 0)
err := r.loadAll(&as, options) err := r.loadAll(&as, options)
return &as, err return as, err
} }
func (r *albumRepository) GetAllIds() (*[]string, error) { func (r *albumRepository) GetAllIds() ([]string, error) {
idMap, err := r.getAllIds() idMap, err := r.getAllIds()
if err != nil { if err != nil {
return nil, err return nil, err
@ -54,7 +54,7 @@ func (r *albumRepository) GetAllIds() (*[]string, error) {
i++ i++
} }
return &ids, nil return ids, nil
} }
func (r *albumRepository) PurgeInactive(active domain.Albums) ([]string, error) { func (r *albumRepository) PurgeInactive(active domain.Albums) ([]string, error) {

View File

@ -51,12 +51,12 @@ func (m *MockAlbum) Get(id string) (*domain.Album, error) {
return nil, domain.ErrNotFound return nil, domain.ErrNotFound
} }
func (m *MockAlbum) GetAll(qo domain.QueryOptions) (*domain.Albums, error) { func (m *MockAlbum) GetAll(qo domain.QueryOptions) (domain.Albums, error) {
m.Options = qo m.Options = qo
if m.err { if m.err {
return nil, errors.New("Error!") 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) {