setRating.view implemented

This commit is contained in:
Deluan 2016-03-23 19:37:48 -04:00
parent d59394c653
commit fc8bb34ae3
4 changed files with 59 additions and 0 deletions

View file

@ -60,6 +60,14 @@ func (c *BaseAPIController) ParamTimes(param string) []time.Time {
return times
}
func (c *BaseAPIController) RequiredParamInt(param string, msg string) int {
p := c.Input().Get(param)
if p == "" {
c.SendError(responses.ErrorMissingParameter, msg)
}
return c.ParamInt(param, 0)
}
func (c *BaseAPIController) ParamInt(param string, def int) int {
value := def
c.Ctx.Input.Bind(&value, param)

View file

@ -21,6 +21,25 @@ func (c *MediaAnnotationController) Prepare() {
utils.ResolveDependencies(&c.scrobbler, &c.ratings)
}
func (c *MediaAnnotationController) SetRating() {
id := c.RequiredParamString("id", "Required id parameter is missing")
rating := c.RequiredParamInt("rating", "Required rating parameter is missing")
beego.Debug("Setting rating", rating, "for id", id)
err := c.ratings.SetRating(id, rating)
switch {
case err == domain.ErrNotFound:
beego.Error(err)
c.SendError(responses.ErrorDataNotFound, "Id not found")
case err != nil:
beego.Error(err)
c.SendError(responses.ErrorGeneric, "Internal Error")
}
c.SendEmptyResponse()
}
func (c *MediaAnnotationController) Star() {
ids := c.RequiredParamStrings("id", "Required id parameter is missing")

View file

@ -33,6 +33,7 @@ func mapEndpoints() {
beego.NSRouter("/scrobble.view", &api.MediaAnnotationController{}, "*:Scrobble"),
beego.NSRouter("/star.view", &api.MediaAnnotationController{}, "*:Star"),
beego.NSRouter("/unstar.view", &api.MediaAnnotationController{}, "*:Unstar"),
beego.NSRouter("/setRating.view", &api.MediaAnnotationController{}, "*:SetRating"),
beego.NSRouter("/getAlbumList.view", &api.AlbumListController{}, "*:GetAlbumList"),
beego.NSRouter("/getStarred.view", &api.AlbumListController{}, "*:GetStarred"),

View file

@ -4,10 +4,12 @@ import (
"github.com/astaxie/beego"
"github.com/deluan/gosonic/domain"
"github.com/deluan/gosonic/itunesbridge"
"github.com/deluan/gosonic/utils"
)
type Ratings interface {
SetStar(star bool, ids ...string) error
SetRating(id string, rating int) error
}
func NewRatings(itunes itunesbridge.ItunesControl, mr domain.MediaFileRepository, alr domain.AlbumRepository, ar domain.ArtistRepository) Ratings {
@ -21,6 +23,35 @@ type ratings struct {
artistRepo domain.ArtistRepository
}
func (r ratings) SetRating(id string, rating int) error {
rating = utils.MinInt(rating, 5) * 20
isAlbum, _ := r.albumRepo.Exists(id)
if isAlbum {
mfs, _ := r.mfRepo.FindByAlbum(id)
if len(mfs) > 0 {
beego.Debug("SetRating:", rating, "Album:", mfs[0].Album)
if err := r.itunes.SetAlbumRating(mfs[0].Id, rating); err != nil {
return err
}
}
return nil
}
mf, err := r.mfRepo.Get(id)
if err != nil {
return err
}
if mf != nil {
beego.Debug("SetRating:", rating, "Song:", mf.Title)
if err := r.itunes.SetTrackRating(mf.Id, rating); err != nil {
return err
}
return nil
}
return domain.ErrNotFound
}
func (r ratings) SetStar(star bool, ids ...string) error {
for _, id := range ids {
isAlbum, _ := r.albumRepo.Exists(id)