Exemplo n.º 1
0
        /// <summary>
        /// Gets the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>Task{ItemsResult}.</returns>
        protected ItemsResult GetResult(GetItemsByName request)
        {
            User     user = null;
            BaseItem item;

            if (request.UserId.HasValue)
            {
                user = UserManager.GetUserById(request.UserId.Value);
                item = string.IsNullOrEmpty(request.ParentId) ? user.RootFolder : DtoService.GetItemByDtoId(request.ParentId, user.Id);
            }
            else
            {
                item = string.IsNullOrEmpty(request.ParentId) ? LibraryManager.RootFolder : DtoService.GetItemByDtoId(request.ParentId);
            }

            IEnumerable <BaseItem> items;

            if (item.IsFolder)
            {
                var folder = (Folder)item;

                if (request.UserId.HasValue)
                {
                    items = request.Recursive ? folder.GetRecursiveChildren(user) : folder.GetChildren(user, true);
                }
                else
                {
                    items = request.Recursive ? folder.GetRecursiveChildren() : folder.Children;
                }
            }
            else
            {
                items = new[] { item };
            }

            items = FilterItems(request, items);

            var extractedItems = GetAllItems(request, items);

            var filteredItems = FilterItems(request, extractedItems, user);

            filteredItems = FilterByLibraryItems(request, filteredItems, user);

            filteredItems = ItemsService.ApplySortOrder(request, filteredItems, user, LibraryManager).Cast <TItemType>();

            var ibnItemsArray = filteredItems.ToList();

            IEnumerable <TItemType> ibnItems = ibnItemsArray;

            var result = new ItemsResult
            {
                TotalRecordCount = ibnItemsArray.Count
            };

            if (request.StartIndex.HasValue || request.Limit.HasValue)
            {
                if (request.StartIndex.HasValue)
                {
                    ibnItems = ibnItems.Skip(request.StartIndex.Value);
                }

                if (request.Limit.HasValue)
                {
                    ibnItems = ibnItems.Take(request.Limit.Value);
                }
            }

            var fields = request.GetItemFields().ToList();

            var dtos = ibnItems.Select(i => GetDto(i, user, fields));

            result.Items = dtos.Where(i => i != null).ToArray();

            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>Task{ItemsResult}.</returns>
        protected async Task <ItemsResult> GetResult(GetItemsByName request)
        {
            User     user = null;
            BaseItem item;

            if (request.UserId.HasValue)
            {
                user = UserManager.GetUserById(request.UserId.Value);
                item = string.IsNullOrEmpty(request.ParentId) ? user.RootFolder : DtoBuilder.GetItemByClientId(request.ParentId, UserManager, LibraryManager, user.Id);
            }
            else
            {
                item = string.IsNullOrEmpty(request.ParentId) ? LibraryManager.RootFolder : DtoBuilder.GetItemByClientId(request.ParentId, UserManager, LibraryManager);
            }

            IEnumerable <BaseItem> items;

            if (item.IsFolder)
            {
                var folder = (Folder)item;

                if (request.UserId.HasValue)
                {
                    items = request.Recursive ? folder.GetRecursiveChildren(user) : folder.GetChildren(user, true);
                }
                else
                {
                    items = request.Recursive ? folder.RecursiveChildren: folder.Children;
                }
            }
            else
            {
                items = new[] { item };
            }

            items = FilterItems(request, items);

            var extractedItems = GetAllItems(request, items);

            extractedItems = FilterItems(request, extractedItems, user);
            extractedItems = SortItems(request, extractedItems);

            var ibnItemsArray = extractedItems.ToArray();

            IEnumerable <IbnStub <TItemType> > ibnItems = ibnItemsArray;

            var result = new ItemsResult
            {
                TotalRecordCount = ibnItemsArray.Length
            };

            if (request.StartIndex.HasValue || request.Limit.HasValue)
            {
                if (request.StartIndex.HasValue)
                {
                    ibnItems = ibnItems.Skip(request.StartIndex.Value);
                }

                if (request.Limit.HasValue)
                {
                    ibnItems = ibnItems.Take(request.Limit.Value);
                }
            }

            var fields = request.GetItemFields().ToList();

            var tasks = ibnItems.Select(i => GetDto(i, user, fields));

            var resultItems = await Task.WhenAll(tasks).ConfigureAwait(false);

            result.Items = resultItems.Where(i => i != null).ToArray();

            return(result);
        }