public async Task <IActionResult> Get([FromRoute] int catalogId)
        {
            var catalogs = await GetWithParentsRecursive(catalogId);

            var spec = new ProductFilterSpec();

            spec.Query.Where(LambdaExpression(catalogs.Select(a => a.Id)));
            spec.Query.Include(nameof(Product.Specifications));

            var products = await _unitOfWork.ProductRepository.ListAsync(spec);

            var specifications = products.SelectMany(a => a.Specifications);
            var specDtos       = _mapper.Map <IEnumerable <ProductSpecificationDto> >(specifications);

            return(Ok(specDtos));
        }
示例#2
0
        public async Task <IActionResult> Get(
            [FromQuery(Name = "pageIndex")] int?pageIndex     = null,
            [FromQuery(Name = "search")] ProductSearch search = null,
            [FromQuery(Name = "sort")] ProductSort?sort       = null,
            [FromQuery(Name = "pageSize")] int pageSize       = 10)
        {
            var spec = new ProductFilterSpec();

            if (sort != null)
            {
                switch (sort)
                {
                case ProductSort.PriceAsc:
                    spec.Query.OrderBy(a => a.Price);
                    break;

                case ProductSort.PriceDesc:
                    spec.Query.OrderByDescending(a => a.Price);
                    break;

                case ProductSort.DateAsc:
                    spec.Query.OrderBy(a => a.Date);
                    break;

                case ProductSort.DateDesc:
                    spec.Query.OrderByDescending(a => a.Date);
                    break;

                case ProductSort.OrdersAsc:
                    spec.Query.OrderBy(a => a.OrderProducts.Count);
                    break;

                case ProductSort.OrdersDesc:
                    spec.Query.OrderByDescending(a => a.OrderProducts.Count);
                    break;

                default:
                    break;
                }
            }

            var lambdaCombiner = new LambdaExpressionCombiner <Product>();

            if (!search.Names.IsNullOrEmpty())
            {
                lambdaCombiner.Add(new ProductByNameContainsSpec(search.Names).WhereExpressions.First());
            }

            if (!search.Ids.IsNullOrEmpty())
            {
                lambdaCombiner.Add(new ProductByIdSpec(search.Ids).WhereExpressions.First());
            }

            if (!search.GroupIds.IsNullOrEmpty())
            {
                lambdaCombiner.Add(new ProductByGroupIdSpec(search.GroupIds).WhereExpressions.First());
            }

            if (!search.CatalogIds.IsNullOrEmpty())
            {
                lambdaCombiner.Add(new ProductByCatalogIdSpec(search.CatalogIds).WhereExpressions.First());
            }

            if (!search.BrandIds.IsNullOrEmpty())
            {
                lambdaCombiner.Add(new ProductByBrandIdSpec(search.BrandIds).WhereExpressions.First());
            }

            if (search.PriceRange != null)
            {
                lambdaCombiner.Add(new ProductByPriceRangeSpec(search.PriceRange.From, search.PriceRange.To).WhereExpressions.First());
            }

            if (!search.Specifications.IsNullOrEmpty())
            {
                lambdaCombiner.Add(new ProductBySpecNameIdAndValueIdSpec(search.Specifications.ToArray()).WhereExpressions.First());
            }

            var lambda = lambdaCombiner.Combine(ExpressionType.AndAlso);

            if (lambda != null)
            {
                spec.Query.Where(lambda);
            }

            if (pageIndex.HasValue)
            {
                spec.Query.Paginate(pageIndex.Value * pageSize, pageSize);

                var totalCount = 0;

                if (lambda is null)
                {
                    totalCount = await _unitOfWork.ProductRepository.CountAsync();
                }
                else
                {
                    var noPagingSpec = new ProductFilterSpec();
                    noPagingSpec.Query.Where(lambda);
                    totalCount = await _unitOfWork.ProductRepository.CountAsync(noPagingSpec);
                }

                var items         = _mapper.Map <IEnumerable <ProductDto> >(await _unitOfWork.ProductRepository.ListAsync(spec));
                var paginatedList = new Page <ProductDto>(items, totalCount, pageIndex.Value, pageSize);

                return(Ok(paginatedList));
            }

            return(Ok(_mapper.Map <IEnumerable <ProductDto> >(await _unitOfWork.ProductRepository.ListAsync(spec))));
        }
