mirror of
https://github.com/navidrome/navidrome.git
synced 2025-06-14 14:22:28 +03:00
Refactoring to remove duplicated code
This commit is contained in:
parent
10dc172a11
commit
4e6cd7e05d
@ -1,6 +1,8 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
"github.com/astaxie/beego"
|
"github.com/astaxie/beego"
|
||||||
"github.com/deluan/gosonic/api/responses"
|
"github.com/deluan/gosonic/api/responses"
|
||||||
"github.com/deluan/gosonic/engine"
|
"github.com/deluan/gosonic/engine"
|
||||||
@ -9,8 +11,8 @@ import (
|
|||||||
|
|
||||||
type AlbumListController struct {
|
type AlbumListController struct {
|
||||||
BaseAPIController
|
BaseAPIController
|
||||||
listGen engine.ListGenerator
|
listGen engine.ListGenerator
|
||||||
types map[string]strategy
|
listFunctions map[string]strategy
|
||||||
}
|
}
|
||||||
|
|
||||||
type strategy func(offset int, size int) (engine.Entries, error)
|
type strategy func(offset int, size int) (engine.Entries, error)
|
||||||
@ -18,34 +20,43 @@ type strategy func(offset int, size int) (engine.Entries, error)
|
|||||||
func (c *AlbumListController) Prepare() {
|
func (c *AlbumListController) Prepare() {
|
||||||
utils.ResolveDependencies(&c.listGen)
|
utils.ResolveDependencies(&c.listGen)
|
||||||
|
|
||||||
c.types = map[string]strategy{
|
c.listFunctions = map[string]strategy{
|
||||||
"random": func(o int, s int) (engine.Entries, error) { return c.listGen.GetRandom(o, s) },
|
"random": c.listGen.GetRandom,
|
||||||
"newest": func(o int, s int) (engine.Entries, error) { return c.listGen.GetNewest(o, s) },
|
"newest": c.listGen.GetNewest,
|
||||||
"recent": func(o int, s int) (engine.Entries, error) { return c.listGen.GetRecent(o, s) },
|
"recent": c.listGen.GetRecent,
|
||||||
"frequent": func(o int, s int) (engine.Entries, error) { return c.listGen.GetFrequent(o, s) },
|
"frequent": c.listGen.GetFrequent,
|
||||||
"highest": func(o int, s int) (engine.Entries, error) { return c.listGen.GetHighest(o, s) },
|
"highest": c.listGen.GetHighest,
|
||||||
"alphabeticalByName": func(o int, s int) (engine.Entries, error) { return c.listGen.GetByName(o, s) },
|
"alphabeticalByName": c.listGen.GetByName,
|
||||||
"alphabeticalByArtist": func(o int, s int) (engine.Entries, error) { return c.listGen.GetByArtist(o, s) },
|
"alphabeticalByArtist": c.listGen.GetByArtist,
|
||||||
"starred": func(o int, s int) (engine.Entries, error) { return c.listGen.GetStarred(o, s) },
|
"starred": c.listGen.GetStarred,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *AlbumListController) GetAlbumList() {
|
func (c *AlbumListController) getAlbumList() (engine.Entries, error) {
|
||||||
typ := c.RequiredParamString("type", "Required string parameter 'type' is not present")
|
typ := c.RequiredParamString("type", "Required string parameter 'type' is not present")
|
||||||
method, found := c.types[typ]
|
listFunc, found := c.listFunctions[typ]
|
||||||
|
|
||||||
if !found {
|
if !found {
|
||||||
beego.Error("albumList type", typ, "not implemented!")
|
beego.Error("albumList type", typ, "not implemented!")
|
||||||
c.SendError(responses.ErrorGeneric, "Not implemented!")
|
return nil, errors.New("Not implemented!")
|
||||||
}
|
}
|
||||||
|
|
||||||
offset := c.ParamInt("offset", 0)
|
offset := c.ParamInt("offset", 0)
|
||||||
size := utils.MinInt(c.ParamInt("size", 10), 500)
|
size := utils.MinInt(c.ParamInt("size", 10), 500)
|
||||||
|
|
||||||
albums, err := method(offset, size)
|
albums, err := listFunc(offset, size)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
beego.Error("Error retrieving albums:", err)
|
beego.Error("Error retrieving albums:", err)
|
||||||
c.SendError(responses.ErrorGeneric, "Internal Error")
|
return nil, errors.New("Internal Error")
|
||||||
|
}
|
||||||
|
|
||||||
|
return albums, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *AlbumListController) GetAlbumList() {
|
||||||
|
albums, err := c.getAlbumList()
|
||||||
|
if err != nil {
|
||||||
|
c.SendError(responses.ErrorGeneric, err.Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
response := c.NewEmpty()
|
response := c.NewEmpty()
|
||||||
@ -54,21 +65,9 @@ func (c *AlbumListController) GetAlbumList() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *AlbumListController) GetAlbumList2() {
|
func (c *AlbumListController) GetAlbumList2() {
|
||||||
typ := c.RequiredParamString("type", "Required string parameter 'type' is not present")
|
albums, err := c.getAlbumList()
|
||||||
method, found := c.types[typ]
|
|
||||||
|
|
||||||
if !found {
|
|
||||||
beego.Error("albumList2 type", typ, "not implemented!")
|
|
||||||
c.SendError(responses.ErrorGeneric, "Not implemented!")
|
|
||||||
}
|
|
||||||
|
|
||||||
offset := c.ParamInt("offset", 0)
|
|
||||||
size := utils.MinInt(c.ParamInt("size", 10), 500)
|
|
||||||
|
|
||||||
albums, err := method(offset, size)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
beego.Error("Error retrieving albums:", err)
|
c.SendError(responses.ErrorGeneric, err.Error)
|
||||||
c.SendError(responses.ErrorGeneric, "Internal Error")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
response := c.NewEmpty()
|
response := c.NewEmpty()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user