コード例 #1
0
        public IQueryable <Product> OrderProducts(IQueryable <Product> products, ProductOrderingOption productOrderingOption)
        {
            var productOrderingOptionName        = productOrderingOption.ProductOrderingOptionName;
            IQueryable <Product> orderedProducts = products;

            if (productOrderingOptionName == GlobalConstants.OrderProductByPrice)
            {
                orderedProducts = products
                                  .OrderBy(x => x.Price);
            }
            else if (productOrderingOptionName == GlobalConstants.OrderProductByRating)
            {
                orderedProducts = products
                                  .OrderBy(x => x.ProductRatings.Average(x => x.Rating));
            }
            else if (productOrderingOptionName == GlobalConstants.OrderProductBySales)
            {
                orderedProducts = products
                                  .OrderBy(x => x.ProductSales.Count);
            }
            else
            {
                throw new Exception("Something went wrong");
            }

            if (productOrderingOption.IsDescendning)
            {
                orderedProducts = orderedProducts.Reverse();
            }

            return(orderedProducts);
        }
        public IActionResult All(int productsPage, List <ProductFilterOptionsViewModel> productFilterOptions, ProductOrderingOption productOrderingOption, string searchString)
        {
            //Set the default filter options if they are null or empty
            if (productFilterOptions == null || productFilterOptions.Count == 0)
            {
                var categories = this.categoryService.GetAllCategories();
                var categoriesProductFilterOptions = categories
                                                     .Select(x => new ProductFilterOptionsViewModel {
                    FilterName = $"{GlobalConstants.IncludeCategorySplitter}{x.Name}", FilterValue = true
                });

                productFilterOptions.AddRange(categoriesProductFilterOptions);
            }

            //Filter the products by categories
            var productsFiltered = this.productService.FilterProducts(productFilterOptions);

            //Filter the products by search string
            if (string.IsNullOrWhiteSpace(searchString) == false)
            {
                productsFiltered = this.productService.FilterProducts(productsFiltered, searchString.Trim());
            }

            //Order the products by ordering options
            if (string.IsNullOrWhiteSpace(productOrderingOption.ProductOrderingOptionName) == false)
            {
                productsFiltered = this.productService.OrderProducts(productsFiltered, productOrderingOption);
            }

            //Get the count of the filtered products and the pages for these products
            var productsCount = productsFiltered.Count();
            var pagesCount    = (productsCount % GlobalConstants.ProductsPerPage == 0)
                ? (productsCount / GlobalConstants.ProductsPerPage)
                : (productsCount / GlobalConstants.ProductsPerPage) + 1;

            //Validate current page
            if (productsPage <= 0 || productsPage > pagesCount)
            {
                productsPage = 1;
            }

            //Get the products for the current page and with the applied filters
            var productsForCurrentPage = this.productService.GetProductsForPage(productsFiltered, productsPage);

            //Currently for debug purposes. This removes unnecessary temp data
            foreach (var key in TempData.Keys)
            {
                if (key == GlobalConstants.InputModelFromPOSTRequest || key == GlobalConstants.InputModelFromPOSTRequestType)
                {
                    TempData.Remove(key);
                }
            }

            //Create pagination model
            var paginationViewModel = new PaginationViewModel
            {
                CurrentPage   = productsPage,
                CutoffNumber  = GlobalConstants.ProductsPagesCutoffNumber,
                NumberOfPages = pagesCount
            };

            //Create the view model
            var allProductViewModel = new AllProductsViewModel
            {
                ProductViewModels     = productsForCurrentPage,
                PaginationViewModel   = paginationViewModel,
                ProductFilterOptions  = productFilterOptions,
                ProductOrderingOption = new ProductOrderingOption {
                    ProductOrderingOptionName = productOrderingOption.ProductOrderingOptionName, IsDescendning = productOrderingOption.IsDescendning
                }
            };

            return(this.View(allProductViewModel));
        }