Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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
                }
            });
        }
Exemplo n.º 3
0
        public async Task <PaginatedResult <GetAllPagedProductsResponse> > Handle(GetAllProductsQuery request, CancellationToken cancellationToken)
        {
            Expression <Func <Product, GetAllPagedProductsResponse> > expression = e => new GetAllPagedProductsResponse
            {
                Id          = e.Id,
                Name        = e.Name,
                Description = e.Description,
                Rate        = e.Rate,
                Barcode     = e.Barcode,
                Brand       = e.Brand.Name,
                BrandId     = e.BrandId
            };
            var productFilterSpec = new ProductFilterSpecification(request.SearchString);

            if (request.OrderBy?.Any() != true)
            {
                var data = await _unitOfWork.Repository <Product>().Entities
                           .Specify(productFilterSpec)
                           .Select(expression)
                           .ToPaginatedListAsync(request.PageNumber, request.PageSize);

                return(data);
            }
            else
            {
                var ordering = string.Join(",", request.OrderBy); // of the form fieldname [ascending|descending], ...
                var data     = await _unitOfWork.Repository <Product>().Entities
                               .Specify(productFilterSpec)
                               .OrderBy(ordering) // require system.linq.dynamic.core
                               .Select(expression)
                               .ToPaginatedListAsync(request.PageNumber, request.PageSize);

                return(data);
            }
        }
Exemplo n.º 4
0
        private CatalogModel FilterCatalog(ProductFilter filter)
        {
            // Should resolve IRepository from IoC Container
            IRepository repository = new EntityFrameworkRepository(new CatalogDataContext("name=CatalogDataContext"));

            // Create specification
            var specification = new ProductFilterSpecification(filter.PriceFrom, filter.PriceTo);

            // Get products
            var products = repository.Query(specification);

            return new CatalogModel { Filter = filter, Products = products };
        }
Exemplo n.º 5
0
        private CatalogModel FilterCatalog(ProductFilter filter)
        {
            // Should resolve IRepository from IoC Container
            IRepository repository = new EntityFrameworkRepository(new CatalogDataContext("name=CatalogDataContext"));

            // Create specification
            var specification = new ProductFilterSpecification(filter.PriceFrom, filter.PriceTo);

            // Get products
            var products = repository.Query(specification);


            return(new CatalogModel {
                Filter = filter, Products = products
            });
        }
Exemplo n.º 6
0
        public async Task <Result <string> > Handle(ExportProductsQuery request, CancellationToken cancellationToken)
        {
            var productFilterSpec = new ProductFilterSpecification(request.SearchString);
            var products          = await _unitOfWork.Repository <Product>().Entities
                                    .Specify(productFilterSpec)
                                    .ToListAsync(cancellationToken);

            var data = await _excelService.ExportAsync(products, mappers : new Dictionary <string, Func <Product, object> >
            {
                { _localizer["Id"], item => item.Id },
                { _localizer["Name"], item => item.Name },
                { _localizer["Barcode"], item => item.Barcode },
                { _localizer["Description"], item => item.Description },
                { _localizer["Rate"], item => item.Rate }
            }, sheetName : _localizer["Products"]);

            return(await Result <string> .SuccessAsync(data : data));
        }
Exemplo n.º 7
0
        public async Task <PaginatedResult <GetAllPagedProductsResponse> > Handle(GetAllProductsQuery request, CancellationToken cancellationToken)
        {
            Expression <Func <Product, GetAllPagedProductsResponse> > expression = e => new GetAllPagedProductsResponse
            {
                Id          = e.Id,
                Name        = e.Name,
                Description = e.Description,
                Rate        = e.Rate,
                Barcode     = e.Barcode
            };
            var productFilterSpec = new ProductFilterSpecification(request.SearchString);
            var data = await _repository.Products
                       .Specify(productFilterSpec)
                       .Select(expression)
                       .ToPaginatedListAsync(request.PageNumber, request.PageSize);

            return(data);
        }
Exemplo n.º 8
0
        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());
        }