jellyfin/MediaBrowser.Api/UserLibrary/ItemByNameUserDataService.cs

265 lines
10 KiB
C#
Raw Normal View History

using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Dto;
2013-10-02 17:23:10 +00:00
using MediaBrowser.Model.Entities;
2013-12-07 15:52:38 +00:00
using ServiceStack;
using System;
using System.Threading;
using System.Threading.Tasks;
2013-12-07 15:52:38 +00:00
using ServiceStack.Text.Controller;
namespace MediaBrowser.Api.UserLibrary
{
/// <summary>
/// Class MarkItemByNameFavorite
/// </summary>
2013-04-22 04:38:03 +00:00
[Route("/Users/{UserId}/Favorites/Artists/{Name}", "POST")]
[Route("/Users/{UserId}/Favorites/Persons/{Name}", "POST")]
[Route("/Users/{UserId}/Favorites/Studios/{Name}", "POST")]
[Route("/Users/{UserId}/Favorites/Genres/{Name}", "POST")]
2013-06-11 03:31:00 +00:00
[Route("/Users/{UserId}/Favorites/MusicGenres/{Name}", "POST")]
2013-07-01 17:17:33 +00:00
[Route("/Users/{UserId}/Favorites/GameGenres/{Name}", "POST")]
[Api(Description = "Marks something as a favorite")]
public class MarkItemByNameFavorite : IReturn<UserItemDataDto>
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public Guid UserId { get; set; }
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>The name.</value>
2013-04-22 04:38:03 +00:00
[ApiMember(Name = "Name", Description = "The name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public string Name { get; set; }
}
/// <summary>
/// Class UnmarkItemByNameFavorite
/// </summary>
2013-04-22 04:38:03 +00:00
[Route("/Users/{UserId}/Favorites/Artists/{Name}", "DELETE")]
[Route("/Users/{UserId}/Favorites/Persons/{Name}", "DELETE")]
[Route("/Users/{UserId}/Favorites/Studios/{Name}", "DELETE")]
[Route("/Users/{UserId}/Favorites/Genres/{Name}", "DELETE")]
2013-06-11 03:31:00 +00:00
[Route("/Users/{UserId}/Favorites/MusicGenres/{Name}", "DELETE")]
2013-07-01 17:17:33 +00:00
[Route("/Users/{UserId}/Favorites/GameGenres/{Name}", "DELETE")]
[Api(Description = "Unmarks something as a favorite")]
public class UnmarkItemByNameFavorite : IReturn<UserItemDataDto>
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
public Guid UserId { get; set; }
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>The name.</value>
2013-04-22 04:38:03 +00:00
[ApiMember(Name = "Name", Description = "The name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
public string Name { get; set; }
}
2013-04-22 04:38:03 +00:00
/// <summary>
/// Class UpdateItemByNameRating
/// </summary>
[Route("/Users/{UserId}/Ratings/Artists/{Name}", "POST")]
[Route("/Users/{UserId}/Ratings/Persons/{Name}", "POST")]
[Route("/Users/{UserId}/Ratings/Studios/{Name}", "POST")]
[Route("/Users/{UserId}/Ratings/Genres/{Name}", "POST")]
2013-06-11 03:31:00 +00:00
[Route("/Users/{UserId}/Ratings/MusicGenres/{Name}", "POST")]
[Api(Description = "Updates a user's rating for an item")]
public class UpdateItemByNameRating : IReturn<UserItemDataDto>
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public Guid UserId { get; set; }
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>The name.</value>
2013-04-22 04:38:03 +00:00
[ApiMember(Name = "Name", Description = "The name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public string Name { get; set; }
/// <summary>
/// Gets or sets a value indicating whether this <see cref="UpdateUserItemRating" /> is likes.
/// </summary>
/// <value><c>true</c> if likes; otherwise, <c>false</c>.</value>
[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; }
}
2013-04-22 04:38:03 +00:00
/// <summary>
/// Class DeleteItemByNameRating
/// </summary>
[Route("/Users/{UserId}/Ratings/Artists/{Name}", "DELETE")]
[Route("/Users/{UserId}/Ratings/Persons/{Name}", "DELETE")]
[Route("/Users/{UserId}/Ratings/Studios/{Name}", "DELETE")]
[Route("/Users/{UserId}/Ratings/Genres/{Name}", "DELETE")]
2013-06-11 03:31:00 +00:00
[Route("/Users/{UserId}/Ratings/MusicGenres/{Name}", "DELETE")]
2013-07-01 17:17:33 +00:00
[Route("/Users/{UserId}/Ratings/GameGenres/{Name}", "DELETE")]
[Api(Description = "Deletes a user's saved personal rating for an item")]
public class DeleteItemByNameRating : IReturn<UserItemDataDto>
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
public Guid UserId { get; set; }
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>The name.</value>
2013-04-22 04:38:03 +00:00
[ApiMember(Name = "Name", Description = "The item name (genre, person, year, studio, artist)", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
public string Name { get; set; }
}
2013-04-22 04:38:03 +00:00
/// <summary>
/// Class ItemByNameUserDataService
/// </summary>
public class ItemByNameUserDataService : BaseApiService
{
/// <summary>
/// The user data repository
/// </summary>
2013-10-02 16:08:58 +00:00
protected readonly IUserDataManager UserDataRepository;
2013-04-22 04:38:03 +00:00
/// <summary>
/// The library manager
/// </summary>
protected readonly ILibraryManager LibraryManager;
2013-09-04 17:02:19 +00:00
private readonly IDtoService _dtoService;
2013-04-22 04:38:03 +00:00
/// <summary>
/// Initializes a new instance of the <see cref="ItemByNameUserDataService" /> class.
/// </summary>
/// <param name="userDataRepository">The user data repository.</param>
2013-04-22 04:38:03 +00:00
/// <param name="libraryManager">The library manager.</param>
2013-10-02 16:08:58 +00:00
public ItemByNameUserDataService(IUserDataManager userDataRepository, ILibraryManager libraryManager, IDtoService dtoService)
{
UserDataRepository = userDataRepository;
2013-04-22 04:38:03 +00:00
LibraryManager = libraryManager;
2013-09-04 17:02:19 +00:00
_dtoService = dtoService;
}
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
public object Post(MarkItemByNameFavorite request)
{
2013-12-07 15:52:38 +00:00
var pathInfo = PathInfo.Parse(Request.PathInfo);
2013-04-22 04:38:03 +00:00
var type = pathInfo.GetArgumentValue<string>(3);
var task = MarkFavorite(request.UserId, type, request.Name, true);
return ToOptimizedResult(task.Result);
}
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
public object Post(UpdateItemByNameRating request)
{
2013-12-07 15:52:38 +00:00
var pathInfo = PathInfo.Parse(Request.PathInfo);
2013-04-22 04:38:03 +00:00
var type = pathInfo.GetArgumentValue<string>(3);
var task = MarkLike(request.UserId, type, request.Name, request.Likes);
return ToOptimizedResult(task.Result);
}
2013-04-22 04:38:03 +00:00
/// <summary>
/// Deletes the specified request.
/// </summary>
/// <param name="request">The request.</param>
public object Delete(UnmarkItemByNameFavorite request)
{
2013-12-07 15:52:38 +00:00
var pathInfo = PathInfo.Parse(Request.PathInfo);
2013-04-22 04:38:03 +00:00
var type = pathInfo.GetArgumentValue<string>(3);
var task = MarkFavorite(request.UserId, type, request.Name, false);
return ToOptimizedResult(task.Result);
}
/// <summary>
/// Deletes the specified request.
/// </summary>
/// <param name="request">The request.</param>
public object Delete(DeleteItemByNameRating request)
{
2013-12-07 15:52:38 +00:00
var pathInfo = PathInfo.Parse(Request.PathInfo);
2013-04-22 04:38:03 +00:00
var type = pathInfo.GetArgumentValue<string>(3);
var task = MarkLike(request.UserId, type, request.Name, null);
return ToOptimizedResult(task.Result);
}
/// <summary>
/// Marks the favorite.
/// </summary>
/// <param name="userId">The user id.</param>
2013-04-22 04:38:03 +00:00
/// <param name="type">The type.</param>
/// <param name="name">The name.</param>
/// <param name="isFavorite">if set to <c>true</c> [is favorite].</param>
/// <returns>Task.</returns>
protected async Task<UserItemDataDto> MarkFavorite(Guid userId, string type, string name, bool isFavorite)
{
var item = GetItemByName(name, type, LibraryManager);
2013-04-22 04:38:03 +00:00
var key = item.GetUserDataKey();
2013-06-17 20:35:43 +00:00
// Get the user data for this item
2013-06-17 20:35:43 +00:00
var data = UserDataRepository.GetUserData(userId, key);
// Set favorite status
data.IsFavorite = isFavorite;
2013-10-23 16:03:12 +00:00
await UserDataRepository.SaveUserData(userId, item, data, UserDataSaveReason.UpdateUserRating, CancellationToken.None).ConfigureAwait(false);
data = UserDataRepository.GetUserData(userId, key);
2013-09-04 17:02:19 +00:00
return _dtoService.GetUserItemDataDto(data);
}
/// <summary>
/// Marks the like.
/// </summary>
/// <param name="userId">The user id.</param>
2013-04-22 04:38:03 +00:00
/// <param name="type">The type.</param>
/// <param name="name">The name.</param>
/// <param name="likes">if set to <c>true</c> [likes].</param>
/// <returns>Task.</returns>
protected async Task<UserItemDataDto> MarkLike(Guid userId, string type, string name, bool? likes)
{
var item = GetItemByName(name, type, LibraryManager);
2013-04-22 04:38:03 +00:00
var key = item.GetUserDataKey();
2013-06-17 20:35:43 +00:00
// Get the user data for this item
2013-06-17 20:35:43 +00:00
var data = UserDataRepository.GetUserData(userId, key);
data.Likes = likes;
2013-10-23 16:03:12 +00:00
await UserDataRepository.SaveUserData(userId, item, data, UserDataSaveReason.UpdateUserRating, CancellationToken.None).ConfigureAwait(false);
data = UserDataRepository.GetUserData(userId, key);
2013-09-04 17:02:19 +00:00
return _dtoService.GetUserItemDataDto(data);
}
}
}