DisplayPreferencesService.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using MediaBrowser.Controller.Library;
  2. using MediaBrowser.Model.Entities;
  3. using MediaBrowser.Model.Serialization;
  4. using ServiceStack.ServiceHost;
  5. using ServiceStack.Text.Controller;
  6. using System;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace MediaBrowser.Api
  10. {
  11. /// <summary>
  12. /// Class UpdateDisplayPreferences
  13. /// </summary>
  14. [Route("/Users/{UserId}/DisplayPreferences/{Id}", "POST")]
  15. [Api(("Updates a user's display preferences for an item"))]
  16. public class UpdateDisplayPreferences : DisplayPreferences, IReturnVoid
  17. {
  18. /// <summary>
  19. /// Gets or sets the id.
  20. /// </summary>
  21. /// <value>The id.</value>
  22. [ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  23. public Guid Id { get; set; }
  24. }
  25. [Route("/Users/{UserId}/DisplayPreferences/{Id}", "GET")]
  26. [Api(("Gets a user's display preferences for an item"))]
  27. public class GetDisplayPreferences : IReturn<DisplayPreferences>
  28. {
  29. [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  30. public Guid UserId { get; set; }
  31. /// <summary>
  32. /// Gets or sets the id.
  33. /// </summary>
  34. /// <value>The id.</value>
  35. [ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  36. public Guid Id { get; set; }
  37. }
  38. /// <summary>
  39. /// Class DisplayPreferencesService
  40. /// </summary>
  41. public class DisplayPreferencesService : BaseApiService
  42. {
  43. /// <summary>
  44. /// The _user manager
  45. /// </summary>
  46. private readonly IUserManager _userManager;
  47. /// <summary>
  48. /// The _json serializer
  49. /// </summary>
  50. private readonly IJsonSerializer _jsonSerializer;
  51. /// <summary>
  52. /// Initializes a new instance of the <see cref="DisplayPreferencesService"/> class.
  53. /// </summary>
  54. /// <param name="userManager">The user manager.</param>
  55. /// <param name="jsonSerializer">The json serializer.</param>
  56. public DisplayPreferencesService(IUserManager userManager, IJsonSerializer jsonSerializer)
  57. {
  58. _userManager = userManager;
  59. _jsonSerializer = jsonSerializer;
  60. }
  61. /// <summary>
  62. /// Gets the specified request.
  63. /// </summary>
  64. /// <param name="request">The request.</param>
  65. public object Get(GetDisplayPreferences request)
  66. {
  67. var task = _userManager.GetDisplayPreferences(request.UserId, request.Id);
  68. return ToOptimizedResult(task.Result);
  69. }
  70. /// <summary>
  71. /// Posts the specified request.
  72. /// </summary>
  73. /// <param name="request">The request.</param>
  74. public void Post(UpdateDisplayPreferences request)
  75. {
  76. // We need to parse this manually because we told service stack not to with IRequiresRequestStream
  77. // https://code.google.com/p/servicestack/source/browse/trunk/Common/ServiceStack.Text/ServiceStack.Text/Controller/PathInfo.cs
  78. var pathInfo = PathInfo.Parse(RequestContext.PathInfo);
  79. var userId = new Guid(pathInfo.GetArgumentValue<string>(1));
  80. var displayPreferencesId = new Guid(pathInfo.GetArgumentValue<string>(3));
  81. var user = _userManager.GetUserById(userId);
  82. // Serialize to json and then back so that the core doesn't see the request dto type
  83. var displayPreferences = _jsonSerializer.DeserializeFromString<DisplayPreferences>(_jsonSerializer.SerializeToString(request));
  84. var task = _userManager.SaveDisplayPreferences(user.Id, displayPreferencesId, displayPreferences, CancellationToken.None);
  85. Task.WaitAll(task);
  86. }
  87. }
  88. }