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
{
///
/// Class UpdateDisplayPreferences
///
[Route("/DisplayPreferences/{DisplayPreferencesId}", "POST")]
[Api(("Updates a user's display preferences for an item"))]
public class UpdateDisplayPreferences : DisplayPreferences, IReturnVoid
{
///
/// Gets or sets the id.
///
/// The id.
[ApiMember(Name = "DisplayPreferencesId", Description = "DisplayPreferences Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public Guid DisplayPreferencesId { get; set; }
}
[Route("/DisplayPreferences/{Id}", "GET")]
[Api(("Gets a user's display preferences for an item"))]
public class GetDisplayPreferences : IReturn
{
///
/// Gets or sets the id.
///
/// The id.
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public Guid Id { get; set; }
}
///
/// Class DisplayPreferencesService
///
public class DisplayPreferencesService : BaseApiService
{
///
/// The _user manager
///
private readonly IUserManager _userManager;
///
/// The _json serializer
///
private readonly IJsonSerializer _jsonSerializer;
///
/// Initializes a new instance of the class.
///
/// The user manager.
/// The json serializer.
public DisplayPreferencesService(IUserManager userManager, IJsonSerializer jsonSerializer)
{
_userManager = userManager;
_jsonSerializer = jsonSerializer;
}
///
/// Gets the specified request.
///
/// The request.
public object Get(GetDisplayPreferences request)
{
var task = _userManager.GetDisplayPreferences(request.Id);
return ToOptimizedResult(task.Result);
}
///
/// Posts the specified request.
///
/// The request.
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 displayPreferencesId = new Guid(pathInfo.GetArgumentValue(1));
// Serialize to json and then back so that the core doesn't see the request dto type
var displayPreferences = _jsonSerializer.DeserializeFromString(_jsonSerializer.SerializeToString(request));
var task = _userManager.SaveDisplayPreferences(displayPreferences, CancellationToken.None);
Task.WaitAll(task);
}
}
}