mirror of
https://github.com/navidrome/navidrome.git
synced 2025-06-12 13:23:05 +03:00
Moved getAlbumList logic to engine layer
This commit is contained in:
parent
8607e25c90
commit
5be236515d
@ -1,52 +1,56 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/astaxie/beego"
|
"github.com/astaxie/beego"
|
||||||
"github.com/deluan/gosonic/api/responses"
|
"github.com/deluan/gosonic/api/responses"
|
||||||
"github.com/deluan/gosonic/domain"
|
"github.com/deluan/gosonic/domain"
|
||||||
|
"github.com/deluan/gosonic/engine"
|
||||||
"github.com/deluan/gosonic/utils"
|
"github.com/deluan/gosonic/utils"
|
||||||
"github.com/karlkfi/inject"
|
"github.com/karlkfi/inject"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type GetAlbumListController struct {
|
type GetAlbumListController struct {
|
||||||
BaseAPIController
|
BaseAPIController
|
||||||
albumRepo domain.AlbumRepository
|
listGen engine.ListGenerator
|
||||||
types map[string]domain.QueryOptions
|
types map[string]strategy
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *GetAlbumListController) Prepare() {
|
type strategy func(offset int, size int) (*domain.Albums, error)
|
||||||
inject.ExtractAssignable(utils.Graph, &c.albumRepo)
|
|
||||||
|
|
||||||
c.types = map[string]domain.QueryOptions{
|
func (c *GetAlbumListController) Prepare() {
|
||||||
"newest": domain.QueryOptions{SortBy: "CreatedAt", Desc: true, Alpha: true},
|
inject.ExtractAssignable(utils.Graph, &c.listGen)
|
||||||
"recent": domain.QueryOptions{SortBy: "PlayDate", Desc: true, Alpha: true},
|
|
||||||
"frequent": domain.QueryOptions{SortBy: "PlayCount", Desc: true},
|
c.types = map[string]strategy{
|
||||||
"highest": domain.QueryOptions{SortBy: "Rating", Desc: true},
|
"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() {
|
func (c *GetAlbumListController) Get() {
|
||||||
typ := c.RequiredParamString("type", "Required string parameter 'type' is not present")
|
typ := c.RequiredParamString("type", "Required string parameter 'type' is not present")
|
||||||
qo, found := c.types[typ]
|
method, found := c.types[typ]
|
||||||
|
|
||||||
if !found {
|
if !found {
|
||||||
beego.Error("getAlbumList type", typ, "not implemented!")
|
beego.Error("getAlbumList type", typ, "not implemented!")
|
||||||
c.SendError(responses.ERROR_GENERIC, "Not implemented!")
|
c.SendError(responses.ERROR_GENERIC, "Not implemented!")
|
||||||
}
|
}
|
||||||
|
|
||||||
qo.Size = utils.MinInt(c.ParamInt("size"), 500)
|
offset := c.ParamInt("offset")
|
||||||
qo.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 {
|
if err != nil {
|
||||||
beego.Error("Error retrieving albums:", err)
|
beego.Error("Error retrieving albums:", err)
|
||||||
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
|
||||||
|
@ -18,4 +18,5 @@ func init() {
|
|||||||
// Engine (Use cases)
|
// Engine (Use cases)
|
||||||
utils.DefineSingleton(new(engine.PropertyRepository), persistence.NewPropertyRepository)
|
utils.DefineSingleton(new(engine.PropertyRepository), persistence.NewPropertyRepository)
|
||||||
utils.DefineSingleton(new(engine.Browser), engine.NewBrowser)
|
utils.DefineSingleton(new(engine.Browser), engine.NewBrowser)
|
||||||
|
utils.DefineSingleton(new(engine.ListGenerator), engine.NewListGenerator)
|
||||||
}
|
}
|
||||||
|
47
engine/lists.go
Normal file
47
engine/lists.go
Normal file
@ -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)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user