Пример #1
0
        public IActionResult Index()
        {
            var categories = _categoryRepository.FindAll();
            var vm         = new CategoryOverviewModel()
            {
                Categories = Mapper.Map <IEnumerable <Category>, IList <CategoryIndexModel> >(categories),
            };

            return(View(vm));
        }
Пример #2
0
        public ActionResult CategoryWithProducts(string top, string second, string third,
                                                 CategoryProductPagingFilteringModel command)
        {
            var topCategory = _categoryService.GetActiveCategoryOverviewModel(top);

            if (topCategory == null)
            {
                return(InvokeHttp404());
            }
            var secondCategory = _categoryService.GetActiveCategoryOverviewModel(top, second);

            if (secondCategory == null)
            {
                return(InvokeHttp404());
            }
            var categoryIds = new List <int>(secondCategory.Children.Select(x => x.Id).ToArray());

            if (categoryIds.Count <= 0)
            {
                categoryIds = new List <int> {
                    -1
                }
            }
            ;                                                               // To indicate that no products should be loaded.

            CategoryOverviewModel thirdCategory = null;

            if (!string.IsNullOrEmpty(third))
            {
                thirdCategory = _categoryService.GetActiveCategoryOverviewModel(top, second, third);
                categoryIds.Clear();
                categoryIds.Add(thirdCategory.Id);
            }

            #region Price filter
            decimal price;
            decimal?priceMin = null;
            if (!string.IsNullOrEmpty(command.From) && decimal.TryParse(command.From, out price))
            {
                priceMin = price;
            }
            decimal?priceMax = null;
            if (!string.IsNullOrEmpty(command.To) && decimal.TryParse(command.To, out price))
            {
                priceMax = price;
            }
            #endregion

            #region Brand filter
            var brandList = new List <int>();
            if (!string.IsNullOrEmpty(command.Brands))
            {
                var brandStringArray = command.Brands.Split(',');
                if (brandStringArray.Length > 0)
                {
                    int[] brandArray = Array.ConvertAll(brandStringArray, int.Parse);
                    brandList = new List <int>(brandArray);
                }
            }
            #endregion

            #region Type filter
            var filterList = new List <int>();
            if (!string.IsNullOrEmpty(command.Filters))
            {
                var filterStringArray = command.Filters.Split(',');
                if (filterStringArray.Length > 0)
                {
                    int[] filterArray = Array.ConvertAll(filterStringArray, int.Parse);
                    filterList = new List <int>(filterArray);
                }
            }
            #endregion

            var result = _productService.GetPagedProductOverviewModel(
                pageIndex: command.PageNumber - 1,
                pageSize: command.PageSize,
                categoryIds: categoryIds,
                categoryFilterIds: filterList.Count > 0 ? filterList : null,
                brandIds: brandList.Count > 0 ? brandList : null,
                enabled: true,
                visibleIndividually: true,
                includeDiscontinuedButInStock: true,
                priceMin: priceMin,
                priceMax: priceMax,
                orderBy: command.OrderBy);

            var model = new CategoryModel
            {
                SelectedSecondCategoryName  = secondCategory.Name,
                SelectedSecondCategoryUrl   = secondCategory.UrlKey,
                SelectedThirdCategoryName   = thirdCategory != null ? thirdCategory.Name : null,
                SelectedThirdCategoryUrl    = thirdCategory != null ? thirdCategory.UrlKey : null,
                SelectedCategoryDescription = thirdCategory != null ? thirdCategory.Description : secondCategory.Description,
                Category = topCategory,
                Products = result.Items.PrepareProductBoxModels()
            };

            model.PagingFilteringContext.LoadPagedList(result);

            #region Price range
            var priceRange = _productService.GetPriceRangeByCategory(categoryIds);
            model.PagingFilteringContext.PriceRangeFilter = new PriceRangeFilterModel
            {
                Min  = Math.Round(priceRange[0]).ToString(),
                Max  = Math.Ceiling(priceRange[1]).ToString(),
                From = (command.From == string.Empty) ? Math.Round(priceRange[0]).ToString() : command.From,
                To   = (command.To == string.Empty) ? Math.Ceiling(priceRange[1]).ToString() : command.To
            };
            #endregion

            #region Brand range
            var brandRange = _brandService.GetBrandRangeByCategory(categoryIds);
            model.PagingFilteringContext.BrandRangeFilter.Brands         = brandRange;
            model.PagingFilteringContext.BrandRangeFilter.SelectedBrands = brandList.ToArray();
            #endregion

            #region Type range
            var categoryFilterRange = _categoryService.GetCategoryFiltersRangeByCategory(categoryIds);
            model.PagingFilteringContext.CategoryFilterRangeFilter.Filters         = categoryFilterRange;
            model.PagingFilteringContext.CategoryFilterRangeFilter.SelectedFilters = filterList.ToArray();
            #endregion

            model.PagingFilteringContext.Brands           = command.Brands;
            model.PagingFilteringContext.SelectedPageSize = command.PageSize;
            model.PagingFilteringContext.OrderBy          = command.OrderBy;
            model.PagingFilteringContext.PrepareSortingOptions();
            model.PagingFilteringContext.ViewMode = command.ViewMode;
            model.PagingFilteringContext.PrepareViewModeOptions();

            return(View(model));
        }