public async Task <ProductCatalogDto> ListProductsAsync(int pageIndex, int itemsPage, string orderBy, string search) { var filterPaginatedEspecification = new ProductFilterPaginatedSpecification(itemsPage * pageIndex, itemsPage, orderBy, search); var filterSpecification = new ProductFilterSpecification(orderBy, search); var itemsOnPage = await _productRepository.ListAsync(filterPaginatedEspecification); var totalItems = await _productRepository.CountAsync(filterSpecification); var products = new ProductCatalogDto() { Products = itemsOnPage.Select(s => s.MapProductDto()).ToList(), PaginationInfo = new PaginationInfoDto() { ActualPage = pageIndex, ItemsPerPage = itemsOnPage.Count, TotalItems = totalItems, TotalPages = int.Parse(Math.Ceiling(((decimal)totalItems / itemsPage)).ToString()) } }; products.PaginationInfo.Next = (products.PaginationInfo.ActualPage == products.PaginationInfo.TotalPages - 1) ? "is-disabled" : ""; products.PaginationInfo.Previous = (products.PaginationInfo.ActualPage == 0) ? "is-disabled" : ""; return(products); }
public async Task <HomeIndexViewModel> GetHomeIndexViewModel(int?categoryId, int?brandId, int pageId) { var spec = new ProductFilterPaginatedSpecification(categoryId, brandId, (pageId - 1) * Constants.ITEMS_PER_PAGE, Constants.ITEMS_PER_PAGE); var specAll = new ProductFilterSpecification(categoryId, brandId); var products = await _productRepository.ListAsync(spec); var allCount = await _productRepository.CountAsync(specAll); var totalPages = (int)Math.Ceiling(allCount / (double)Constants.ITEMS_PER_PAGE); return(new HomeIndexViewModel() { Products = products.Select(x => new ProductViewModel() { Id = x.Id, Name = x.Name, Price = x.Price, PictureUri = x.PictureUri }).ToList(), Categories = await GetCategoryListItems(), Brands = await GetBrandListItems(), PaginationInfo = new PaginationInfoViewModel() { ItemsOnPage = products.Count, TotalItems = allCount, CurrentPage = pageId, TotalPages = totalPages, HasPrevious = pageId > 1, HasNext = pageId < totalPages } }); }
public async Task <ResultModel <ProductIndexViewModel> > GetProductList(int pageIndex, int itemsPage, int?supplierId, int?categoryId) { _logger.LogInformation("GetProductList called."); var filterSpecification = new ProductFilterSpecification(supplierId, categoryId); var filterPaginatedSpecification = new ProductFilterPaginatedSpecification(itemsPage * pageIndex, itemsPage, supplierId, categoryId); var itemsOnPage = await _productRepository.ListAsync(filterPaginatedSpecification); var totalItems = await _productRepository.CountAsync(filterSpecification); var vm = new ProductIndexViewModel() { ProductList = itemsOnPage.Select(i => new ProductDetailViewModel() { Id = i.Id, Name = i.Name, Sku = i.Sku, Quantity = i.Quantity, Description = i.Description, CategoryId = i.CategoryId, SupplierId = i.SupplierId }).ToPagedList(totalItems, int.Parse(Math.Ceiling(((decimal)totalItems / itemsPage)).ToString()), itemsPage, ++pageIndex), Suppliers = (await GetSuppliers()).Data, Categories = (await GetCategories()).Data, SupplierFilterApplied = supplierId, CategoryFilterApplied = categoryId }; return(vm.ToResultModel()); }