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); } } }