Get/Set DisplayPreferences separately through the api

This commit is contained in:
Luke Pulverenti 2013-04-05 13:10:55 -04:00
parent 03a36d4628
commit 1bf4b6110d
8 changed files with 124 additions and 72 deletions

View File

@ -0,0 +1,100 @@
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Serialization;
using ServiceStack.ServiceHost;
using ServiceStack.Text.Controller;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Api
{
/// <summary>
/// Class UpdateDisplayPreferences
/// </summary>
[Route("/Users/{UserId}/DisplayPreferences/{Id}", "POST")]
[Api(("Updates a user's display preferences for an item"))]
public class UpdateDisplayPreferences : DisplayPreferences, IReturnVoid
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public Guid Id { get; set; }
}
[Route("/Users/{UserId}/DisplayPreferences/{Id}", "GET")]
[Api(("Gets a user's display preferences for an item"))]
public class GetDisplayPreferences : IReturn<DisplayPreferences>
{
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public Guid UserId { get; set; }
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public Guid Id { get; set; }
}
/// <summary>
/// Class DisplayPreferencesService
/// </summary>
public class DisplayPreferencesService : BaseApiService
{
/// <summary>
/// The _user manager
/// </summary>
private readonly IUserManager _userManager;
/// <summary>
/// The _json serializer
/// </summary>
private readonly IJsonSerializer _jsonSerializer;
/// <summary>
/// Initializes a new instance of the <see cref="DisplayPreferencesService"/> class.
/// </summary>
/// <param name="userManager">The user manager.</param>
/// <param name="jsonSerializer">The json serializer.</param>
public DisplayPreferencesService(IUserManager userManager, IJsonSerializer jsonSerializer)
{
_userManager = userManager;
_jsonSerializer = jsonSerializer;
}
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
public object Get(GetDisplayPreferences request)
{
var task = _userManager.GetDisplayPreferences(request.UserId, request.Id);
return ToOptimizedResult(task.Result);
}
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
public void Post(UpdateDisplayPreferences request)
{
// We need to parse this manually because we told service stack not to with IRequiresRequestStream
// https://code.google.com/p/servicestack/source/browse/trunk/Common/ServiceStack.Text/ServiceStack.Text/Controller/PathInfo.cs
var pathInfo = PathInfo.Parse(RequestContext.PathInfo);
var userId = new Guid(pathInfo.GetArgumentValue<string>(1));
var displayPreferencesId = new Guid(pathInfo.GetArgumentValue<string>(3));
var user = _userManager.GetUserById(userId);
// Serialize to json and then back so that the core doesn't see the request dto type
var displayPreferences = _jsonSerializer.DeserializeFromString<DisplayPreferences>(_jsonSerializer.SerializeToString(request));
var task = _userManager.SaveDisplayPreferences(user.Id, displayPreferencesId, displayPreferences, CancellationToken.None);
Task.WaitAll(task);
}
}
}

View File

@ -62,6 +62,7 @@
<Link>Properties\SharedVersion.cs</Link> <Link>Properties\SharedVersion.cs</Link>
</Compile> </Compile>
<Compile Include="BaseApiService.cs" /> <Compile Include="BaseApiService.cs" />
<Compile Include="DisplayPreferencesService.cs" />
<Compile Include="EnvironmentService.cs" /> <Compile Include="EnvironmentService.cs" />
<Compile Include="Images\ImageRequest.cs" /> <Compile Include="Images\ImageRequest.cs" />
<Compile Include="Images\ImageService.cs" /> <Compile Include="Images\ImageService.cs" />

View File

@ -52,7 +52,7 @@ namespace MediaBrowser.Api.UserLibrary
/// Fields to return within the items, in addition to basic information /// Fields to return within the items, in addition to basic information
/// </summary> /// </summary>
/// <value>The fields.</value> /// <value>The fields.</value>
[ApiMember(Name = "Fields", Description = "Optional. Specify additional fields of information to return in the output. This allows multiple, comma delimeted. Options: AudioInfo, Chapters, DateCreated, DisplayMediaType, DisplayPreferences, Genres, ItemCounts, IndexOptions, MediaStreams, Overview, OverviewHtml, ParentId, Path, People, ProviderIds, PrimaryImageAspectRatio, Publishers, SeriesInfo, SortName, Studios, Taglines, TrailerUrls, UserData", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)] [ApiMember(Name = "Fields", Description = "Optional. Specify additional fields of information to return in the output. This allows multiple, comma delimeted. Options: AudioInfo, Chapters, DateCreated, DisplayMediaType, DisplayPreferences, Genres, ItemCounts, IndexOptions, MediaStreams, Overview, OverviewHtml, ParentId, Path, People, ProviderIds, PrimaryImageAspectRatio, SeriesInfo, SortName, Studios, Taglines, TrailerUrls, UserData", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]
public string Fields { get; set; } public string Fields { get; set; }
} }
} }

