From fc8bb34ae3e1c007e94f71495e9ec900559c1cc1 Mon Sep 17 00:00:00 2001 From: Deluan Date: Wed, 23 Mar 2016 19:37:48 -0400 Subject: [PATCH] setRating.view implemented --- api/base_api_controller.go | 8 ++++++++ api/media_annotation.go | 19 +++++++++++++++++++ conf/router.go | 1 + engine/ratings.go | 31 +++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+) diff --git a/api/base_api_controller.go b/api/base_api_controller.go index 7138069d8..85eb8827d 100644 --- a/api/base_api_controller.go +++ b/api/base_api_controller.go @@ -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) diff --git a/api/media_annotation.go b/api/media_annotation.go index 5ad791e5c..7f94f6430 100644 --- a/api/media_annotation.go +++ b/api/media_annotation.go @@ -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") diff --git a/conf/router.go b/conf/router.go index 55caf56f8..a14aaa36b 100644 --- a/conf/router.go +++ b/conf/router.go @@ -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"), diff --git a/engine/ratings.go b/engine/ratings.go index f14be9fde..07678a26f 100644 --- a/engine/ratings.go +++ b/engine/ratings.go @@ -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)