예제 #1
0
        public async Task <ActionResult <CatalogVM> > Products(CatalogVM catalogVm, uint page)
        {
            CatalogFacetsCriteriesDTO criteriesDto = await _catalogManager.FacetsCriteries(catalogVm?.Category);

            ProductsListDTO productsListDto;

            if (catalogVm == null)
            {
                productsListDto = await _catalogManager.Catalog(null);

                catalogVm = new CatalogVM
                {
                    Category         = null,
                    SortField        = CatalogSortField.Price,
                    SortingDirection = SortingDirection.Ascending
                };
            }
            else
            {
                productsListDto = await _catalogManager.Catalog(new CatalogFiltersDTO
                {
                    Brands           = catalogVm.Brands,
                    Characteristics  = catalogVm.Characteristics,
                    Countries        = catalogVm.Countries,
                    Category         = catalogVm.Category,
                    SortField        = catalogVm.SortField,
                    SortingDirection = catalogVm.SortingDirection,
                    EndPrice         = catalogVm.EndPrice,
                    EndWeight        = catalogVm.EndWeight,
                    StartPrice       = catalogVm.StartPrice,
                    StartWeight      = catalogVm.StartWeight,
                    PageSize         = PageSize,
                    Page             = page
                });
            }

            // При одинаковом названии характеристик и свойств продукта выбросится исключение!
            catalogVm.FacetsCriteries = criteriesDto.Characteristics
                                        .Select(c =>
            {
                HashSet <string> values = null;
                catalogVm.Characteristics?.TryGetValue(c.Key, out values);

                var facetCriterionVm = new FacetCriterionVM(c.Key, nameof(catalogVm.Characteristics))
                {
                    All      = c.Value,
                    Selected = values
                };

                return(facetCriterionVm);
            })
                                        .Append(new FacetCriterionVM(nameof(catalogVm.Countries))
            {
                All      = criteriesDto.Countries,
                Selected = catalogVm.Countries
            })
                                        .Append(new FacetCriterionVM(nameof(catalogVm.Brands))
            {
                All      = criteriesDto.Brands,
                Selected = catalogVm.Brands
            })
                                        .ToHashSet();

            catalogVm.MaxPrice  = criteriesDto.MaxPrice;
            catalogVm.MaxWeight = criteriesDto.MaxWeight;
            catalogVm.MinPrice  = criteriesDto.MinPrice;
            catalogVm.MinWeight = criteriesDto.MinWeight;

            catalogVm.Products = productsListDto.Products.Select(p => new ProductShortVM
            {
                Id      = p.Id,
                Name    = p.Name,
                Brand   = p.Brand?.Name,
                Country = p.Country?.Name,
                Image   = p.Image,
                Price   = p.Price,
                Weight  = p.Weight
            });

            catalogVm.PagingInfo = new PagingInfoVM
            {
                CurrentPage  = productsListDto.PagingInfo.CurrentPage,
                TotalItems   = productsListDto.PagingInfo.TotalItems,
                ItemsPerPage = productsListDto.PagingInfo.ItemsPerPage
            };

            return(View(catalogVm));
        }