BaseItemsByNameService.cs 5.4 KB

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