From b3cb8fda22337e22d6d4ee45d5ec6e9cf69be7fa Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 12 Apr 2013 20:42:51 -0400 Subject: [PATCH] add ability to mark studios, genres and people as favorites --- .../UserLibrary/BaseItemsByNameService.cs | 25 +++++- MediaBrowser.Api/UserLibrary/GenresService.cs | 63 +++++++++++++- .../UserLibrary/PersonsService.cs | 60 ++++++++++++++ .../UserLibrary/StudiosService.cs | 60 ++++++++++++++ .../Api/DashboardService.cs | 1 - MediaBrowser.WebDashboard/ApiClient.js | 83 ++++++++++++++++++- MediaBrowser.WebDashboard/packages.config | 2 +- 7 files changed, 289 insertions(+), 5 deletions(-) diff --git a/MediaBrowser.Api/UserLibrary/BaseItemsByNameService.cs b/MediaBrowser.Api/UserLibrary/BaseItemsByNameService.cs index 583f7460d4..6801e14f0a 100644 --- a/MediaBrowser.Api/UserLibrary/BaseItemsByNameService.cs +++ b/MediaBrowser.Api/UserLibrary/BaseItemsByNameService.cs @@ -1,4 +1,5 @@ -using MediaBrowser.Controller.Entities; +using System.Threading; +using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Library; using MediaBrowser.Model.Dto; using MediaBrowser.Model.Querying; @@ -196,6 +197,28 @@ namespace MediaBrowser.Api.UserLibrary return dto; } + + /// + /// Marks the favorite. + /// + /// The get item. + /// The user id. + /// if set to true [is favorite]. + /// Task. + protected async Task MarkFavorite(Func> getItem, Guid userId, bool isFavorite) + { + var user = UserManager.GetUserById(userId); + + var item = await getItem().ConfigureAwait(false); + + // Get the user data for this item + var data = await UserManager.GetUserData(user.Id, item.UserDataId).ConfigureAwait(false); + + // Set favorite status + data.IsFavorite = isFavorite; + + await UserManager.SaveUserData(user.Id, item.UserDataId, data, CancellationToken.None).ConfigureAwait(false); + } } /// diff --git a/MediaBrowser.Api/UserLibrary/GenresService.cs b/MediaBrowser.Api/UserLibrary/GenresService.cs index 13c441c63a..54561f4005 100644 --- a/MediaBrowser.Api/UserLibrary/GenresService.cs +++ b/MediaBrowser.Api/UserLibrary/GenresService.cs @@ -1,4 +1,5 @@ -using MediaBrowser.Controller.Entities; +using System.Threading; +using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Library; using ServiceStack.ServiceHost; using System; @@ -18,6 +19,44 @@ namespace MediaBrowser.Api.UserLibrary { } + [Route("/Users/{UserId}/FavoriteGenres/{Name}", "POST")] + [Api(Description = "Marks a genre as a favorite")] + public class MarkFavoriteGenre : IReturnVoid + { + /// + /// Gets or sets the user id. + /// + /// The user id. + [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")] + public Guid UserId { get; set; } + + /// + /// Gets or sets the name. + /// + /// The name. + [ApiMember(Name = "Name", Description = "Name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")] + public string Name { get; set; } + } + + [Route("/Users/{UserId}/FavoriteGenres/{Name}", "DELETE")] + [Api(Description = "Unmarks a genre as a favorite")] + public class UnmarkFavoriteGenre : IReturnVoid + { + /// + /// Gets or sets the user id. + /// + /// The user id. + [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")] + public Guid UserId { get; set; } + + /// + /// Gets or sets the name. + /// + /// The name. + [ApiMember(Name = "Name", Description = "Name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")] + public string Name { get; set; } + } + /// /// Class GenresService /// @@ -40,6 +79,28 @@ namespace MediaBrowser.Api.UserLibrary return ToOptimizedResult(result); } + /// + /// Posts the specified request. + /// + /// The request. + public void Post(MarkFavoriteGenre request) + { + var task = MarkFavorite(() => LibraryManager.GetGenre(request.Name), request.UserId, true); + + Task.WaitAll(task); + } + + /// + /// Deletes the specified request. + /// + /// The request. + public void Delete(UnmarkFavoriteGenre request) + { + var task = MarkFavorite(() => LibraryManager.GetGenre(request.Name), request.UserId, false); + + Task.WaitAll(task); + } + /// /// Gets all items. /// diff --git a/MediaBrowser.Api/UserLibrary/PersonsService.cs b/MediaBrowser.Api/UserLibrary/PersonsService.cs index 020c373a08..4253ddc804 100644 --- a/MediaBrowser.Api/UserLibrary/PersonsService.cs +++ b/MediaBrowser.Api/UserLibrary/PersonsService.cs @@ -23,6 +23,44 @@ namespace MediaBrowser.Api.UserLibrary public string PersonTypes { get; set; } } + [Route("/Users/{UserId}/FavoritePersons/{Name}", "POST")] + [Api(Description = "Marks a person as a favorite")] + public class MarkFavoritePerson : IReturnVoid + { + /// + /// Gets or sets the user id. + /// + /// The user id. + [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")] + public Guid UserId { get; set; } + + /// + /// Gets or sets the name. + /// + /// The name. + [ApiMember(Name = "Name", Description = "Name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")] + public string Name { get; set; } + } + + [Route("/Users/{UserId}/FavoritePersons/{Name}", "DELETE")] + [Api(Description = "Unmarks a person as a favorite")] + public class UnmarkFavoritePerson : IReturnVoid + { + /// + /// Gets or sets the user id. + /// + /// The user id. + [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")] + public Guid UserId { get; set; } + + /// + /// Gets or sets the name. + /// + /// The name. + [ApiMember(Name = "Name", Description = "Name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")] + public string Name { get; set; } + } + /// /// Class PersonsService /// @@ -45,6 +83,28 @@ namespace MediaBrowser.Api.UserLibrary return ToOptimizedResult(result); } + /// + /// Posts the specified request. + /// + /// The request. + public void Post(MarkFavoritePerson request) + { + var task = MarkFavorite(() => LibraryManager.GetPerson(request.Name), request.UserId, true); + + Task.WaitAll(task); + } + + /// + /// Deletes the specified request. + /// + /// The request. + public void Delete(UnmarkFavoritePerson request) + { + var task = MarkFavorite(() => LibraryManager.GetPerson(request.Name), request.UserId, false); + + Task.WaitAll(task); + } + /// /// Gets all items. /// diff --git a/MediaBrowser.Api/UserLibrary/StudiosService.cs b/MediaBrowser.Api/UserLibrary/StudiosService.cs index 2194757045..24f09c5ef7 100644 --- a/MediaBrowser.Api/UserLibrary/StudiosService.cs +++ b/MediaBrowser.Api/UserLibrary/StudiosService.cs @@ -18,6 +18,44 @@ namespace MediaBrowser.Api.UserLibrary { } + [Route("/Users/{UserId}/FavoriteStudios/{Name}", "POST")] + [Api(Description = "Marks a studio as a favorite")] + public class MarkFavoriteStudio : IReturnVoid + { + /// + /// Gets or sets the user id. + /// + /// The user id. + [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")] + public Guid UserId { get; set; } + + /// + /// Gets or sets the name. + /// + /// The name. + [ApiMember(Name = "Name", Description = "Name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")] + public string Name { get; set; } + } + + [Route("/Users/{UserId}/FavoriteStudios/{Name}", "DELETE")] + [Api(Description = "Unmarks a studio as a favorite")] + public class UnmarkFavoriteStudio : IReturnVoid + { + /// + /// Gets or sets the user id. + /// + /// The user id. + [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")] + public Guid UserId { get; set; } + + /// + /// Gets or sets the name. + /// + /// The name. + [ApiMember(Name = "Name", Description = "Name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")] + public string Name { get; set; } + } + /// /// Class StudiosService /// @@ -40,6 +78,28 @@ namespace MediaBrowser.Api.UserLibrary return ToOptimizedResult(result); } + /// + /// Posts the specified request. + /// + /// The request. + public void Post(MarkFavoriteStudio request) + { + var task = MarkFavorite(() => LibraryManager.GetStudio(request.Name), request.UserId, true); + + Task.WaitAll(task); + } + + /// + /// Deletes the specified request. + /// + /// The request. + public void Delete(UnmarkFavoriteStudio request) + { + var task = MarkFavorite(() => LibraryManager.GetStudio(request.Name), request.UserId, false); + + Task.WaitAll(task); + } + /// /// Gets all items. /// diff --git a/MediaBrowser.WebDashboard/Api/DashboardService.cs b/MediaBrowser.WebDashboard/Api/DashboardService.cs index f5d4e4147d..7756fb5b9a 100644 --- a/MediaBrowser.WebDashboard/Api/DashboardService.cs +++ b/MediaBrowser.WebDashboard/Api/DashboardService.cs @@ -417,7 +417,6 @@ namespace MediaBrowser.WebDashboard.Api "http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js", "http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js", "http://vjs.zencdn.net/c/video.js", - "thirdparty/jplayer/jquery.jplayer.min.js" + versionString, "scripts/all.js" + versionString }; diff --git a/MediaBrowser.WebDashboard/ApiClient.js b/MediaBrowser.WebDashboard/ApiClient.js index be4267d3c6..65012f94d2 100644 --- a/MediaBrowser.WebDashboard/ApiClient.js +++ b/MediaBrowser.WebDashboard/ApiClient.js @@ -1671,7 +1671,7 @@ MediaBrowser.ApiClient = function ($, navigator, JSON, WebSocket, setTimeout) { }; /** - * Updates a user's favorite status for an item and returns the updated UserItemData object. + * Updates a user's favorite status for an item. * @param {String} userId * @param {String} itemId * @param {Boolean} isFavorite @@ -1723,6 +1723,87 @@ MediaBrowser.ApiClient = function ($, navigator, JSON, WebSocket, setTimeout) { }); }; + /** + * Updates a user's favorite status for a person. + * @param {String} userId + * @param {String} name + * @param {Boolean} isFavorite + */ + self.updateFavoritePersonStatus = function (userId, name, isFavorite) { + + if (!userId) { + throw new Error("null userId"); + } + + if (!name) { + throw new Error("null name"); + } + + var url = self.getUrl("Users/" + userId + "/FavoritePersons/" + name); + + var method = isFavorite ? "POST" : "DELETE"; + + return self.ajax({ + type: method, + url: url, + dataType: "json" + }); + }; + + /** + * Updates a user's favorite status for a genre. + * @param {String} userId + * @param {String} name + * @param {Boolean} isFavorite + */ + self.updateFavoriteGenreStatus = function (userId, name, isFavorite) { + + if (!userId) { + throw new Error("null userId"); + } + + if (!name) { + throw new Error("null name"); + } + + var url = self.getUrl("Users/" + userId + "/FavoriteGenre/" + name); + + var method = isFavorite ? "POST" : "DELETE"; + + return self.ajax({ + type: method, + url: url, + dataType: "json" + }); + }; + + /** + * Updates a user's favorite status for a studio. + * @param {String} userId + * @param {String} name + * @param {Boolean} isFavorite + */ + self.updateFavoriteStudioStatus = function (userId, name, isFavorite) { + + if (!userId) { + throw new Error("null userId"); + } + + if (!name) { + throw new Error("null name"); + } + + var url = self.getUrl("Users/" + userId + "/FavoriteStudios/" + name); + + var method = isFavorite ? "POST" : "DELETE"; + + return self.ajax({ + type: method, + url: url, + dataType: "json" + }); + }; + /** * Clears a user's personal rating for an item * @param {String} userId diff --git a/MediaBrowser.WebDashboard/packages.config b/MediaBrowser.WebDashboard/packages.config index 8669c3db34..74c1b370fa 100644 --- a/MediaBrowser.WebDashboard/packages.config +++ b/MediaBrowser.WebDashboard/packages.config @@ -1,6 +1,6 @@  - + \ No newline at end of file