示例#1
0
 private List <Product> FilterCategory(ProductQueryContract queryContract, IEnumerable <Product> products)
 {
     if (queryContract.CategoryId.HasValue)
     {
         products = from p in products where p.Category.Id == queryContract.CategoryId.Value || GetParentCollectionOfIdValues(p).Contains(queryContract.CategoryId.Value) select p;
     }
     return(products.ToList());
 }
示例#2
0
 private static List <Product> FilterManufacturer(ProductQueryContract queryContract, IEnumerable <Product> products)
 {
     if (queryContract.ManufacturerId.HasValue)
     {
         products = products.Where(p => p.ManufacturerId == queryContract.ManufacturerId.Value);
     }
     return(products.ToList());
 }
示例#3
0
        private static List <Product> FilterName(ProductQueryContract queryContract, IEnumerable <Product> products)
        {
            if (!string.IsNullOrWhiteSpace(queryContract.Name))
            {
                products = products.Where(p => p.Name != null && p.Name.ToLower().Contains(queryContract.Name.ToLower()));
            }

            return(products.ToList());
        }
示例#4
0
        private static List <Product> FilterMinPrice(ProductQueryContract queryContract, IEnumerable <Product> products)
        {
            if (queryContract.MinPrice.HasValue)
            {
                products = products.Where(p => (p.PriceCurrent.HasValue && p.PriceCurrent >= queryContract.MinPrice) || (!p.PriceCurrent.HasValue && p.PriceRegular >= queryContract.MinPrice));
            }

            return(products.ToList());
        }
示例#5
0
        private List <Product> FilterSpecialQueries(ProductQueryContract queryContract, IEnumerable <Product> products)
        {
            if (queryContract.OnSale)
            {
                products =
                    products.Where(p => p.PriceCurrent.HasValue && p.PriceCurrent < p.PriceRegular && p.PriceRegular > 0);
            }

            return(products.ToList());
        }
示例#6
0
        private List <Product> FilterTagValues(ProductQueryContract queryContract, IEnumerable <Product> products)
        {
            if (queryContract.TagFilters.Any())
            {
                foreach (var tagFilter in queryContract.TagFilters)
                {
                    products = products.Where(p => p.ProductTagValues.Any(tv => tv.TagTypeId == tagFilter.TagTypeId && tv.Value.Contains(tagFilter.FilterValue)));
                }
            }

            return(products.ToList());
        }
示例#7
0
        /// <summary>
        /// Query products.
        /// </summary>
        /// <param name="queryContract"></param>
        /// <returns></returns>
        public ActionResult <IEnumerable <Product> > Query(ProductQueryContract queryContract)
        {
            try
            {
                var products = _unitOfWork
                               .ProductRepository
                               .Query()
                               .OnlyActive(queryContract.IgnoreStatus)
                               .ToList();

                products = FilterName(queryContract, products);

                products = FilterCategory(queryContract, products);

                products = FilterManufacturer(queryContract, products);

                products = FilterMinPrice(queryContract, products);

                products = FitlerMaxPrice(queryContract, products);

                products = FilterTagValues(queryContract, products);

                products = FilterSpecialQueries(queryContract, products);

                products = Sort(queryContract, products);

                var enumeratedProducts = products as IList <Product> ?? products.ToList();
                var count = enumeratedProducts.Count;

                return(new ActionResult <IEnumerable <Product> >
                {
                    Data = enumeratedProducts,
                    Total = count,
                    Success = true
                });
            }
            catch (Exception ex)
            {
                return(new ActionResult <IEnumerable <Product> >
                {
                    Data = null,
                    Success = false,
                    Message = ex.Message
                });
            }
        }
示例#8
0
        private List <Product> Sort(ProductQueryContract queryContract, IEnumerable <Product> products)
        {
            switch (queryContract.Sort)
            {
            case SortOptions.NameAscending:
                return(products.OrderBy(p => p.Name).ToList());

            case SortOptions.NameDescending:
                return(products.OrderByDescending(p => p.Name).ToList());

            case SortOptions.PriceAscending:
                return(products.OrderBy(p => p.PriceCurrent != null && p.PriceCurrent != 0 ? p.PriceCurrent : p.PriceRegular).ToList());

            case SortOptions.PriceDescending:
                return(products.OrderByDescending(p => p.PriceCurrent != null && p.PriceCurrent != 0 ? p.PriceCurrent : p.PriceRegular).ToList());

            default:
                return(products.ToList());
            }
        }