/// <summary> /// Gets the items. /// </summary> /// <param name="request">The request.</param> /// <returns>Task{ItemsResult}.</returns> private async Task <ItemsResult> GetItems(GetItems request) { var user = _userManager.GetUserById(request.UserId); var items = GetItemsToSerialize(request, user); // Apply filters // Run them starting with the ones that are likely to reduce the list the most foreach (var filter in GetFilters(request).OrderByDescending(f => (int)f)) { items = ApplyFilter(items, filter, user); } items = ApplyAdditionalFilters(request, items); items = ApplySearchTerm(request, items); items = ApplySortOrder(request, items, user); var itemsArray = items.ToArray(); var pagedItems = ApplyPaging(request, itemsArray); var fields = GetItemFields(request).ToList(); var dtoBuilder = new DtoBuilder(Logger, _libraryManager); var returnItems = await Task.WhenAll(pagedItems.Select(i => dtoBuilder.GetBaseItemDto(i, user, fields))).ConfigureAwait(false); return(new ItemsResult { TotalRecordCount = itemsArray.Length, Items = returnItems }); }
/// <summary> /// Gets the similar items. /// </summary> /// <param name="userManager">The user manager.</param> /// <param name="itemRepository">The item repository.</param> /// <param name="libraryManager">The library manager.</param> /// <param name="userDataRepository">The user data repository.</param> /// <param name="logger">The logger.</param> /// <param name="request">The request.</param> /// <param name="includeInSearch">The include in search.</param> /// <param name="getSimilarityScore">The get similarity score.</param> /// <returns>ItemsResult.</returns> internal static ItemsResult GetSimilarItems(IUserManager userManager, IItemRepository itemRepository, ILibraryManager libraryManager, IUserDataRepository userDataRepository, ILogger logger, BaseGetSimilarItems request, Func <BaseItem, bool> includeInSearch, Func <BaseItem, BaseItem, int> getSimilarityScore) { var user = request.UserId.HasValue ? userManager.GetUserById(request.UserId.Value) : null; var item = string.IsNullOrEmpty(request.Id) ? (request.UserId.HasValue ? user.RootFolder : (Folder)libraryManager.RootFolder) : DtoBuilder.GetItemByClientId(request.Id, userManager, libraryManager, request.UserId); var fields = request.GetItemFields().ToList(); var dtoBuilder = new DtoBuilder(logger, libraryManager, userDataRepository, itemRepository); var inputItems = user == null ? libraryManager.RootFolder.RecursiveChildren : user.RootFolder.GetRecursiveChildren(user); var items = GetSimilaritems(item, inputItems, includeInSearch, getSimilarityScore).ToArray(); var result = new ItemsResult { Items = items.Take(request.Limit ?? items.Length).Select(i => dtoBuilder.GetBaseItemDto(i, fields, user)).Select(t => t.Result).ToArray(), TotalRecordCount = items.Length }; return(result); }
private async Task <ThemeMediaResult> GetThemeSongs(GetThemeSongs request) { var user = request.UserId.HasValue ? _userManager.GetUserById(request.UserId.Value) : null; var item = string.IsNullOrEmpty(request.Id) ? (request.UserId.HasValue ? user.RootFolder : (Folder)_libraryManager.RootFolder) : DtoBuilder.GetItemByClientId(request.Id, _userManager, _libraryManager, request.UserId); while (item.ThemeSongIds.Count == 0 && request.InheritFromParent && item.Parent != null) { item = item.Parent; } // Get everything var fields = Enum.GetNames(typeof(ItemFields)) .Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true)) .ToList(); var dtoBuilder = new DtoBuilder(Logger, _libraryManager, _userDataRepository, _itemRepo); var tasks = item.ThemeSongIds.Select(_itemRepo.RetrieveItem) .OrderBy(i => i.SortName) .Select(i => dtoBuilder.GetBaseItemDto(i, fields, user, item)); var items = await Task.WhenAll(tasks).ConfigureAwait(false); return(new ThemeMediaResult { Items = items, TotalRecordCount = items.Length, OwnerId = DtoBuilder.GetClientItemId(item) }); }
/// <summary> /// Gets the specified request. /// </summary> /// <param name="request">The request.</param> /// <returns>System.Object.</returns> public object Get(GetAdditionalParts request) { var user = request.UserId.HasValue ? _userManager.GetUserById(request.UserId.Value) : null; var item = string.IsNullOrEmpty(request.Id) ? (request.UserId.HasValue ? user.RootFolder : (Folder)_libraryManager.RootFolder) : DtoBuilder.GetItemByClientId(request.Id, _userManager, _libraryManager, request.UserId); // Get everything var fields = Enum.GetNames(typeof(ItemFields)) .Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true)) .ToList(); var dtoBuilder = new DtoBuilder(Logger, _libraryManager, _userDataRepository, _itemRepo); var video = (Video)item; var items = video.AdditionalPartIds.Select(_itemRepo.RetrieveItem) .OrderBy(i => i.SortName) .Select(i => dtoBuilder.GetBaseItemDto(i, fields, user, video)) .Select(t => t.Result) .ToArray(); var result = new ItemsResult { Items = items, TotalRecordCount = items.Length }; return(ToOptimizedResult(result)); }
/// <summary> /// Gets the item. /// </summary> /// <param name="request">The request.</param> /// <returns>Task{BaseItemDto}.</returns> private async Task <BaseItemDto> GetItem(GetGenre request) { var item = await GetGenre(request.Name, LibraryManager).ConfigureAwait(false); // Get everything var fields = Enum.GetNames(typeof(ItemFields)).Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true)); var builder = new DtoBuilder(Logger, LibraryManager, UserDataRepository, ItemRepository); if (request.UserId.HasValue) { var user = UserManager.GetUserById(request.UserId.Value); return(await builder.GetBaseItemDto(item, fields.ToList(), user).ConfigureAwait(false)); } return(await builder.GetBaseItemDto(item, fields.ToList()).ConfigureAwait(false)); }
/// <summary> /// Gets the specified request. /// </summary> /// <param name="request">The request.</param> /// <returns>System.Object.</returns> public object Get(GetRootFolder request) { var user = _userManager.GetUserById(request.UserId); var item = user.RootFolder; // Get everything var fields = Enum.GetNames(typeof(ItemFields)).Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true)).ToList(); var dtoBuilder = new DtoBuilder(Logger, _libraryManager, _userDataRepository, _itemRepo); var result = dtoBuilder.GetBaseItemDto(item, fields, user).Result; return(ToOptimizedResult(result)); }
/// <summary> /// Gets the specified request. /// </summary> /// <param name="request">The request.</param> /// <returns>System.Object.</returns> public object Get(GetItem request) { var user = _userManager.GetUserById(request.UserId); var item = DtoBuilder.GetItemByClientId(request.Id, _userManager, _libraryManager, user.Id); // Get everything var fields = Enum.GetNames(typeof(ItemFields)).Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true)).ToList(); var dtoBuilder = new DtoBuilder(Logger, _libraryManager); var result = dtoBuilder.GetBaseItemDto(item, user, fields).Result; return(ToOptimizedResult(result)); }
/// <summary> /// Gets the specified request. /// </summary> /// <param name="request">The request.</param> /// <returns>System.Object.</returns> public object Get(GetLocalTrailers request) { var user = _userManager.GetUserById(request.UserId); var item = DtoBuilder.GetItemByClientId(request.Id, _userManager, _libraryManager, user.Id); // Get everything var fields = Enum.GetNames(typeof(ItemFields)).Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true)).ToList(); var dtoBuilder = new DtoBuilder(Logger, _libraryManager); var items = item.LocalTrailers.Select(i => dtoBuilder.GetBaseItemDto(i, user, fields)).AsParallel().Select(t => t.Result).ToList(); return(ToOptimizedResult(items)); }
private Task <BaseItemDto[]> GetAsync(GetLocalTrailers request) { var user = _userManager.GetUserById(request.UserId); var item = string.IsNullOrEmpty(request.Id) ? user.RootFolder : DtoBuilder.GetItemByClientId(request.Id, _userManager, _libraryManager, user.Id); // Get everything var fields = Enum.GetNames(typeof(ItemFields)).Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true)).ToList(); var dtoBuilder = new DtoBuilder(Logger, _libraryManager, _userDataRepository, _itemRepo); var tasks = item.LocalTrailerIds .Select(_itemRepo.RetrieveItem) .OrderBy(i => i.SortName) .Select(i => dtoBuilder.GetBaseItemDto(i, fields, user, item)); return(Task.WhenAll(tasks)); }
/// <summary> /// Gets the specified request. /// </summary> /// <param name="request">The request.</param> /// <returns>System.Object.</returns> public object Get(GetLocalTrailers request) { var user = _userManager.GetUserById(request.UserId); var item = string.IsNullOrEmpty(request.Id) ? user.RootFolder : DtoBuilder.GetItemByClientId(request.Id, _userManager, _libraryManager, user.Id); // Get everything var fields = Enum.GetNames(typeof(ItemFields)).Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true)).ToList(); var dtoBuilder = new DtoBuilder(Logger, _libraryManager, _userDataRepository, _itemRepo); var items = item.LocalTrailerIds .Select(_itemRepo.RetrieveItem) .OrderBy(i => i.SortName) .Select(i => dtoBuilder.GetBaseItemDto(i, fields, user, item)) .Select(t => t.Result) .ToList(); return(ToOptimizedResult(items)); }
private async Task <ItemsResult> GetInstantMixResult(BaseGetSimilarItems request, IEnumerable <string> genres) { var user = request.UserId.HasValue ? _userManager.GetUserById(request.UserId.Value) : null; var fields = request.GetItemFields().ToList(); var dtoBuilder = new DtoBuilder(Logger, _libraryManager, _userDataRepository, _itemRepo); var inputItems = user == null ? _libraryManager.RootFolder.RecursiveChildren : user.RootFolder.GetRecursiveChildren(user); var genresDictionary = genres.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase); var limit = request.Limit.HasValue ? request.Limit.Value * 2 : 100; var items = inputItems .OfType <Audio>() .Select(i => new Tuple <Audio, int>(i, i.Genres.Count(genresDictionary.ContainsKey))) .OrderByDescending(i => i.Item2) .ThenBy(i => Guid.NewGuid()) .Select(i => i.Item1) .Take(limit) .OrderBy(i => Guid.NewGuid()) .ToArray(); var result = new ItemsResult { TotalRecordCount = items.Length }; var tasks = items.Take(request.Limit ?? items.Length) .Select(i => dtoBuilder.GetBaseItemDto(i, fields, user)); result.Items = await Task.WhenAll(tasks).ConfigureAwait(false); return(result); }
/// <summary> /// Gets the ancestors. /// </summary> /// <param name="request">The request.</param> /// <returns>Task{BaseItemDto[]}.</returns> public async Task <BaseItemDto[]> GetAncestors(GetAncestors request) { var item = DtoBuilder.GetItemByClientId(request.Id, _userManager, _libraryManager); var tasks = new List <Task <BaseItemDto> >(); var user = request.UserId.HasValue ? _userManager.GetUserById(request.UserId.Value) : null; // Get everything var fields = Enum.GetNames(typeof(ItemFields)) .Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true)) .ToList(); var dtoBuilder = new DtoBuilder(Logger, _libraryManager, _userDataRepository, _itemRepo); BaseItem parent = item.Parent; while (parent != null) { if (user != null) { parent = TranslateParentItem(parent, user); } tasks.Add(dtoBuilder.GetBaseItemDto(parent, fields, user)); if (parent is UserRootFolder) { break; } parent = parent.Parent; } return(await Task.WhenAll(tasks).ConfigureAwait(false)); }
/// <summary> /// Gets the item dtos. /// </summary> /// <param name="pagedItems">The paged items.</param> /// <param name="user">The user.</param> /// <param name="fields">The fields.</param> /// <returns>Task.</returns> private Task <BaseItemDto[]> GetItemDtos(IEnumerable <BaseItem> pagedItems, User user, List <ItemFields> fields) { var dtoBuilder = new DtoBuilder(Logger, _libraryManager, _userDataRepository, _itemRepo); return(Task.WhenAll(pagedItems.Select(i => dtoBuilder.GetBaseItemDto(i, fields, user)))); }