Example #1
0
        public async Task <SortPageResult <Product> > GetSortFilterPageAsync(ItemTypeSelector types, int currPage,
                                                                             int countPerPage, string searchString = null, string sortPropName = null, int[] categoryIds = null,
                                                                             int[] descGroupIds = null)
        {
            ThrowIfDisposed();
            if (countPerPage < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(countPerPage));
            }
            if (currPage < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(currPage));
            }
            if (!Enum.IsDefined(typeof(ItemTypeSelector), types))
            {
                throw new InvalidEnumArgumentException(nameof(ItemTypeSelector), (int)types, typeof(ItemTypeSelector));
            }

            var prodQuery = await FilterProductsQuery(_repository.ProductsQueryable, types, categoryIds, descGroupIds);

            SearchProductsQuery(searchString, ref prodQuery);
            OrderProductsQuery(sortPropName, ref prodQuery);
            var totalProductsN = await prodQuery.CountAsync(CancellationToken);

            PaginateProductsQuery(currPage, countPerPage, ref prodQuery);
            var products = await prodQuery
                           .Include(x => x.Images)
                           .ToListAsync(CancellationToken);

            return(new SortPageResult <Product> {
                FilteredData = products, TotalN = totalProductsN
            });
        }
Example #2
0
        private void OnItemClick()
        {
            if (ItemTypeSelector.Invoke(Source) == TreeItemType.Folder)
            {
                IsExpanded = !IsExpanded;
            }

            ItemSelected.InvokeAsync(Source);
            ParentTree.SelectedNode = this;
        }
Example #3
0
        public async Task <IActionResult> Items(
            [FromQuery(Name = "s")] string search,
            [FromQuery(Name = "st")] ItemTypeSelector types,
            [FromQuery(Name = "o")] string sortOrder,
            [FromQuery(Name = "p")] int?page,
            [FromQuery(Name = "c")] int?pageCount,
            [FromQuery(Name = "cat")] int[] categoryIds,
            [FromQuery(Name = "desc")] int[] descGroupIds)
        {
            if (sortOrder.IsNullOrEmpty())
            {
                sortOrder = nameof(ProductDto.Name);
            }

            var currPage     = page ?? 1;
            var countPerPage = pageCount == null || pageCount <= 0 ? 15 : pageCount.Value;

            SortPageResult <Product> result =
                await _shopManager.GetSortFilterPageAsync(types, currPage, countPerPage, search, sortOrder,
                                                          categoryIds, descGroupIds);

            var allCategories =
                _mapper.Map <IEnumerable <CategoryDto> >(await _shopManager.GetAllCategoriesAsync());

            ViewBag.itemCount = result.TotalN;

            var model = new ItemsViewModel()
            {
                CurrentSearch    = search,
                CurrentSortOrder = sortOrder,
                Descending       = sortOrder.EndsWith("_desc"),
                CurrentPage      = currPage,
                CountPerPage     = countPerPage,
                Types            = (int)types,
                ItemCount        = result.TotalN,
                CategoryIds      = categoryIds,
                DescGroupIds     = descGroupIds,
                Items            = _mapper.Map <IEnumerable <ProductDto> >(result.FilteredData),
                Categories       = allCategories
            };

            return(View(model));
        }
Example #4
0
        private async Task <IQueryable <Product> > FilterProductsQuery(IQueryable <Product> productQuery,
                                                                       ItemTypeSelector types, int[] categoryIds, int[] descGroupIds)
        {
            //filter cats
            if (!categoryIds.IsNullOrEmpty())
            {
                productQuery = productQuery
                               .Where(x => x.ProductToCategory
                                      .Any(z => categoryIds.Contains(z.CategoryId)));
            }

            if (!descGroupIds.IsNullOrEmpty())
            {
                //filter desc groups
                var productIdsInDescGroups = await _repository.DescriptionGroupsQueryable
                                             .Where(x => descGroupIds.Contains(x.Id))
                                             .SelectMany(x => x.DescriptionGroupItems)
                                             .SelectMany(x => x.Descriptions)
                                             .Select(x => x.ProductId)
                                             .Distinct()
                                             .ToArrayAsync(CancellationToken);

                productQuery = productQuery
                               .Where(x => productIdsInDescGroups.Contains(x.Id));
            }

            //filter types
            switch (types)
            {
            case ItemTypeSelector.Enabled:
                productQuery = productQuery.Where(x => x.Available);
                break;

            case ItemTypeSelector.Disabled:
                productQuery = productQuery.Where(x => !x.Available);
                break;

            default:
                break;
            }

            return(productQuery);
        }