示例#3
0
        public async Task <IActionResult> CatalogProducts(
            [FromRoute] int catalogId,
            [FromQuery] int?page = null,
            [FromQuery(Name = "search")] ProductSearch search = null,
            [FromQuery(Name = "sort")] SortModel sort         = null,
            [FromQuery] int pageSize = 10)
        {
            var catalogs = await GetChildsWithSelfRecursive(catalogId);

            if (catalogs.IsNullOrEmpty())
            {
                return(NoContent());
            }

            var spec = new ProductFilterSpec();

            if (!sort.IsNullOrEmpty())
            {
                foreach (var item in sort)
                {
                    if (Enum.TryParse <OrderBy>(item.Value, true, out var order))
                    {
                        if (item.Key.Equals(nameof(Product.Name), StringComparison.OrdinalIgnoreCase))
                        {
                            if (order == OrderBy.ASC)
                            {
                                spec.Query.OrderBy(a => a.Name);
                            }
                            else if (order == OrderBy.DESC)
                            {
                                spec.Query.OrderByDescending(a => a.Name);
                            }
                        }
                        if (item.Key.Equals(nameof(Product.Price), StringComparison.OrdinalIgnoreCase))
                        {
                            if (order == OrderBy.ASC)
                            {
                                spec.Query.OrderBy(a => a.Price);
                            }
                            else if (order == OrderBy.DESC)
                            {
                                spec.Query.OrderByDescending(a => a.Price);
                            }
                        }
                        if (item.Key.Equals(nameof(Product.Date), StringComparison.OrdinalIgnoreCase))
                        {
                            if (order == OrderBy.ASC)
                            {
                                spec.Query.OrderBy(a => a.Date);
                            }
                            else if (order == OrderBy.DESC)
                            {
                                spec.Query.OrderByDescending(a => a.Date);
                            }
                        }
                    }
                }
            }

            var lambdaCombiner = new LambdaExpressionCombiner <Product>();

            var spec1 = new ProductByCatalogIdSpec(catalogs.Select(a => a.Id).ToArray());

            lambdaCombiner.Add(spec1.WhereExpressions.First());

            if (!search.BrandIds.IsNullOrEmpty())
            {
                var spec2 = new ProductByBrandIdSpec(search.BrandIds);
                lambdaCombiner.Add(spec2.WhereExpressions.First());
            }

            if (search.PriceRange != null && (search.PriceRange.From.HasValue || search.PriceRange.To.HasValue))
            {
                lambdaCombiner.Add(new ProductByPriceRangeSpec(search.PriceRange.From, search.PriceRange.To).WhereExpressions.First());
            }

            if (!search.Specifications.IsNullOrEmpty())
            {
                lambdaCombiner.Add(new ProductBySpecNameIdAndValueIdSpec(search.Specifications.ToArray()).WhereExpressions.First());
            }

            var lambda = lambdaCombiner.Combine(ExpressionType.AndAlso);

            if (lambda != null)
            {
                spec.Query.Where(lambda);
            }

            if (page.HasValue)
            {
                spec.Query.Paginate((page.Value) * pageSize, pageSize);

                var totalCount = 0;

                if (lambda is null)
                {
                    totalCount = await _unitOfWork.ProductRepository.CountAsync();
                }
                else
                {
                    var noPagingSpec = new ProductFilterSpec();
                    noPagingSpec.Query.Where(lambda);
                    totalCount = await _unitOfWork.ProductRepository.CountAsync(noPagingSpec);
                }

                var items         = _mapper.Map <IEnumerable <ProductDto> >(await _unitOfWork.ProductRepository.ListAsync(spec));
                var paginatedList = new Page <ProductDto>(items, totalCount, page.Value, pageSize);

                return(Ok(paginatedList));
            }

            return(Ok(_mapper.Map <IEnumerable <ProductDto> >(await _unitOfWork.ProductRepository.ListAsync(spec))));
        }