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 = "GET")] public Guid Id { get; set; } } /// /// Class DisplayPreferencesService /// public class DisplayPreferencesService : BaseApiService { /// /// The _display preferences manager /// private readonly IDisplayPreferencesManager _displayPreferencesManager; /// /// The _json serializer /// private readonly IJsonSerializer _jsonSerializer; /// /// Initializes a new instance of the class. /// /// The json serializer. /// The display preferences manager. public DisplayPreferencesService(IJsonSerializer jsonSerializer, IDisplayPreferencesManager displayPreferencesManager) { _jsonSerializer = jsonSerializer; _displayPreferencesManager = displayPreferencesManager; } /// /// Gets the specified request. /// /// The request. public object Get(GetDisplayPreferences request) { var task = _displayPreferencesManager.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 = _displayPreferencesManager.SaveDisplayPreferences(displayPreferences, CancellationToken.None); Task.WaitAll(task); } } }