2
0

TvShowsService.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using MediaBrowser.Common.Extensions;
  6. using MediaBrowser.Controller.Dto;
  7. using MediaBrowser.Controller.Entities;
  8. using MediaBrowser.Controller.Entities.TV;
  9. using MediaBrowser.Controller.Library;
  10. using MediaBrowser.Controller.Net;
  11. using MediaBrowser.Controller.Persistence;
  12. using MediaBrowser.Controller.TV;
  13. using MediaBrowser.Model.Dto;
  14. using MediaBrowser.Model.Entities;
  15. using MediaBrowser.Model.Querying;
  16. using MediaBrowser.Model.Services;
  17. namespace MediaBrowser.Api
  18. {
  19. /// <summary>
  20. /// Class GetNextUpEpisodes
  21. /// </summary>
  22. [Route("/Shows/NextUp", "GET", Summary = "Gets a list of next up episodes")]
  23. public class GetNextUpEpisodes : IReturn<QueryResult<BaseItemDto>>, IHasDtoOptions
  24. {
  25. /// <summary>
  26. /// Gets or sets the user id.
  27. /// </summary>
  28. /// <value>The user id.</value>
  29. [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
  30. public Guid UserId { get; set; }
  31. /// <summary>
  32. /// Skips over a given number of items within the results. Use for paging.
  33. /// </summary>
  34. /// <value>The start index.</value>
  35. [ApiMember(Name = "StartIndex", Description = "Optional. The record index to start at. All items with a lower index will be dropped from the results.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  36. public int? StartIndex { get; set; }
  37. /// <summary>
  38. /// The maximum number of items to return
  39. /// </summary>
  40. /// <value>The limit.</value>
  41. [ApiMember(Name = "Limit", Description = "Optional. The maximum number of records to return", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  42. public int? Limit { get; set; }
  43. /// <summary>
  44. /// Fields to return within the items, in addition to basic information
  45. /// </summary>
  46. /// <value>The fields.</value>
  47. [ApiMember(Name = "Fields", Description = "Optional. Specify additional fields of information to return in the output. This allows multiple, comma delimeted. Options: Budget, Chapters, DateCreated, Genres, HomePageUrl, IndexOptions, MediaStreams, Overview, ParentId, Path, People, ProviderIds, PrimaryImageAspectRatio, Revenue, SortName, Studios, Taglines, TrailerUrls", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]
  48. public string Fields { get; set; }
  49. [ApiMember(Name = "SeriesId", Description = "Optional. Filter by series id", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  50. public string SeriesId { get; set; }
  51. /// <summary>
  52. /// Specify this to localize the search to a specific item or folder. Omit to use the root.
  53. /// </summary>
  54. /// <value>The parent id.</value>
  55. [ApiMember(Name = "ParentId", Description = "Specify this to localize the search to a specific item or folder. Omit to use the root", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  56. public string ParentId { get; set; }
  57. [ApiMember(Name = "EnableImages", Description = "Optional, include image information in output", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "GET")]
  58. public bool? EnableImages { get; set; }
  59. [ApiMember(Name = "ImageTypeLimit", Description = "Optional, the max number of images to return, per image type", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  60. public int? ImageTypeLimit { get; set; }
  61. [ApiMember(Name = "EnableImageTypes", Description = "Optional. The image types to include in the output.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  62. public string EnableImageTypes { get; set; }
  63. [ApiMember(Name = "EnableUserData", Description = "Optional, include user data", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "GET")]
  64. public bool? EnableUserData { get; set; }
  65. public bool EnableTotalRecordCount { get; set; }
  66. public GetNextUpEpisodes()
  67. {
  68. EnableTotalRecordCount = true;
  69. }
  70. }
  71. [Route("/Shows/Upcoming", "GET", Summary = "Gets a list of upcoming episodes")]
  72. public class GetUpcomingEpisodes : IReturn<QueryResult<BaseItemDto>>, IHasDtoOptions
  73. {
  74. /// <summary>
  75. /// Gets or sets the user id.
  76. /// </summary>
  77. /// <value>The user id.</value>
  78. [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
  79. public Guid UserId { get; set; }
  80. /// <summary>
  81. /// Skips over a given number of items within the results. Use for paging.
  82. /// </summary>
  83. /// <value>The start index.</value>
  84. [ApiMember(Name = "StartIndex", Description = "Optional. The record index to start at. All items with a lower index will be dropped from the results.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  85. public int? StartIndex { get; set; }
  86. /// <summary>
  87. /// The maximum number of items to return
  88. /// </summary>
  89. /// <value>The limit.</value>
  90. [ApiMember(Name = "Limit", Description = "Optional. The maximum number of records to return", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  91. public int? Limit { get; set; }
  92. /// <summary>
  93. /// Fields to return within the items, in addition to basic information
  94. /// </summary>
  95. /// <value>The fields.</value>
  96. [ApiMember(Name = "Fields", Description = "Optional. Specify additional fields of information to return in the output. This allows multiple, comma delimeted. Options: Budget, Chapters, DateCreated, Genres, HomePageUrl, IndexOptions, MediaStreams, Overview, ParentId, Path, People, ProviderIds, PrimaryImageAspectRatio, Revenue, SortName, Studios, Taglines, TrailerUrls", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]
  97. public string Fields { get; set; }
  98. /// <summary>
  99. /// Specify this to localize the search to a specific item or folder. Omit to use the root.
  100. /// </summary>
  101. /// <value>The parent id.</value>
  102. [ApiMember(Name = "ParentId", Description = "Specify this to localize the search to a specific item or folder. Omit to use the root", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  103. public string ParentId { get; set; }
  104. [ApiMember(Name = "EnableImages", Description = "Optional, include image information in output", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "GET")]
  105. public bool? EnableImages { get; set; }
  106. [ApiMember(Name = "ImageTypeLimit", Description = "Optional, the max number of images to return, per image type", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  107. public int? ImageTypeLimit { get; set; }
  108. [ApiMember(Name = "EnableImageTypes", Description = "Optional. The image types to include in the output.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  109. public string EnableImageTypes { get; set; }
  110. [ApiMember(Name = "EnableUserData", Description = "Optional, include user data", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "GET")]
  111. public bool? EnableUserData { get; set; }
  112. }
  113. [Route("/Shows/{Id}/Episodes", "GET", Summary = "Gets episodes for a tv season")]
  114. public class GetEpisodes : IReturn<QueryResult<BaseItemDto>>, IHasItemFields, IHasDtoOptions
  115. {
  116. /// <summary>
  117. /// Gets or sets the user id.
  118. /// </summary>
  119. /// <value>The user id.</value>
  120. [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
  121. public Guid UserId { get; set; }
  122. /// <summary>
  123. /// Fields to return within the items, in addition to basic information
  124. /// </summary>
  125. /// <value>The fields.</value>
  126. [ApiMember(Name = "Fields", Description = "Optional. Specify additional fields of information to return in the output. This allows multiple, comma delimeted. Options: Budget, Chapters, DateCreated, Genres, HomePageUrl, IndexOptions, MediaStreams, Overview, ParentId, Path, People, ProviderIds, PrimaryImageAspectRatio, Revenue, SortName, Studios, Taglines, TrailerUrls", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]
  127. public string Fields { get; set; }
  128. [ApiMember(Name = "Id", Description = "The series id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
  129. public string Id { get; set; }
  130. [ApiMember(Name = "Season", Description = "Optional filter by season number.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  131. public int? Season { get; set; }
  132. [ApiMember(Name = "SeasonId", Description = "Optional. Filter by season id", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  133. public string SeasonId { get; set; }
  134. [ApiMember(Name = "IsMissing", Description = "Optional filter by items that are missing episodes or not.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
  135. public bool? IsMissing { get; set; }
  136. [ApiMember(Name = "AdjacentTo", Description = "Optional. Return items that are siblings of a supplied item.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  137. public string AdjacentTo { get; set; }
  138. [ApiMember(Name = "StartItemId", Description = "Optional. Skip through the list until a given item is found.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  139. public string StartItemId { get; set; }
  140. /// <summary>
  141. /// Skips over a given number of items within the results. Use for paging.
  142. /// </summary>
  143. /// <value>The start index.</value>
  144. [ApiMember(Name = "StartIndex", Description = "Optional. The record index to start at. All items with a lower index will be dropped from the results.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  145. public int? StartIndex { get; set; }
  146. /// <summary>
  147. /// The maximum number of items to return
  148. /// </summary>
  149. /// <value>The limit.</value>
  150. [ApiMember(Name = "Limit", Description = "Optional. The maximum number of records to return", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  151. public int? Limit { get; set; }
  152. [ApiMember(Name = "EnableImages", Description = "Optional, include image information in output", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "GET")]
  153. public bool? EnableImages { get; set; }
  154. [ApiMember(Name = "ImageTypeLimit", Description = "Optional, the max number of images to return, per image type", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  155. public int? ImageTypeLimit { get; set; }
  156. [ApiMember(Name = "EnableImageTypes", Description = "Optional. The image types to include in the output.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  157. public string EnableImageTypes { get; set; }
  158. [ApiMember(Name = "EnableUserData", Description = "Optional, include user data", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "GET")]
  159. public bool? EnableUserData { get; set; }
  160. [ApiMember(Name = "SortBy", Description = "Optional. Specify one or more sort orders, comma delimeted. Options: Album, AlbumArtist, Artist, Budget, CommunityRating, CriticRating, DateCreated, DatePlayed, PlayCount, PremiereDate, ProductionYear, SortName, Random, Revenue, Runtime", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]
  161. public string SortBy { get; set; }
  162. [ApiMember(Name = "SortOrder", Description = "Sort Order - Ascending,Descending", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  163. public SortOrder? SortOrder { get; set; }
  164. }
  165. [Route("/Shows/{Id}/Seasons", "GET", Summary = "Gets seasons for a tv series")]
  166. public class GetSeasons : IReturn<QueryResult<BaseItemDto>>, IHasItemFields, IHasDtoOptions
  167. {
  168. /// <summary>
  169. /// Gets or sets the user id.
  170. /// </summary>
  171. /// <value>The user id.</value>
  172. [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
  173. public Guid UserId { get; set; }
  174. /// <summary>
  175. /// Fields to return within the items, in addition to basic information
  176. /// </summary>
  177. /// <value>The fields.</value>
  178. [ApiMember(Name = "Fields", Description = "Optional. Specify additional fields of information to return in the output. This allows multiple, comma delimeted. Options: Budget, Chapters, DateCreated, Genres, HomePageUrl, IndexOptions, MediaStreams, Overview, ParentId, Path, People, ProviderIds, PrimaryImageAspectRatio, Revenue, SortName, Studios, Taglines, TrailerUrls", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]
  179. public string Fields { get; set; }
  180. [ApiMember(Name = "Id", Description = "The series id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
  181. public string Id { get; set; }
  182. [ApiMember(Name = "IsSpecialSeason", Description = "Optional. Filter by special season.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
  183. public bool? IsSpecialSeason { get; set; }
  184. [ApiMember(Name = "IsMissing", Description = "Optional filter by items that are missing episodes or not.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
  185. public bool? IsMissing { get; set; }
  186. [ApiMember(Name = "AdjacentTo", Description = "Optional. Return items that are siblings of a supplied item.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  187. public string AdjacentTo { get; set; }
  188. [ApiMember(Name = "EnableImages", Description = "Optional, include image information in output", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "GET")]
  189. public bool? EnableImages { get; set; }
  190. [ApiMember(Name = "ImageTypeLimit", Description = "Optional, the max number of images to return, per image type", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  191. public int? ImageTypeLimit { get; set; }
  192. [ApiMember(Name = "EnableImageTypes", Description = "Optional. The image types to include in the output.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  193. public string EnableImageTypes { get; set; }
  194. [ApiMember(Name = "EnableUserData", Description = "Optional, include user data", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "GET")]
  195. public bool? EnableUserData { get; set; }
  196. }
  197. /// <summary>
  198. /// Class TvShowsService
  199. /// </summary>
  200. [Authenticated]
  201. public class TvShowsService : BaseApiService
  202. {
  203. /// <summary>
  204. /// The _user manager
  205. /// </summary>
  206. private readonly IUserManager _userManager;
  207. /// <summary>
  208. /// The _user data repository
  209. /// </summary>
  210. private readonly IUserDataManager _userDataManager;
  211. /// <summary>
  212. /// The _library manager
  213. /// </summary>
  214. private readonly ILibraryManager _libraryManager;
  215. private readonly IItemRepository _itemRepo;
  216. private readonly IDtoService _dtoService;
  217. private readonly ITVSeriesManager _tvSeriesManager;
  218. private readonly IAuthorizationContext _authContext;
  219. /// <summary>
  220. /// Initializes a new instance of the <see cref="TvShowsService" /> class.
  221. /// </summary>
  222. /// <param name="userManager">The user manager.</param>
  223. /// <param name="userDataManager">The user data repository.</param>
  224. /// <param name="libraryManager">The library manager.</param>
  225. public TvShowsService(IUserManager userManager, IUserDataManager userDataManager, ILibraryManager libraryManager, IItemRepository itemRepo, IDtoService dtoService, ITVSeriesManager tvSeriesManager, IAuthorizationContext authContext)
  226. {
  227. _userManager = userManager;
  228. _userDataManager = userDataManager;
  229. _libraryManager = libraryManager;
  230. _itemRepo = itemRepo;
  231. _dtoService = dtoService;
  232. _tvSeriesManager = tvSeriesManager;
  233. _authContext = authContext;
  234. }
  235. public object Get(GetUpcomingEpisodes request)
  236. {
  237. var user = _userManager.GetUserById(request.UserId);
  238. var minPremiereDate = DateTime.Now.Date.ToUniversalTime().AddDays(-1);
  239. var parentIdGuid = string.IsNullOrWhiteSpace(request.ParentId) ? Guid.Empty : new Guid(request.ParentId);
  240. var options = GetDtoOptions(_authContext, request);
  241. var itemsResult = _libraryManager.GetItemList(new InternalItemsQuery(user)
  242. {
  243. IncludeItemTypes = new[] { typeof(Episode).Name },
  244. OrderBy = new[] { ItemSortBy.PremiereDate, ItemSortBy.SortName }.Select(i => new ValueTuple<string, SortOrder>(i, SortOrder.Ascending)).ToArray(),
  245. MinPremiereDate = minPremiereDate,
  246. StartIndex = request.StartIndex,
  247. Limit = request.Limit,
  248. ParentId = parentIdGuid,
  249. Recursive = true,
  250. DtoOptions = options
  251. });
  252. var returnItems = _dtoService.GetBaseItemDtos(itemsResult, options, user);
  253. var result = new QueryResult<BaseItemDto>
  254. {
  255. TotalRecordCount = itemsResult.Count,
  256. Items = returnItems
  257. };
  258. return ToOptimizedResult(result);
  259. }
  260. /// <summary>
  261. /// Gets the specified request.
  262. /// </summary>
  263. /// <param name="request">The request.</param>
  264. /// <returns>System.Object.</returns>
  265. public object Get(GetNextUpEpisodes request)
  266. {
  267. var options = GetDtoOptions(_authContext, request);
  268. var result = _tvSeriesManager.GetNextUp(new NextUpQuery
  269. {
  270. Limit = request.Limit,
  271. ParentId = request.ParentId,
  272. SeriesId = request.SeriesId,
  273. StartIndex = request.StartIndex,
  274. UserId = request.UserId,
  275. EnableTotalRecordCount = request.EnableTotalRecordCount
  276. }, options);
  277. var user = _userManager.GetUserById(request.UserId);
  278. var returnItems = _dtoService.GetBaseItemDtos(result.Items, options, user);
  279. return ToOptimizedResult(new QueryResult<BaseItemDto>
  280. {
  281. TotalRecordCount = result.TotalRecordCount,
  282. Items = returnItems
  283. });
  284. }
  285. /// <summary>
  286. /// Applies the paging.
  287. /// </summary>
  288. /// <param name="items">The items.</param>
  289. /// <param name="startIndex">The start index.</param>
  290. /// <param name="limit">The limit.</param>
  291. /// <returns>IEnumerable{BaseItem}.</returns>
  292. private IEnumerable<BaseItem> ApplyPaging(IEnumerable<BaseItem> items, int? startIndex, int? limit)
  293. {
  294. // Start at
  295. if (startIndex.HasValue)
  296. {
  297. items = items.Skip(startIndex.Value);
  298. }
  299. // Return limit
  300. if (limit.HasValue)
  301. {
  302. items = items.Take(limit.Value);
  303. }
  304. return items;
  305. }
  306. public object Get(GetSeasons request)
  307. {
  308. var user = _userManager.GetUserById(request.UserId);
  309. var series = GetSeries(request.Id, user);
  310. if (series == null)
  311. {
  312. throw new ResourceNotFoundException("Series not found");
  313. }
  314. var seasons = series.GetItemList(new InternalItemsQuery(user)
  315. {
  316. IsMissing = request.IsMissing,
  317. IsSpecialSeason = request.IsSpecialSeason,
  318. AdjacentTo = request.AdjacentTo
  319. });
  320. var dtoOptions = GetDtoOptions(_authContext, request);
  321. var returnItems = _dtoService.GetBaseItemDtos(seasons, dtoOptions, user);
  322. return new QueryResult<BaseItemDto>
  323. {
  324. TotalRecordCount = returnItems.Count,
  325. Items = returnItems
  326. };
  327. }
  328. private Series GetSeries(string seriesId, User user)
  329. {
  330. if (!string.IsNullOrWhiteSpace(seriesId))
  331. {
  332. return _libraryManager.GetItemById(seriesId) as Series;
  333. }
  334. return null;
  335. }
  336. public object Get(GetEpisodes request)
  337. {
  338. var user = _userManager.GetUserById(request.UserId);
  339. List<BaseItem> episodes;
  340. var dtoOptions = GetDtoOptions(_authContext, request);
  341. if (!string.IsNullOrWhiteSpace(request.SeasonId))
  342. {
  343. var season = _libraryManager.GetItemById(new Guid(request.SeasonId)) as Season;
  344. if (season == null)
  345. {
  346. throw new ResourceNotFoundException("No season exists with Id " + request.SeasonId);
  347. }
  348. episodes = season.GetEpisodes(user, dtoOptions);
  349. }
  350. else if (request.Season.HasValue)
  351. {
  352. var series = GetSeries(request.Id, user);
  353. if (series == null)
  354. {
  355. throw new ResourceNotFoundException("Series not found");
  356. }
  357. var season = series.GetSeasons(user, dtoOptions).FirstOrDefault(i => i.IndexNumber == request.Season.Value);
  358. if (season == null)
  359. {
  360. episodes = new List<BaseItem>();
  361. }
  362. else
  363. {
  364. episodes = ((Season)season).GetEpisodes(user, dtoOptions);
  365. }
  366. }
  367. else
  368. {
  369. var series = GetSeries(request.Id, user);
  370. if (series == null)
  371. {
  372. throw new ResourceNotFoundException("Series not found");
  373. }
  374. episodes = series.GetEpisodes(user, dtoOptions).ToList();
  375. }
  376. // Filter after the fact in case the ui doesn't want them
  377. if (request.IsMissing.HasValue)
  378. {
  379. var val = request.IsMissing.Value;
  380. episodes = episodes.Where(i => ((Episode)i).IsMissingEpisode == val).ToList();
  381. }
  382. if (!string.IsNullOrWhiteSpace(request.StartItemId))
  383. {
  384. episodes = episodes.SkipWhile(i => !string.Equals(i.Id.ToString("N", CultureInfo.InvariantCulture), request.StartItemId, StringComparison.OrdinalIgnoreCase)).ToList();
  385. }
  386. // This must be the last filter
  387. if (!string.IsNullOrEmpty(request.AdjacentTo))
  388. {
  389. episodes = UserViewBuilder.FilterForAdjacency(episodes, request.AdjacentTo).ToList();
  390. }
  391. if (string.Equals(request.SortBy, ItemSortBy.Random, StringComparison.OrdinalIgnoreCase))
  392. {
  393. episodes = episodes.OrderBy(i => Guid.NewGuid()).ToList();
  394. }
  395. var returnItems = episodes;
  396. if (request.StartIndex.HasValue || request.Limit.HasValue)
  397. {
  398. returnItems = ApplyPaging(episodes, request.StartIndex, request.Limit).ToList();
  399. }
  400. var dtos = _dtoService.GetBaseItemDtos(returnItems, dtoOptions, user);
  401. return new QueryResult<BaseItemDto>
  402. {
  403. TotalRecordCount = episodes.Count,
  404. Items = dtos
  405. };
  406. }
  407. }
  408. }