예제 #1
0
        public ProductsWithTypesAndBrandsSpecification(ProductSpecParamsModel productParamsModel)
        {
            Criteria = BaseCriteria(productParamsModel);
            IncludeReferenceSpec();
            ApplyPaging(productParamsModel.PageSize * (productParamsModel.PageIndex - 1), productParamsModel.PageSize);
            var sortSortProperty = productParamsModel.Sort;

            switch (sortSortProperty)
            {
            case SortBy.PriceAsc:
                AddOrderBy(p => p.Price);
                break;

            case SortBy.PriceDesc:
                AddOrderByDescending(p => p.Price);
                break;

            case SortBy.NameAsc:
                AddOrderBy(product => product.Name ?? string.Empty);
                break;

            case SortBy.NameDesc:
                AddOrderByDescending(product => product.Name ?? string.Empty);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(sortSortProperty), sortSortProperty, null);
            }
        }
 private static Expression <Func <Product, bool> > BaseCriteria(ProductSpecParamsModel productParamsModel)
 {
     return(product =>
            product.Name != null && (string.IsNullOrEmpty(productParamsModel.Search) ||
                                     product.Name.ToLower().Contains(productParamsModel.Search)) &&
            (!productParamsModel.BrandId.HasValue || product.ProductBrandId == productParamsModel.BrandId) &&
            (!productParamsModel.TypeId.HasValue || product.ProductTypeId == productParamsModel.TypeId));
 }
예제 #3
0
        public async Task GetProductsAsync_ShouldReturnAListOfProduct()
        {
            // Arrange
            var paramsModel = new ProductSpecParamsModel();
            // Act
            var products = await _sut.GetProducts(paramsModel);

            // Assert
            var actionResult = Assert.IsType <ActionResult <Pagination <ProductToReturnDto> > >(products);
            var productList  =
                Assert.IsType <Pagination <ProductToReturnDto> >(((OkObjectResult)actionResult.Result).Value);

            Assert.Equal(_fixture.SeedEntries, productList.Count);
        }
예제 #4
0
        public async Task <ActionResult <Pagination <ProductToReturnDto> > > GetProducts(
            [FromQuery] ProductSpecParamsModel productParamsModel)
        {
            var spec       = new ProductsWithTypesAndBrandsSpecification(productParamsModel);
            var countSpec  = new ProductWithFiltersForCountSpecification(productParamsModel);
            var totalItems = await _genericRepo.CountEntityAsync(countSpec);

            var products = await _genericRepo.ListEntityAsync(spec);

            var mappedProducts = _mapper.Map <IReadOnlyList <Product>, IReadOnlyList <ProductToReturnDto> >(products);

            return(Ok(new Pagination <ProductToReturnDto>(
                          productParamsModel.PageIndex, productParamsModel.PageSize, totalItems, mappedProducts
                          )));
        }
 public ProductWithFiltersForCountSpecification(ProductSpecParamsModel productParamsModel)
 {
     Criteria = BaseCriteria(productParamsModel);
 }