public async Task ListPaginationCount()
        {
            // Arrange
            var filter = new ProductFilter
            {
                Page = 1,
                Rows = 10
            };

            var objectsList = new ProductPaginationViewModel()
            {
                Rows = new List <ProductViewModel> {
                    new ProductViewModel {
                        code = "001"
                    }
                }
            };
            var searchResults = Task.FromResult(objectsList);

            _productQueryMock.Setup(m => m.Pagination(filter)).Returns(searchResults);

            // Act
            var actual = await _productQueryMock.Object.Pagination(filter);

            // Assert
            actual.Rows.Count().Should().BeGreaterThan(0);
        }
Exemple #2
0
        public async Task <ProductPaginationViewModel> GetProductsPagination(int page, int size, string search)
        {
            var item = await _productRepository.ListAllAsync();

            var vm = item.Select(i => new ProductViewModel()
            {
                Id          = i.Id,
                Name        = i.Name,
                CategoryId  = i.CategoryId,
                Description = i.Description,
                MainUrl     = i.MainUrl,
                Price       = i.Price,
                BrandId     = i.BrandId,
                Image1      = i.Image1,
                Image2      = i.Image2,
                Image3      = i.Image3,
                CountOrder  = i.CountOrder
            }).ToList();
            int length = vm.Count();

            if (!String.IsNullOrEmpty(search))
            {
                vm = vm.Where(i => i.Name.Contains(search)).ToList();
            }
            vm = vm.Skip((page - 1) * size).Take(size).ToList();
            ProductPaginationViewModel productPg = new ProductPaginationViewModel()
            {
                ProductList   = vm,
                ProductLength = length
            };

            return(productPg);
        }
Exemple #3
0
        public async Task <ProductPaginationViewModel> GetProductsByCategoryAndBrandPaging(int page, int size, string search, string category, string brand, int stPrice, int enPrice)
        {
            var listPr = await _productRepository.ListAllAsync();

            var list = new List <Product>();

            listPr = listPr.Where(i => i.Price > Convert.ToDouble(stPrice) && i.Price < Convert.ToDouble(enPrice)).ToList();
            if (!String.IsNullOrEmpty(category))
            {
                List <string> listCategory = category.Split(new char[] { ',' }).ToList();
                foreach (var item in listCategory)
                {
                    var i = Int32.Parse(item);
                    list.AddRange(listPr.Where(p => p.CategoryId == i));
                }
            }
            if (!String.IsNullOrEmpty(brand))
            {
                List <string> listBrand = brand.Split(new char[] { ',' }).ToList();
                foreach (var item in listBrand)
                {
                    var j = Int32.Parse(item);
                    list.AddRange(listPr.Where(p => p.BrandId == j));
                }
            }
            if (String.IsNullOrEmpty(category) && String.IsNullOrEmpty(brand))
            {
                list = listPr;
            }
            var vm = list.Select(i => new ProductViewModel()
            {
                Id          = i.Id,
                Name        = i.Name,
                CategoryId  = i.CategoryId,
                Description = i.Description,
                MainUrl     = i.MainUrl,
                Price       = i.Price,
                BrandId     = i.BrandId,
                Image1      = i.Image1,
                Image2      = i.Image2,
                Image3      = i.Image3,
                CountOrder  = i.CountOrder
            }).ToList();

            if (!String.IsNullOrEmpty(search))
            {
                vm = vm.Where(i => i.Name.Contains(search)).ToList();
            }
            int length = vm.Count();

            vm = vm.Skip((page - 1) * size).Take(size).ToList();
            ProductPaginationViewModel productPg = new ProductPaginationViewModel()
            {
                ProductList   = vm,
                ProductLength = length
            };

            return(productPg);
        }
Exemple #4
0
        public async Task GetPaginationProductVerifyIsCalled()
        {
            // Arrange
            var productFilter = new ProductFilter
            {
                Page = 1,
                Rows = 10
            };

            var resultQuery = new ProductPaginationViewModel
            {
                Records = 10
            };

            _IProductQueryMock.Setup(m => m.Pagination(productFilter)).ReturnsAsync(resultQuery);

            // Act

            var actual = await _sut.Pagination(productFilter);

            // Assert
            ((ObjectResult)actual).StatusCode.Should().Be((int)HttpStatusCode.OK);
        }
Exemple #5
0
        public async Task <ProductPaginationViewModel> Pagination(ProductFilter filter)
        {
            var result = new ProductPaginationViewModel();

            using (var cn = new SqlConnection(_connectionString))
            {
                await cn.OpenAsync();

                var param = new DynamicParameters();
                param.Add("@Page", filter.Page, DbType.Int32);
                param.Add("@Rows", filter.Rows, DbType.Int32);
                param.Add("@Records", 1, DbType.Int32, ParameterDirection.InputOutput);
                param.Add("@Total", 1, DbType.Int32, ParameterDirection.InputOutput);

                result.Page = filter.Page;
                result.Rows = await cn.QueryAsync <ProductViewModel>(@"[dbo].[sp_get_product_pagination]", param, commandType : CommandType.StoredProcedure);

                result.Records = param.Get <int>("@Records");
                result.Total   = param.Get <int>("@Total");
            }

            return(result);
        }
Exemple #6
0
        public async Task <ActionResult <ProductPaginationViewModel> > All(string area, int?currentPage = 1, string search = null)
        {
            int count = 0;

            var query = this.dbContext
                        .Products
                        .Where(x => !x.IsArchived);

            if (this.currentUserService.IsAdministrator &&
                area == AuthConstants.AdminAreaName)
            {
                query = this.dbContext
                        .Products;
            }

            if (!string.IsNullOrWhiteSpace(search))
            {
                query = query
                        .Where(x => x.Name.ToLower().Contains(search.ToLower()));

                count = await this.dbContext
                        .Products
                        .CountAsync(x => x.Name.ToLower().Contains(search.ToLower()));
            }
            else
            {
                count = await this.dbContext
                        .Products
                        .CountAsync();
            }

            int size       = ProductConstants.MaxTakeCount;
            int totalPages = (int)Math.Ceiling(decimal.Divide(count, size));

            if (currentPage < 1)
            {
                currentPage = 1;
            }
            if (currentPage > totalPages)
            {
                currentPage = totalPages;
            }

            int skip = (int)(currentPage - 1) * size;

            if (skip < 0)
            {
                skip = 0;
            }
            int take = size;

            var productPaginationViewModel = new ProductPaginationViewModel
            {
                Search = search,
                PaginationViewModel = new PaginationViewModel
                {
                    CurrentPage = currentPage.Value,
                    TotalPages  = totalPages
                },
                ProductIndexViewModels = await query
                                         .Skip(skip)
                                         .Take(take)
                                         .Select(x => new ProductIndexViewModel()
                {
                    Id          = x.Id,
                    Description = x.Description,
                    Name        = x.Name,
                    Price       = x.Price,
                    ImageUrl    = x.ImageUrl,
                    IsArchived  = x.IsArchived
                })
                                         .ToListAsync()
            };

            return(this.Ok(productPaginationViewModel));
        }