Пример #1
0
        public PagedProductViewModel FetchProducts(string categorySlug, string brandSlug)
        {
            var brands     = _brandRepository.GetAllBrands().Where(brand => brand.IsDeleted == false);
            var categories = _categoryRepository.GetAllCategories().Where(category => category.IsDeleted == false);

            var productPage = GetCurrentPage();

            IEnumerable <Product> products = new List <Product>();
            var productCount = 0;


            productCount = GetProducts(categorySlug, brandSlug, productPage, out products);


            var totalPages = (int)Math.Ceiling((decimal)productCount / _productPerPage);

            int[] pages = Enumerable.Range(1, totalPages).ToArray();

            var pagedProduct = new PaginationViewModel
            {
                Products         = products,
                HasPreviousPages = (productPage > 1),
                CurrentPage      = productPage,
                HasNextPages     = (productPage < totalPages),
                Pages            = pages
            };
            var pagedProducts = new PagedProductViewModel
            {
                PagedProducts = pagedProduct,
                Brands        = brands,
                Categories    = categories
            };

            return(pagedProducts);
        }
        public PagedProductViewModel PrepareProductmodel(ProductFilterViewModel filterModel = null)
        {
            var products = _productService.GetAllProducts(filterModel, page: filterModel.PageNumber, pageSize: filterModel.PageSize);

            var model = new PagedProductViewModel
            {
                PagingFilterModel = new PagingFilterModel
                {
                    PageIndex       = products.PageIndex,
                    PageSize        = products.PageSize,
                    TotalCount      = products.TotalCount,
                    TotalPages      = products.TotalPages,
                    Storage         = filterModel.Storage,
                    OperatingSystem = filterModel.OperatingSystem,
                    Proccessor      = filterModel.Processor,
                    FromPrice       = filterModel.FromPrice,
                    ToPrice         = filterModel.ToPrice
                },
                Products = products.Select(p => new ProductViewModel
                {
                    Id         = p.Id,
                    Name       = p.Name,
                    PictureUrl = _pictureService?.GetPictureUrl(p.Pictures.FirstOrDefault()),
                    Price      = p.Price
                }).ToList()
            };

            return(model);
        }
Пример #3
0
        public PagedProductViewModel FetchProducts(string categorySlug, string brandSlug)
        {
            var brands     = _brandRepository.GetAllBrands().Where(brand => brand.isDeleted == false);
            var categories = _categoryRepository.GetAllCategories().Where(cat => cat.isDeleted == false);

            var productPage = GetCurrentPage();

            IEnumerable <Product> products = new List <Product>();
            int productCount = 0;

            if (categorySlug == "all-categories" && brandSlug == "all-brands")
            {
                productCount = _productRepository.GetAllProducts().Count();
                products     = _productRepository.GetAllProducts()
                               .Where(product => product.ProductStatus == ProductStatus.Active)
                               .Skip((productPage - 1) * _productsPerPage)
                               .Take(_productsPerPage);
            }

            if (categorySlug != "all-categories" && brandSlug != "all-brands")
            {
                var filteredProducts = _productRepository.GetAllProducts()
                                       .Where(product => product.ProductStatus == ProductStatus.Active &&
                                              product.Category.Slug == categorySlug &&
                                              product.Brand.Slug == brandSlug);

                productCount = filteredProducts.Count();

                products = filteredProducts.Skip((productPage - 1) * _productsPerPage)
                           .Take(_productsPerPage);
            }

            if (categorySlug != "all-categories" && brandSlug == "all-brands")
            {
                var filteredProducts = _productRepository.GetAllProducts()
                                       .Where(product => product.ProductStatus == ProductStatus.Active &&
                                              product.Category.Slug == categorySlug);

                productCount = filteredProducts.Count();

                products = filteredProducts.Skip((productPage - 1) * _productsPerPage)
                           .Take(_productsPerPage);
            }

            if (categorySlug == "all-categories" && brandSlug != "all-brands")
            {
                var filteredProducts = _productRepository.GetAllProducts()
                                       .Where(product => product.ProductStatus == ProductStatus.Active &&
                                              product.Brand.Slug == brandSlug);

                productCount = filteredProducts.Count();

                products = filteredProducts.Skip((productPage - 1) * _productsPerPage)
                           .Take(_productsPerPage);
            }

            var totalPages = (int)Math.Ceiling((decimal)productCount / _productsPerPage);

            int[] pages = Enumerable.Range(1, totalPages).ToArray();

            var pagedProduct = new PaginationViewModel
            {
                Products         = products,
                HasPreviousPages = (productPage > 1),
                CurrentPage      = productPage,
                HasNextPages     = (productPage < totalPages),
                Pages            = pages
            };

            var pagedProducts = new PagedProductViewModel
            {
                PagedProducts = pagedProduct,
                Brands        = brands,
                Categories    = categories
            };

            return(pagedProducts);
        }