From 25d2a57aca5f8f1999ecf6f8b9cf6a51a5317383 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sat, 13 Apr 2013 14:51:20 -0400 Subject: [PATCH 1/2] added user data methods for items by name --- MediaBrowser.Api/MediaBrowser.Api.csproj | 1 + .../UserLibrary/BaseItemsByNameService.cs | 25 -- .../UserLibrary/ItemByNameUserDataService.cs | 231 ++++++++++++++++++ .../UserLibrary/UserLibraryService.cs | 2 +- 4 files changed, 233 insertions(+), 26 deletions(-) create mode 100644 MediaBrowser.Api/UserLibrary/ItemByNameUserDataService.cs diff --git a/MediaBrowser.Api/MediaBrowser.Api.csproj b/MediaBrowser.Api/MediaBrowser.Api.csproj index c7593d9e9..710c159e7 100644 --- a/MediaBrowser.Api/MediaBrowser.Api.csproj +++ b/MediaBrowser.Api/MediaBrowser.Api.csproj @@ -90,6 +90,7 @@ + diff --git a/MediaBrowser.Api/UserLibrary/BaseItemsByNameService.cs b/MediaBrowser.Api/UserLibrary/BaseItemsByNameService.cs index 42aafd2cd..432594842 100644 --- a/MediaBrowser.Api/UserLibrary/BaseItemsByNameService.cs +++ b/MediaBrowser.Api/UserLibrary/BaseItemsByNameService.cs @@ -9,7 +9,6 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; -using System.Threading; using System.Threading.Tasks; namespace MediaBrowser.Api.UserLibrary @@ -203,30 +202,6 @@ 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); - - var key = item.GetUserDataKey(); - - // Get the user data for this item - var data = await UserDataRepository.GetUserData(user.Id, key).ConfigureAwait(false); - - // Set favorite status - data.IsFavorite = isFavorite; - - await UserDataRepository.SaveUserData(user.Id, key, data, CancellationToken.None).ConfigureAwait(false); - } } /// diff --git a/MediaBrowser.Api/UserLibrary/ItemByNameUserDataService.cs b/MediaBrowser.Api/UserLibrary/ItemByNameUserDataService.cs new file mode 100644 index 000000000..f872fb680 --- /dev/null +++ b/MediaBrowser.Api/UserLibrary/ItemByNameUserDataService.cs @@ -0,0 +1,231 @@ +using MediaBrowser.Controller.Persistence; +using ServiceStack.ServiceHost; +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace MediaBrowser.Api.UserLibrary +{ + /// + /// Class GetItemByNameUserData + /// + [Route("/Users/{UserId}/ItemsByName/{Name}/UserData", "GET")] + [Api(Description = "Gets user data for an item")] + public class GetItemByNameUserData : IReturnVoid + { + /// + /// Gets or sets the user id. + /// + /// The user id. + [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")] + public Guid UserId { get; set; } + + /// + /// Gets or sets the name. + /// + /// The name. + [ApiMember(Name = "Name", Description = "The item name (genre, person, year, studio, artist, album)", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")] + public string Name { get; set; } + } + + /// + /// Class MarkItemByNameFavorite + /// + [Route("/Users/{UserId}/ItemsByName/Favorites/{Name}", "POST")] + [Api(Description = "Marks something as a favorite")] + public class MarkItemByNameFavorite : 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 = "The item name (genre, person, year, studio, artist, album)", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")] + public string Name { get; set; } + } + + /// + /// Class UnmarkItemByNameFavorite + /// + [Route("/Users/{UserId}/ItemsByName/Favorites/{Name}", "DELETE")] + [Api(Description = "Unmarks something as a favorite")] + public class UnmarkItemByNameFavorite : 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 = "The item name (genre, person, year, studio, artist, album)", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")] + public string Name { get; set; } + } + + [Route("/Users/{UserId}/ItemsByName/{Name}/Rating", "POST")] + [Api(Description = "Updates a user's rating for an item")] + public class UpdateItemByNameRating : 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 = "The item name (genre, person, year, studio, artist, album)", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")] + public string Name { get; set; } + + /// + /// Gets or sets a value indicating whether this is likes. + /// + /// true if likes; otherwise, false. + [ApiMember(Name = "Likes", Description = "Whether the user likes the item or not. true/false", IsRequired = true, DataType = "boolean", ParameterType = "query", Verb = "POST")] + public bool Likes { get; set; } + } + + [Route("/Users/{UserId}/ItemsByName/{Name}/Rating", "DELETE")] + [Api(Description = "Deletes a user's saved personal rating for an item")] + public class DeleteItemByNameRating : 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 = "The item name (genre, person, year, studio, artist, album)", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")] + public string Name { get; set; } + } + + /// + /// Class ItemByNameUserDataService + /// + public class ItemByNameUserDataService : BaseApiService + { + /// + /// The user data repository + /// + protected readonly IUserDataRepository UserDataRepository; + + /// + /// Initializes a new instance of the class. + /// + /// The user data repository. + public ItemByNameUserDataService(IUserDataRepository userDataRepository) + { + UserDataRepository = userDataRepository; + } + + /// + /// Gets the specified request. + /// + /// The request. + /// System.Object. + public object Get(GetItemByNameUserData request) + { + // Get the user data for this item + var data = UserDataRepository.GetUserData(request.UserId, request.Name).Result; + + return ToOptimizedResult(data); + } + + /// + /// Posts the specified request. + /// + /// The request. + public void Post(MarkItemByNameFavorite request) + { + var task = MarkFavorite(request.UserId, request.Name, true); + + Task.WaitAll(task); + } + + /// + /// Posts the specified request. + /// + /// The request. + public void Post(UpdateItemByNameRating request) + { + var task = MarkLike(request.UserId, request.Name, request.Likes); + + Task.WaitAll(task); + } + + /// + /// Deletes the specified request. + /// + /// The request. + public void Delete(MarkItemByNameFavorite request) + { + var task = MarkFavorite(request.UserId, request.Name, false); + + Task.WaitAll(task); + } + + /// + /// Deletes the specified request. + /// + /// The request. + public void Delete(DeleteItemByNameRating request) + { + var task = MarkLike(request.UserId, request.Name, null); + + Task.WaitAll(task); + } + + /// + /// Marks the favorite. + /// + /// The user id. + /// The key. + /// if set to true [is favorite]. + /// Task. + protected async Task MarkFavorite(Guid userId, string key, bool isFavorite) + { + // Get the user data for this item + var data = await UserDataRepository.GetUserData(userId, key).ConfigureAwait(false); + + // Set favorite status + data.IsFavorite = isFavorite; + + await UserDataRepository.SaveUserData(userId, key, data, CancellationToken.None).ConfigureAwait(false); + } + + /// + /// Marks the like. + /// + /// The user id. + /// The key. + /// if set to true [likes]. + /// Task. + protected async Task MarkLike(Guid userId, string key, bool? likes) + { + // Get the user data for this item + var data = await UserDataRepository.GetUserData(userId, key).ConfigureAwait(false); + + data.Likes = likes; + + await UserDataRepository.SaveUserData(userId, key, data, CancellationToken.None).ConfigureAwait(false); + } + } +} diff --git a/MediaBrowser.Api/UserLibrary/UserLibraryService.cs b/MediaBrowser.Api/UserLibrary/UserLibraryService.cs index 70e5287a1..964315fd2 100644 --- a/MediaBrowser.Api/UserLibrary/UserLibraryService.cs +++ b/MediaBrowser.Api/UserLibrary/UserLibraryService.cs @@ -161,7 +161,7 @@ namespace MediaBrowser.Api.UserLibrary public string Id { get; set; } /// - /// Gets or sets a value indicating whether this is likes. + /// Gets or sets a value indicating whether this is likes. /// /// true if likes; otherwise, false. [ApiMember(Name = "Likes", Description = "Whether the user likes the item or not. true/false", IsRequired = true, DataType = "boolean", ParameterType = "query", Verb = "POST")] From 5231cab777e37c908ef1650bae86c7b0ecb32499 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sat, 13 Apr 2013 15:24:34 -0400 Subject: [PATCH 2/2] added ability to mark IBN items as favorites --- .../UserLibrary/ItemByNameUserDataService.cs | 7 +- MediaBrowser.Controller/Dto/DtoBuilder.cs | 2 +- MediaBrowser.WebDashboard/ApiClient.js | 66 ++++++++++++------- MediaBrowser.WebDashboard/packages.config | 2 +- 4 files changed, 49 insertions(+), 28 deletions(-) diff --git a/MediaBrowser.Api/UserLibrary/ItemByNameUserDataService.cs b/MediaBrowser.Api/UserLibrary/ItemByNameUserDataService.cs index f872fb680..e068eb216 100644 --- a/MediaBrowser.Api/UserLibrary/ItemByNameUserDataService.cs +++ b/MediaBrowser.Api/UserLibrary/ItemByNameUserDataService.cs @@ -1,4 +1,5 @@ -using MediaBrowser.Controller.Persistence; +using MediaBrowser.Controller.Dto; +using MediaBrowser.Controller.Persistence; using ServiceStack.ServiceHost; using System; using System.Threading; @@ -146,7 +147,7 @@ namespace MediaBrowser.Api.UserLibrary // Get the user data for this item var data = UserDataRepository.GetUserData(request.UserId, request.Name).Result; - return ToOptimizedResult(data); + return ToOptimizedResult(DtoBuilder.GetUserItemDataDto(data)); } /// @@ -175,7 +176,7 @@ namespace MediaBrowser.Api.UserLibrary /// Deletes the specified request. /// /// The request. - public void Delete(MarkItemByNameFavorite request) + public void Delete(UnmarkItemByNameFavorite request) { var task = MarkFavorite(request.UserId, request.Name, false); diff --git a/MediaBrowser.Controller/Dto/DtoBuilder.cs b/MediaBrowser.Controller/Dto/DtoBuilder.cs index 0da4da356..0c47013bf 100644 --- a/MediaBrowser.Controller/Dto/DtoBuilder.cs +++ b/MediaBrowser.Controller/Dto/DtoBuilder.cs @@ -682,7 +682,7 @@ namespace MediaBrowser.Controller.Dto /// The data. /// DtoUserItemData. /// - public UserItemDataDto GetUserItemDataDto(UserItemData data) + public static UserItemDataDto GetUserItemDataDto(UserItemData data) { if (data == null) { diff --git a/MediaBrowser.WebDashboard/ApiClient.js b/MediaBrowser.WebDashboard/ApiClient.js index 65012f94d..4c88b35c3 100644 --- a/MediaBrowser.WebDashboard/ApiClient.js +++ b/MediaBrowser.WebDashboard/ApiClient.js @@ -1724,12 +1724,12 @@ MediaBrowser.ApiClient = function ($, navigator, JSON, WebSocket, setTimeout) { }; /** - * Updates a user's favorite status for a person. + * Updates a user's favorite status for an item by name. * @param {String} userId * @param {String} name * @param {Boolean} isFavorite */ - self.updateFavoritePersonStatus = function (userId, name, isFavorite) { + self.updateItemByNameFavoriteStatus = function (userId, name, isFavorite) { if (!userId) { throw new Error("null userId"); @@ -1739,7 +1739,7 @@ MediaBrowser.ApiClient = function ($, navigator, JSON, WebSocket, setTimeout) { throw new Error("null name"); } - var url = self.getUrl("Users/" + userId + "/FavoritePersons/" + name); + var url = self.getUrl("Users/" + userId + "/ItemsByName/Favorites/" + name); var method = isFavorite ? "POST" : "DELETE"; @@ -1751,12 +1751,12 @@ MediaBrowser.ApiClient = function ($, navigator, JSON, WebSocket, setTimeout) { }; /** - * Updates a user's favorite status for a genre. - * @param {String} userId - * @param {String} name - * @param {Boolean} isFavorite - */ - self.updateFavoriteGenreStatus = function (userId, name, isFavorite) { + * Updates a user's rating for an item by name. + * @param {String} userId + * @param {String} name + * @param {Boolean} likes + */ + self.updateItemByNameRating = function (userId, name, likes) { if (!userId) { throw new Error("null userId"); @@ -1766,24 +1766,46 @@ MediaBrowser.ApiClient = function ($, navigator, JSON, WebSocket, setTimeout) { throw new Error("null name"); } - var url = self.getUrl("Users/" + userId + "/FavoriteGenre/" + name); - - var method = isFavorite ? "POST" : "DELETE"; + var url = self.getUrl("Users/" + userId + "/ItemsByName/" + name + "/Rating", { + likes: likes + }); return self.ajax({ - type: method, + type: "POST", + url: url + }); + }; + + /** + * Clears a user's rating for an item by name. + * @param {String} userId + * @param {String} name + */ + self.clearItemByNameRating = function (userId, name) { + + if (!userId) { + throw new Error("null userId"); + } + + if (!name) { + throw new Error("null name"); + } + + var url = self.getUrl("Users/" + userId + "/ItemsByName/" + name + "/Rating"); + + return self.ajax({ + type: "DELETE", 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) { + * Gets the full user data object for an item by name. + * @param {String} userId + * @param {String} name + */ + self.getItembyNameUserData = function (userId, name) { if (!userId) { throw new Error("null userId"); @@ -1793,12 +1815,10 @@ MediaBrowser.ApiClient = function ($, navigator, JSON, WebSocket, setTimeout) { throw new Error("null name"); } - var url = self.getUrl("Users/" + userId + "/FavoriteStudios/" + name); - - var method = isFavorite ? "POST" : "DELETE"; + var url = self.getUrl("Users/" + userId + "/ItemsByName/" + name + "/UserData"); return self.ajax({ - type: method, + type: "GET", url: url, dataType: "json" }); diff --git a/MediaBrowser.WebDashboard/packages.config b/MediaBrowser.WebDashboard/packages.config index 74c1b370f..01740f591 100644 --- a/MediaBrowser.WebDashboard/packages.config +++ b/MediaBrowser.WebDashboard/packages.config @@ -1,6 +1,6 @@  - + \ No newline at end of file