View File

@ -1,17 +1,13 @@
using System.Threading; using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies; using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Connectivity;
using MediaBrowser.Model.Dto; using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Querying; using MediaBrowser.Model.Querying;
using MediaBrowser.Model.Serialization;
using ServiceStack.ServiceHost; using ServiceStack.ServiceHost;
using ServiceStack.Text.Controller;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace MediaBrowser.Api.UserLibrary namespace MediaBrowser.Api.UserLibrary
@ -20,7 +16,7 @@ namespace MediaBrowser.Api.UserLibrary
/// Class GetItem /// Class GetItem
/// </summary> /// </summary>
[Route("/Users/{UserId}/Items/{Id}", "GET")] [Route("/Users/{UserId}/Items/{Id}", "GET")]
[ServiceStack.ServiceHost.Api(Description = "Gets an item from a user's library")] [Api(Description = "Gets an item from a user's library")]
public class GetItem : IReturn<BaseItemDto> public class GetItem : IReturn<BaseItemDto>
{ {
/// <summary> /// <summary>
@ -42,7 +38,7 @@ namespace MediaBrowser.Api.UserLibrary
/// Class GetItem /// Class GetItem
/// </summary> /// </summary>
[Route("/Users/{UserId}/Items/Root", "GET")] [Route("/Users/{UserId}/Items/Root", "GET")]
[ServiceStack.ServiceHost.Api(Description = "Gets the root folder from a user's library")] [Api(Description = "Gets the root folder from a user's library")]
public class GetRootFolder : IReturn<BaseItemDto> public class GetRootFolder : IReturn<BaseItemDto>
{ {
/// <summary> /// <summary>
@ -57,7 +53,7 @@ namespace MediaBrowser.Api.UserLibrary
/// Class GetIntros /// Class GetIntros
/// </summary> /// </summary>
[Route("/Users/{UserId}/Items/{Id}/Intros", "GET")] [Route("/Users/{UserId}/Items/{Id}/Intros", "GET")]
[ServiceStack.ServiceHost.Api(("Gets intros to play before the main media item plays"))] [Api(("Gets intros to play before the main media item plays"))]
public class GetIntros : IReturn<List<string>> public class GetIntros : IReturn<List<string>>
{ {
/// <summary> /// <summary>
@ -75,21 +71,6 @@ namespace MediaBrowser.Api.UserLibrary
public string Id { get; set; } public string Id { get; set; }
} }
/// <summary>
/// Class UpdateDisplayPreferences
/// </summary>
[Route("/Users/{UserId}/Items/{Id}/DisplayPreferences", "POST")]
[Api(("Updates a user's display preferences for an item"))]
public class UpdateDisplayPreferences : DisplayPreferences, IReturnVoid
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public string Id { get; set; }
}
/// <summary> /// <summary>
/// Class MarkFavoriteItem /// Class MarkFavoriteItem
/// </summary> /// </summary>
@ -138,7 +119,7 @@ namespace MediaBrowser.Api.UserLibrary
/// Class ClearUserItemRating /// Class ClearUserItemRating
/// </summary> /// </summary>
[Route("/Users/{UserId}/Items/{Id}/Rating", "DELETE")] [Route("/Users/{UserId}/Items/{Id}/Rating", "DELETE")]
[ServiceStack.ServiceHost.Api(Description = "Deletes a user's saved personal rating for an item")] [Api(Description = "Deletes a user's saved personal rating for an item")]
public class DeleteUserItemRating : IReturnVoid public class DeleteUserItemRating : IReturnVoid
{ {
/// <summary> /// <summary>
@ -160,7 +141,7 @@ namespace MediaBrowser.Api.UserLibrary
/// Class UpdateUserItemRating /// Class UpdateUserItemRating
/// </summary> /// </summary>
[Route("/Users/{UserId}/Items/{Id}/Rating", "POST")] [Route("/Users/{UserId}/Items/{Id}/Rating", "POST")]
[ServiceStack.ServiceHost.Api(Description = "Updates a user's rating for an item")] [Api(Description = "Updates a user's rating for an item")]
public class UpdateUserItemRating : IReturnVoid public class UpdateUserItemRating : IReturnVoid
{ {
/// <summary> /// <summary>
@ -189,7 +170,7 @@ namespace MediaBrowser.Api.UserLibrary
/// Class MarkPlayedItem /// Class MarkPlayedItem
/// </summary> /// </summary>
[Route("/Users/{UserId}/PlayedItems/{Id}", "POST")] [Route("/Users/{UserId}/PlayedItems/{Id}", "POST")]
[ServiceStack.ServiceHost.Api(Description = "Marks an item as played")] [Api(Description = "Marks an item as played")]
public class MarkPlayedItem : IReturnVoid public class MarkPlayedItem : IReturnVoid
{ {
/// <summary> /// <summary>
@ -211,7 +192,7 @@ namespace MediaBrowser.Api.UserLibrary
/// Class MarkUnplayedItem /// Class MarkUnplayedItem
/// </summary> /// </summary>
[Route("/Users/{UserId}/PlayedItems/{Id}", "DELETE")] [Route("/Users/{UserId}/PlayedItems/{Id}", "DELETE")]
[ServiceStack.ServiceHost.Api(Description = "Marks an item as unplayed")] [Api(Description = "Marks an item as unplayed")]
public class MarkUnplayedItem : IReturnVoid public class MarkUnplayedItem : IReturnVoid
{ {
/// <summary> /// <summary>
@ -230,7 +211,7 @@ namespace MediaBrowser.Api.UserLibrary
} }
[Route("/Users/{UserId}/PlayingItems/{Id}", "POST")] [Route("/Users/{UserId}/PlayingItems/{Id}", "POST")]
[ServiceStack.ServiceHost.Api(Description = "Reports that a user has begun playing an item")] [Api(Description = "Reports that a user has begun playing an item")]
public class OnPlaybackStart : IReturnVoid public class OnPlaybackStart : IReturnVoid
{ {
/// <summary> /// <summary>
@ -249,7 +230,7 @@ namespace MediaBrowser.Api.UserLibrary
} }
[Route("/Users/{UserId}/PlayingItems/{Id}/Progress", "POST")] [Route("/Users/{UserId}/PlayingItems/{Id}/Progress", "POST")]
[ServiceStack.ServiceHost.Api(Description = "Reports a user's playback progress")] [Api(Description = "Reports a user's playback progress")]
public class OnPlaybackProgress : IReturnVoid public class OnPlaybackProgress : IReturnVoid
{ {
/// <summary> /// <summary>
@ -275,7 +256,7 @@ namespace MediaBrowser.Api.UserLibrary
} }
[Route("/Users/{UserId}/PlayingItems/{Id}", "DELETE")] [Route("/Users/{UserId}/PlayingItems/{Id}", "DELETE")]
[ServiceStack.ServiceHost.Api(Description = "Reports that a user has stopped playing an item")] [Api(Description = "Reports that a user has stopped playing an item")]
public class OnPlaybackStopped : IReturnVoid public class OnPlaybackStopped : IReturnVoid
{ {
/// <summary> /// <summary>
@ -304,7 +285,7 @@ namespace MediaBrowser.Api.UserLibrary
/// Class GetLocalTrailers /// Class GetLocalTrailers
/// </summary> /// </summary>
[Route("/Users/{UserId}/Items/{Id}/LocalTrailers", "GET")] [Route("/Users/{UserId}/Items/{Id}/LocalTrailers", "GET")]
[ServiceStack.ServiceHost.Api(Description = "Gets local trailers for an item")] [Api(Description = "Gets local trailers for an item")]
public class GetLocalTrailers : IReturn<List<BaseItemDto>> public class GetLocalTrailers : IReturn<List<BaseItemDto>>
{ {
/// <summary> /// <summary>
@ -326,7 +307,7 @@ namespace MediaBrowser.Api.UserLibrary
/// Class GetSpecialFeatures /// Class GetSpecialFeatures
/// </summary> /// </summary>
[Route("/Users/{UserId}/Items/{Id}/SpecialFeatures", "GET")] [Route("/Users/{UserId}/Items/{Id}/SpecialFeatures", "GET")]
[ServiceStack.ServiceHost.Api(Description = "Gets special features for a movie")] [Api(Description = "Gets special features for a movie")]
public class GetSpecialFeatures : IReturn<List<BaseItemDto>> public class GetSpecialFeatures : IReturn<List<BaseItemDto>>
{ {
/// <summary> /// <summary>
@ -356,18 +337,16 @@ namespace MediaBrowser.Api.UserLibrary
private readonly IUserManager _userManager; private readonly IUserManager _userManager;
private readonly ILibraryManager _libraryManager; private readonly ILibraryManager _libraryManager;
private readonly IJsonSerializer _jsonSerializer;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="UserLibraryService" /> class. /// Initializes a new instance of the <see cref="UserLibraryService" /> class.
/// </summary> /// </summary>
/// <exception cref="System.ArgumentNullException">jsonSerializer</exception> /// <exception cref="System.ArgumentNullException">jsonSerializer</exception>
public UserLibraryService(IUserManager userManager, ILibraryManager libraryManager, IJsonSerializer jsonSerializer) public UserLibraryService(IUserManager userManager, ILibraryManager libraryManager)
: base() : base()
{ {
_userManager = userManager; _userManager = userManager;
_libraryManager = libraryManager; _libraryManager = libraryManager;
_jsonSerializer = jsonSerializer;
} }
/// <summary> /// <summary>
@ -467,31 +446,6 @@ namespace MediaBrowser.Api.UserLibrary
return ToOptimizedResult(result); return ToOptimizedResult(result);
} }
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
public void Post(UpdateDisplayPreferences request)
{
// We need to parse this manually because we told service stack not to with IRequiresRequestStream
// https://code.google.com/p/servicestack/source/browse/trunk/Common/ServiceStack.Text/ServiceStack.Text/Controller/PathInfo.cs
var pathInfo = PathInfo.Parse(RequestContext.PathInfo);
var userId = new Guid(pathInfo.GetArgumentValue<string>(1));
var itemId = pathInfo.GetArgumentValue<string>(3);
var user = _userManager.GetUserById(userId);
var item = string.IsNullOrEmpty(itemId) ? user.RootFolder : DtoBuilder.GetItemByClientId(itemId, _userManager, _libraryManager, user.Id);
var folder = (Folder)item;
// Serialize to json and then back so that the core doesn't see the request dto type
var displayPreferences = _jsonSerializer.DeserializeFromString<DisplayPreferences>(_jsonSerializer.SerializeToString(request));
var task = _userManager.SaveDisplayPreferences(user.Id, folder.DisplayPreferencesId, displayPreferences, CancellationToken.None);
Task.WaitAll(task);
}
/// <summary> /// <summary>
/// Posts the specified request. /// Posts the specified request.
/// </summary> /// </summary>

View File

@ -172,11 +172,9 @@ namespace MediaBrowser.Controller.Library
dto.UserData = GetUserItemDataDto(userData); dto.UserData = GetUserItemDataDto(userData);
} }
if (item.IsFolder && fields.Contains(ItemFields.DisplayPreferences)) if (item.IsFolder && fields.Contains(ItemFields.DisplayPreferencesId))
{ {
var displayPreferencesId = ((Folder) item).DisplayPreferencesId; dto.DisplayPreferencesId = ((Folder)item).DisplayPreferencesId.ToString();
dto.DisplayPreferences = await _userManager.GetDisplayPreferences(user.Id, displayPreferencesId).ConfigureAwait(false);
} }
if (item.IsFolder) if (item.IsFolder)

View File

@ -207,7 +207,6 @@ namespace MediaBrowser.Controller.Library
/// <param name="displayPreferences">The display preferences.</param> /// <param name="displayPreferences">The display preferences.</param>
/// <param name="cancellationToken">The cancellation token.</param> /// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns> /// <returns>Task.</returns>
Task SaveDisplayPreferences(Guid userId, Guid displayPreferencesId, DisplayPreferences displayPreferences, Task SaveDisplayPreferences(Guid userId, Guid displayPreferencesId, DisplayPreferences displayPreferences, CancellationToken cancellationToken);
CancellationToken cancellationToken);
} }
} }

View File

@ -267,11 +267,11 @@ namespace MediaBrowser.Model.Dto
public int? SpecialFeatureCount { get; set; } public int? SpecialFeatureCount { get; set; }
/// <summary> /// <summary>
/// Gets or sets the display preferences. /// Gets or sets the display preferences id.
/// </summary> /// </summary>
/// <value>The display preferences.</value> /// <value>The display preferences id.</value>
[ProtoMember(49)] [ProtoMember(49)]
public DisplayPreferences DisplayPreferences { get; set; } public string DisplayPreferencesId { get; set; }
/// <summary> /// <summary>
/// Gets or sets the status. /// Gets or sets the status.

View File

@ -29,7 +29,7 @@ namespace MediaBrowser.Model.Querying
/// <summary> /// <summary>
/// Item display preferences /// Item display preferences
/// </summary> /// </summary>
DisplayPreferences, DisplayPreferencesId,
/// <summary> /// <summary>
/// Genres /// Genres