示例#1
0
        public async Task <IActionResult> Get(
            [FromQuery] int?page = null,
            [FromQuery(Name = "search")] OrderSearchModel search = null,
            [FromQuery(Name = "sort")] SortModel sort            = null,
            [FromQuery] int pageSize = 10)
        {
            var spec = new OrderFilterSpec();

            if (!sort.IsNullOrEmpty())
            {
                foreach (var item in sort)
                {
                    if (Enum.TryParse <OrderBy>(item.Value, true, out var order))
                    {
                        if (item.Key.Equals(nameof(Order.Id), StringComparison.OrdinalIgnoreCase))
                        {
                            if (order == OrderBy.ASC)
                            {
                                spec.Query.OrderBy(a => a.Id);
                            }
                            else if (order == OrderBy.DESC)
                            {
                                spec.Query.OrderByDescending(a => a.Id);
                            }
                        }
                        if (item.Key.Equals(nameof(Order.Comment), StringComparison.OrdinalIgnoreCase))
                        {
                            if (order == OrderBy.ASC)
                            {
                                spec.Query.OrderBy(a => a.Comment);
                            }
                            else if (order == OrderBy.DESC)
                            {
                                spec.Query.OrderByDescending(a => a.Comment);
                            }
                        }
                        if (item.Key.Equals(nameof(Order.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 <Order>();

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

            if (!search.Comments.IsNullOrEmpty())
            {
                lambdaCombiner.Add(new OrderByCommentContainsSpec(search.Comments).WhereExpressions.First());
            }

            if (!search.BuyerIds.IsNullOrEmpty())
            {
                lambdaCombiner.Add(new OrderByBuyerIdSpec(search.BuyerIds).WhereExpressions.First());
            }

            if (!search.AddressIds.IsNullOrEmpty())
            {
                lambdaCombiner.Add(new OrderByAddressIdSpec(search.AddressIds).WhereExpressions.First());
            }

            if (!search.Statuses.IsNullOrEmpty())
            {
                lambdaCombiner.Add(new OrderByStatusSpec(search.Statuses).WhereExpressions.First());
            }

            if (!search.UserOrAnonymousIds.IsNullOrEmpty())
            {
                lambdaCombiner.Add(new OrderByUserOrAnonymousIdSpec(search.UserOrAnonymousIds).WhereExpressions.First());
            }

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

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

            var items = _mapper.Map <IEnumerable <OrderDto> >(await _unitOfWork.OrderRepository.ListAsync(spec));

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

                var totalCount = 0;

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

                var paginatedList = new Page <OrderDto>(items, totalCount, page.Value, pageSize);

                return(Ok(paginatedList));
            }

            return(Ok(items));
        }
示例#2
0
        public async Task <IActionResult> Get(
            [FromQuery] int?page = null,
            [FromQuery(Name = "search")] BuyerSearchModel search = null,
            [FromQuery(Name = "sort")] SortModel sort            = null,
            [FromQuery] int pageSize = 10)
        {
            var spec = new BuyerFilterSpec();

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

            if (!sort.IsNullOrEmpty())
            {
                foreach (var item in sort)
                {
                    if (Enum.TryParse <OrderBy>(item.Value, true, out var order))
                    {
                        if (item.Key.Equals(nameof(Buyer.Email), StringComparison.OrdinalIgnoreCase))
                        {
                            if (order == OrderBy.ASC)
                            {
                                spec.Query.OrderBy(a => a.Email);
                            }
                            else if (order == OrderBy.DESC)
                            {
                                spec.Query.OrderByDescending(a => a.Email);
                            }
                        }
                        if (item.Key.Equals(nameof(Buyer.FirstName), StringComparison.OrdinalIgnoreCase))
                        {
                            if (order == OrderBy.ASC)
                            {
                                spec.Query.OrderBy(a => a.FirstName);
                            }
                            else if (order == OrderBy.DESC)
                            {
                                spec.Query.OrderByDescending(a => a.FirstName);
                            }
                        }
                        if (item.Key.Equals(nameof(Buyer.LastName), StringComparison.OrdinalIgnoreCase))
                        {
                            if (order == OrderBy.ASC)
                            {
                                spec.Query.OrderBy(a => a.LastName);
                            }
                            else if (order == OrderBy.DESC)
                            {
                                spec.Query.OrderByDescending(a => a.LastName);
                            }
                        }
                        if (item.Key.Equals(nameof(Buyer.SurName), StringComparison.OrdinalIgnoreCase))
                        {
                            if (order == OrderBy.ASC)
                            {
                                spec.Query.OrderBy(a => a.SurName);
                            }
                            else if (order == OrderBy.DESC)
                            {
                                spec.Query.OrderByDescending(a => a.SurName);
                            }
                        }
                        if (item.Key.Equals(nameof(Buyer.PhoneNumber), StringComparison.OrdinalIgnoreCase))
                        {
                            if (order == OrderBy.ASC)
                            {
                                spec.Query.OrderBy(a => a.PhoneNumber);
                            }
                            else if (order == OrderBy.DESC)
                            {
                                spec.Query.OrderByDescending(a => a.PhoneNumber);
                            }
                        }
                        if (item.Key.Equals(nameof(Buyer.UserOrAnonymousId), StringComparison.OrdinalIgnoreCase))
                        {
                            if (order == OrderBy.ASC)
                            {
                                spec.Query.OrderBy(a => a.UserOrAnonymousId);
                            }
                            else if (order == OrderBy.DESC)
                            {
                                spec.Query.OrderByDescending(a => a.UserOrAnonymousId);
                            }
                        }
                    }
                }
            }

            var        parameter  = Expression.Parameter(typeof(Buyer));
            Expression expression = null;

            if (!search.FirstNames.IsNullOrEmpty())
            {
                var firstNamesOrExpression = search.Ids.Distinct().Select(firstName =>
                {
                    var propExpr  = Expression.Property(parameter, typeof(Buyer).GetProperty(nameof(Buyer.FirstName)));
                    var constExpr = Expression.Constant(firstName, typeof(string));

                    var method = typeof(string).GetMethod(nameof(Buyer.FirstName.Contains), new[] { typeof(string) });

                    return(Expression.Call(propExpr, method, constExpr).Reduce());
                })
                                             .Aggregate((expr1, expr2) =>
                {
                    var expr = Expression.OrElse(expr1, expr2);
                    return(expr);
                });

                if (expression is null)
                {
                    expression = firstNamesOrExpression;
                    _logger.LogInformation(expression.ToString());
                }
                else
                {
                    expression = Expression.AndAlso(expression, firstNamesOrExpression);
                }
            }

            if (!search.LastNames.IsNullOrEmpty())
            {
                var lastNamesOrExpression = search.LastNames.Distinct().Select(lastName =>
                {
                    var propExpr  = Expression.Property(parameter, typeof(Buyer).GetProperty(nameof(Buyer.LastName)));
                    var constExpr = Expression.Constant(lastName, typeof(string));

                    var method = typeof(string).GetMethod(nameof(Buyer.LastName.Contains), new[] { typeof(string) });

                    return(Expression.Call(propExpr, method, constExpr).Reduce());
                })
                                            .Aggregate((expr1, expr2) =>
                {
                    var expr = Expression.OrElse(expr1, expr2);
                    return(expr);
                });

                if (expression is null)
                {
                    expression = lastNamesOrExpression;
                    _logger.LogInformation(expression.ToString());
                }
                else
                {
                    expression = Expression.AndAlso(expression, lastNamesOrExpression);
                }
            }

            if (!search.SurNames.IsNullOrEmpty())
            {
                var surNamesOrExpression = search.LastNames.Distinct().Select(surName =>
                {
                    var propExpr  = Expression.Property(parameter, typeof(Buyer).GetProperty(nameof(Buyer.SurName)));
                    var constExpr = Expression.Constant(surName, typeof(string));

                    var method = typeof(string).GetMethod(nameof(Buyer.SurName.Contains), new[] { typeof(string) });

                    return(Expression.Call(propExpr, method, constExpr).Reduce());
                })
                                           .Aggregate((expr1, expr2) =>
                {
                    var expr = Expression.OrElse(expr1, expr2);
                    return(expr);
                });

                if (expression is null)
                {
                    expression = surNamesOrExpression;
                    _logger.LogInformation(expression.ToString());
                }
                else
                {
                    expression = Expression.AndAlso(expression, surNamesOrExpression);
                }
            }

            if (!search.Emails.IsNullOrEmpty())
            {
                var emailsOrExpression = search.LastNames.Distinct().Select(lastName =>
                {
                    var propExpr  = Expression.Property(parameter, typeof(Buyer).GetProperty(nameof(Buyer.Email)));
                    var constExpr = Expression.Constant(lastName, typeof(string));

                    var method = typeof(string).GetMethod(nameof(Buyer.Email.Contains), new[] { typeof(string) });

                    return(Expression.Call(propExpr, method, constExpr).Reduce());
                })
                                         .Aggregate((expr1, expr2) =>
                {
                    var expr = Expression.OrElse(expr1, expr2);
                    return(expr);
                });

                if (expression is null)
                {
                    expression = emailsOrExpression;
                    _logger.LogInformation(expression.ToString());
                }
                else
                {
                    expression = Expression.AndAlso(expression, emailsOrExpression);
                }
            }

            if (!search.PhoneNumbers.IsNullOrEmpty())
            {
                var phonesOrExpression = search.PhoneNumbers.Distinct().Select(lastName =>
                {
                    var propExpr  = Expression.Property(parameter, typeof(Buyer).GetProperty(nameof(Buyer.PhoneNumber)));
                    var constExpr = Expression.Constant(lastName, typeof(string));

                    var method = typeof(string).GetMethod(nameof(Buyer.PhoneNumber.Contains), new[] { typeof(string) });

                    return(Expression.Call(propExpr, method, constExpr).Reduce());
                })
                                         .Aggregate((expr1, expr2) =>
                {
                    var expr = Expression.OrElse(expr1, expr2);
                    return(expr);
                });

                if (expression is null)
                {
                    expression = phonesOrExpression;
                    _logger.LogInformation(expression.ToString());
                }
                else
                {
                    expression = Expression.AndAlso(expression, phonesOrExpression);
                }
            }

            if (!search.Ids.IsNullOrEmpty())
            {
                var idsOrExpression = search.Ids.Distinct().Select(id => Expression.Equal(
                                                                       Expression.Property(parameter, typeof(Buyer).GetProperty(nameof(Buyer.Id))),
                                                                       Expression.Constant(id, typeof(int))))
                                      .Aggregate((expr1, expr2) => Expression.OrElse(expr1, expr2));

                if (expression is null)
                {
                    expression = idsOrExpression;
                }
                else
                {
                    expression = Expression.AndAlso(expression, idsOrExpression);
                }
            }

            if (!search.UserOrAnonymousIds.IsNullOrEmpty())
            {
                var idsOrExpression = search.UserOrAnonymousIds.Distinct().Select(userOrAnonymousId => Expression.Equal(
                                                                                      Expression.Property(parameter, typeof(Buyer).GetProperty(nameof(Buyer.UserOrAnonymousId))),
                                                                                      Expression.Constant(userOrAnonymousId, typeof(string))))
                                      .Aggregate((expr1, expr2) => Expression.OrElse(expr1, expr2));

                if (expression is null)
                {
                    expression = idsOrExpression;
                }
                else
                {
                    expression = Expression.AndAlso(expression, idsOrExpression);
                }
            }

            if (expression != null)
            {
                _logger.LogInformation(expression.ToString());
                spec.Query.Where(Expression.Lambda <Func <Buyer, bool> >(expression, parameter));
            }

            var items = _mapper.Map <IEnumerable <BuyerDto> >(await _unitOfWork.BuyerRepository.ListAsync(spec));

            if (page.HasValue)
            {
                var totalCount = 0;

                if (expression is null)
                {
                    totalCount = await _unitOfWork.BuyerRepository.CountAsync();
                }
                else
                {
                    var noPagingSpec = new BuyerFilterSpec();
                    spec.Query.Where(Expression.Lambda <Func <Buyer, bool> >(expression, parameter));

                    totalCount = await _unitOfWork.BuyerRepository.CountAsync(noPagingSpec);
                }

                var paginatedList = new Page <BuyerDto>(items, totalCount, page.Value, pageSize);

                return(Ok(paginatedList));
            }

            return(Ok(items));
        }
示例#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))));
        }