Added a new layer: engine (equivalent to use cases in Clean Architecture). Should make testing things easier
This commit is contained in:
+19
-31
@@ -1,23 +1,23 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/deluan/gosonic/api/responses"
|
||||
"github.com/deluan/gosonic/consts"
|
||||
"github.com/deluan/gosonic/domain"
|
||||
"github.com/deluan/gosonic/engine"
|
||||
"github.com/deluan/gosonic/utils"
|
||||
"github.com/karlkfi/inject"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type GetIndexesController struct {
|
||||
BaseAPIController
|
||||
repo domain.ArtistIndexRepository
|
||||
properties domain.PropertyRepository
|
||||
browser engine.Browser
|
||||
}
|
||||
|
||||
func (c *GetIndexesController) Prepare() {
|
||||
inject.ExtractAssignable(utils.Graph, &c.repo)
|
||||
inject.ExtractAssignable(utils.Graph, &c.browser)
|
||||
inject.ExtractAssignable(utils.Graph, &c.properties)
|
||||
}
|
||||
|
||||
@@ -25,40 +25,28 @@ func (c *GetIndexesController) Prepare() {
|
||||
func (c *GetIndexesController) Get() {
|
||||
var err error
|
||||
|
||||
ifModifiedSince := c.Input().Get("ifModifiedSince")
|
||||
if ifModifiedSince == "" {
|
||||
ifModifiedSince = "0"
|
||||
}
|
||||
var ifModifiedSince int64
|
||||
c.Ctx.Input.Bind(&ifModifiedSince, "ifModifiedSince")
|
||||
|
||||
res := responses.Indexes{}
|
||||
res.IgnoredArticles = beego.AppConfig.String("ignoredArticles")
|
||||
|
||||
res.LastModified, err = c.properties.DefaultGet(consts.LastScan, "-1")
|
||||
indexes, lastModified, err := c.browser.Indexes(utils.ToTime(ifModifiedSince))
|
||||
if err != nil {
|
||||
beego.Error("Error retrieving LastScan property:", err)
|
||||
beego.Error("Error retrieving Indexes:", err)
|
||||
c.SendError(responses.ERROR_GENERIC, "Internal Error")
|
||||
}
|
||||
|
||||
i, _ := strconv.Atoi(ifModifiedSince)
|
||||
l, _ := strconv.Atoi(res.LastModified)
|
||||
res := responses.Indexes{
|
||||
IgnoredArticles: beego.AppConfig.String("ignoredArticles"),
|
||||
LastModified: fmt.Sprint(utils.ToMillis(lastModified)),
|
||||
}
|
||||
|
||||
if l > i {
|
||||
indexes, err := c.repo.GetAll()
|
||||
if err != nil {
|
||||
beego.Error("Error retrieving Indexes:", err)
|
||||
c.SendError(responses.ERROR_GENERIC, "Internal Error")
|
||||
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 {
|
||||
res.Index[i].Artists[j].Id = a.ArtistId
|
||||
res.Index[i].Artists[j].Name = a.Artist
|
||||
}
|
||||
|
||||
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 {
|
||||
res.Index[i].Artists[j].Id = a.ArtistId
|
||||
res.Index[i].Artists[j].Name = a.Artist
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
response := c.NewEmpty()
|
||||
|
||||
Reference in New Issue
Block a user