🎧 Your Personal Streaming Service
Go airsonicmadsonicmedia-servermusicmusic-servernavidromeopensubsonicraspberry-piself-hostedstreamerstreaming-apistreaming-audiosubsonicsubsonic-server
Moved getAlbumList logic to engine layer
Deluan5be236515d0c
diff --git a/api/get_album_list.go b/api/get_album_list.goindex 3b26240e6..dafdcb3ae 100644--- a/api/get_album_list.go+++ b/api/get_album_list.go@@ -1,52 +1,56 @@package apiimport (+ "time"+"github.com/astaxie/beego""github.com/deluan/gosonic/api/responses""github.com/deluan/gosonic/domain"+ "github.com/deluan/gosonic/engine""github.com/deluan/gosonic/utils""github.com/karlkfi/inject"- "time")type GetAlbumListController struct {BaseAPIController- albumRepo domain.AlbumRepository- types map[string]domain.QueryOptions+ listGen engine.ListGenerator+ types map[string]strategy}+type strategy func(offset int, size int) (*domain.Albums, error)+func (c *GetAlbumListController) Prepare() {- inject.ExtractAssignable(utils.Graph, &c.albumRepo)+ inject.ExtractAssignable(utils.Graph, &c.listGen)- c.types = map[string]domain.QueryOptions{- "newest": domain.QueryOptions{SortBy: "CreatedAt", Desc: true, Alpha: true},- "recent": domain.QueryOptions{SortBy: "PlayDate", Desc: true, Alpha: true},- "frequent": domain.QueryOptions{SortBy: "PlayCount", Desc: true},- "highest": domain.QueryOptions{SortBy: "Rating", Desc: true},+ c.types = map[string]strategy{+ "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) },+ "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) },}}func (c *GetAlbumListController) Get() {typ := c.RequiredParamString("type", "Required string parameter 'type' is not present")- qo, found := c.types[typ]+ method, found := c.types[typ]if !found {beego.Error("getAlbumList type", typ, "not implemented!")c.SendError(responses.ERROR_GENERIC, "Not implemented!")}- qo.Size = utils.MinInt(c.ParamInt("size"), 500)- qo.Offset = c.ParamInt("offset")+ offset := c.ParamInt("offset")+ size := utils.MinInt(c.ParamInt("size"), 500)- albums, err := c.albumRepo.GetAll(qo)+ albums, err := method(offset, size)if err != nil {beego.Error("Error retrieving albums:", err)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.IdalbumList[i].Title = al.NamealbumList[i].Parent = al.ArtistIddiff --git a/conf/inject_definitions.go b/conf/inject_definitions.goindex 2ffc7f3b3..28d28c916 100644--- a/conf/inject_definitions.go+++ b/conf/inject_definitions.go@@ -18,4 +18,5 @@ func init() {// Engine (Use cases)utils.DefineSingleton(new(engine.PropertyRepository), persistence.NewPropertyRepository)utils.DefineSingleton(new(engine.Browser), engine.NewBrowser)+ utils.DefineSingleton(new(engine.ListGenerator), engine.NewListGenerator)}diff --git a/engine/lists.go b/engine/lists.gonew file mode 100644index 000000000..c5fc45c31--- /dev/null+++ b/engine/lists.go@@ -0,0 +1,47 @@+package engine++import (+ "github.com/deluan/gosonic/domain"+)++type ListGenerator interface {+ GetNewest(offset int, size int) (*domain.Albums, error)+ GetRecent(offset int, size int) (*domain.Albums, error)+ GetFrequent(offset int, size int) (*domain.Albums, error)+ GetHighest(offset int, size int) (*domain.Albums, error)+}++func NewListGenerator(alr domain.AlbumRepository) ListGenerator {+ return listGenerator{alr}+}++type listGenerator struct {+ albumRepo domain.AlbumRepository+}++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+}++func (g listGenerator) GetNewest(offset int, size int) (*domain.Albums, error) {+ qo := domain.QueryOptions{SortBy: "CreatedAt", Desc: true, Alpha: true}+ return g.query(qo, offset, size)+}++func (g listGenerator) GetRecent(offset int, size int) (*domain.Albums, error) {+ qo := domain.QueryOptions{SortBy: "PlayDate", Desc: true, Alpha: true}+ return g.query(qo, offset, size)+}++func (g listGenerator) GetFrequent(offset int, size int) (*domain.Albums, error) {+ qo := domain.QueryOptions{SortBy: "PlayCount", Desc: true}+ return g.query(qo, offset, size)+}++func (g listGenerator) GetHighest(offset int, size int) (*domain.Albums, error) {+ qo := domain.QueryOptions{SortBy: "Rating", Desc: true}+ return g.query(qo, offset, size)+}