Removed Beego routing/controllers, converted to Chi.

Also introduced Wire for dependency injection
This commit is contained in:
Deluan
2020-01-07 14:56:26 -05:00
committed by Deluan Quintão
parent 1f4dfcb853
commit 79701caca3
31 changed files with 1603 additions and 1188 deletions
+54 -37
View File
@@ -2,97 +2,114 @@ package api
import (
"fmt"
"net/http"
"time"
"github.com/astaxie/beego"
"github.com/cloudsonic/sonic-server/api/responses"
"github.com/cloudsonic/sonic-server/domain"
"github.com/cloudsonic/sonic-server/engine"
"github.com/cloudsonic/sonic-server/utils"
)
type MediaAnnotationController struct {
BaseAPIController
scrobbler engine.Scrobbler
ratings engine.Ratings
}
func (c *MediaAnnotationController) Prepare() {
utils.ResolveDependencies(&c.scrobbler, &c.ratings)
func NewMediaAnnotationController(scrobbler engine.Scrobbler, ratings engine.Ratings) *MediaAnnotationController {
return &MediaAnnotationController{
scrobbler: scrobbler,
ratings: ratings,
}
}
func (c *MediaAnnotationController) SetRating() {
id := c.RequiredParamString("id", "Required id parameter is missing")
rating := c.RequiredParamInt("rating", "Required rating parameter is missing")
func (c *MediaAnnotationController) SetRating(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
id, err := RequiredParamString(r, "id", "Required id parameter is missing")
if err != nil {
return nil, err
}
rating, err := RequiredParamInt(r, "rating", "Required rating parameter is missing")
if err != nil {
return nil, err
}
beego.Debug("Setting rating", rating, "for id", id)
err := c.ratings.SetRating(id, rating)
err = c.ratings.SetRating(id, rating)
switch {
case err == domain.ErrNotFound:
beego.Error(err)
c.SendError(responses.ErrorDataNotFound, "Id not found")
return nil, NewError(responses.ErrorDataNotFound, "Id not found")
case err != nil:
beego.Error(err)
c.SendError(responses.ErrorGeneric, "Internal Error")
return nil, NewError(responses.ErrorGeneric, "Internal Error")
}
c.SendEmptyResponse()
return NewEmpty(), nil
}
func (c *MediaAnnotationController) getIds() []string {
ids := c.ParamStrings("id")
albumIds := c.ParamStrings("albumId")
func (c *MediaAnnotationController) getIds(r *http.Request) ([]string, error) {
ids := ParamStrings(r, "id")
albumIds := ParamStrings(r,"albumId")
if len(ids) == 0 && len(albumIds) == 0 {
c.SendError(responses.ErrorMissingParameter, "Required id parameter is missing")
return nil, NewError(responses.ErrorMissingParameter, "Required id parameter is missing")
}
return append(ids, albumIds...)
return append(ids, albumIds...), nil
}
func (c *MediaAnnotationController) Star() {
ids := c.getIds()
func (c *MediaAnnotationController) Star(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
ids, err := c.getIds(r)
if err != nil {
return nil, err
}
beego.Debug("Starring ids:", ids)
err := c.ratings.SetStar(true, ids...)
err = c.ratings.SetStar(true, ids...)
switch {
case err == domain.ErrNotFound:
beego.Error(err)
c.SendError(responses.ErrorDataNotFound, "Id not found")
return nil, NewError(responses.ErrorDataNotFound, "Id not found")
case err != nil:
beego.Error(err)
c.SendError(responses.ErrorGeneric, "Internal Error")
return nil, NewError(responses.ErrorGeneric, "Internal Error")
}
c.SendEmptyResponse()
return NewEmpty(), nil
}
func (c *MediaAnnotationController) Unstar() {
ids := c.getIds()
func (c *MediaAnnotationController) Unstar(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
ids, err := c.getIds(r)
if err != nil {
return nil, err
}
beego.Debug("Unstarring ids:", ids)
err := c.ratings.SetStar(false, ids...)
err = c.ratings.SetStar(false, ids...)
switch {
case err == domain.ErrNotFound:
beego.Error(err)
c.SendError(responses.ErrorDataNotFound, "Directory not found")
return nil, NewError(responses.ErrorDataNotFound, "Directory not found")
case err != nil:
beego.Error(err)
c.SendError(responses.ErrorGeneric, "Internal Error")
return nil, NewError(responses.ErrorGeneric, "Internal Error")
}
c.SendEmptyResponse()
return NewEmpty(), nil
}
func (c *MediaAnnotationController) Scrobble() {
ids := c.RequiredParamStrings("id", "Required id parameter is missing")
times := c.ParamTimes("time")
if len(times) > 0 && len(times) != len(ids) {
c.SendError(responses.ErrorGeneric, "Wrong number of timestamps: %d", len(times))
func (c *MediaAnnotationController) Scrobble(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
ids, err := RequiredParamStrings(r, "id", "Required id parameter is missing")
if err != nil {
return nil, err
}
submission := c.ParamBool("submission", true)
times := ParamTimes(r, "time")
if len(times) > 0 && len(times) != len(ids) {
return nil, NewError(responses.ErrorGeneric, "Wrong number of timestamps: %d, should be %d", len(times), len(ids))
}
submission := ParamBool(r, "submission", true)
playerId := 1 // TODO Multiple players, based on playerName/username/clientIP(?)
playerName := c.ParamString("c")
username := c.ParamString("u")
playerName := ParamString(r, "c")
username := ParamString(r, "u")
beego.Debug("Scrobbling ids:", ids, "times:", times, "submission:", submission)
for i, id := range ids {
@@ -118,5 +135,5 @@ func (c *MediaAnnotationController) Scrobble() {
beego.Info(fmt.Sprintf(`Now Playing (%s) "%s" at %v`, id, mf.Title, t))
}
}
c.SendEmptyResponse()
return NewEmpty(), nil
}