BaseItemsByNameService.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using MediaBrowser.Common.Implementations.HttpServer;
  2. using MediaBrowser.Controller;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Controller.Library;
  5. using MediaBrowser.Model.Dto;
  6. using MediaBrowser.Model.Entities;
  7. using ServiceStack.ServiceHost;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Threading.Tasks;
  13. namespace MediaBrowser.Api.UserLibrary
  14. {
  15. /// <summary>
  16. /// Class BaseItemsByNameService
  17. /// </summary>
  18. /// <typeparam name="TItemType">The type of the T item type.</typeparam>
  19. public abstract class BaseItemsByNameService<TItemType> : BaseRestService
  20. where TItemType : BaseItem
  21. {
  22. /// <summary>
  23. /// Gets the specified request.
  24. /// </summary>
  25. /// <param name="request">The request.</param>
  26. /// <returns>Task{ItemsResult}.</returns>
  27. protected async Task<ItemsResult> GetResult(GetItemsByName request)
  28. {
  29. var kernel = (Kernel)Kernel;
  30. var user = kernel.GetUserById(request.UserId);
  31. var item = string.IsNullOrEmpty(request.Id) ? user.RootFolder : DtoBuilder.GetItemByClientId(request.Id, user.Id);
  32. IEnumerable<BaseItem> items;
  33. if (item.IsFolder)
  34. {
  35. var folder = (Folder)item;
  36. items = request.Recursive ? folder.GetRecursiveChildren(user) : folder.GetChildren(user);
  37. }
  38. else
  39. {
  40. items = new[] { item };
  41. }
  42. var ibnItemsArray = GetAllItems(request, items, user).ToArray();
  43. IEnumerable<Tuple<string, Func<int>>> ibnItems = ibnItemsArray;
  44. var result = new ItemsResult
  45. {
  46. TotalRecordCount = ibnItemsArray.Length
  47. };
  48. if (request.StartIndex.HasValue || request.PageSize.HasValue)
  49. {
  50. if (request.StartIndex.HasValue)
  51. {
  52. ibnItems = ibnItems.Skip(request.StartIndex.Value);
  53. }
  54. if (request.PageSize.HasValue)
  55. {
  56. ibnItems = ibnItems.Take(request.PageSize.Value);
  57. }
  58. }
  59. var tasks = ibnItems.Select(i => GetDto(i, user, new List<ItemFields>()));
  60. var resultItems = await Task.WhenAll(tasks).ConfigureAwait(false);
  61. result.Items = resultItems.Where(i => i != null).OrderByDescending(i => i.SortName ?? i.Name).ToArray();
  62. return result;
  63. }
  64. /// <summary>
  65. /// Gets all items.
  66. /// </summary>
  67. /// <param name="request">The request.</param>
  68. /// <param name="items">The items.</param>
  69. /// <param name="user">The user.</param>
  70. /// <returns>IEnumerable{Tuple{System.StringFunc{System.Int32}}}.</returns>
  71. protected abstract IEnumerable<Tuple<string, Func<int>>> GetAllItems(GetItemsByName request, IEnumerable<BaseItem> items, User user);
  72. /// <summary>
  73. /// Gets the entity.
  74. /// </summary>
  75. /// <param name="name">The name.</param>
  76. /// <returns>Task{BaseItem}.</returns>
  77. protected abstract Task<TItemType> GetEntity(string name);
  78. /// <summary>
  79. /// Gets the dto.
  80. /// </summary>
  81. /// <param name="stub">The stub.</param>
  82. /// <param name="user">The user.</param>
  83. /// <param name="fields">The fields.</param>
  84. /// <returns>Task{DtoBaseItem}.</returns>
  85. private async Task<BaseItemDto> GetDto(Tuple<string, Func<int>> stub, User user, List<ItemFields> fields)
  86. {
  87. BaseItem item;
  88. try
  89. {
  90. item = await GetEntity(stub.Item1).ConfigureAwait(false);
  91. }
  92. catch (IOException ex)
  93. {
  94. Logger.ErrorException("Error getting IBN item {0}", ex, stub.Item1);
  95. return null;
  96. }
  97. var dto = await new DtoBuilder(Logger).GetDtoBaseItem(item, user, fields).ConfigureAwait(false);
  98. dto.ChildCount = stub.Item2();
  99. return dto;
  100. }
  101. }
  102. /// <summary>
  103. /// Class GetItemsByName
  104. /// </summary>
  105. public class GetItemsByName : IReturn<ItemsResult>
  106. {
  107. /// <summary>
  108. /// Gets or sets the user id.
  109. /// </summary>
  110. /// <value>The user id.</value>
  111. public Guid UserId { get; set; }
  112. /// <summary>
  113. /// Gets or sets the start index.
  114. /// </summary>
  115. /// <value>The start index.</value>
  116. public int? StartIndex { get; set; }
  117. /// <summary>
  118. /// Gets or sets the size of the page.
  119. /// </summary>
  120. /// <value>The size of the page.</value>
  121. public int? PageSize { get; set; }
  122. /// <summary>
  123. /// Gets or sets a value indicating whether this <see cref="GetItemsByName" /> is recursive.
  124. /// </summary>
  125. /// <value><c>true</c> if recursive; otherwise, <c>false</c>.</value>
  126. public bool Recursive { get; set; }
  127. /// <summary>
  128. /// Gets or sets the sort order.
  129. /// </summary>
  130. /// <value>The sort order.</value>
  131. public SortOrder? SortOrder { get; set; }
  132. /// <summary>
  133. /// If specified the search will be localized within a specific item or folder
  134. /// </summary>
  135. /// <value>The item id.</value>
  136. public string Id { get; set; }
  137. }
  138. }