コード例 #1
0
        /// <summary>
        /// Gets a cart by specifing a condition
        /// </summary>
        /// <param name="selector">Cart item condition</param>
        /// <returns>A cart</returns>
        private List <CartItem> GetCartItems(Expression <Func <CartItem, bool> > selector)
        {
            var cart             = _cartItemRepository.GetCartItems().Where(selector).ToList();
            var products         = _productRepository.GetAllProducts().Where(p => cart.Select(ci => ci.ProductId).Contains(p.Id));
            var ratings          = _productRatingRepository.GetRatings().Where(pr => cart.Select(ci => ci.ProductId).Contains(pr.ProductId));
            var completeProducts = products.Join(ratings, p => p.Id, r => r.ProductId, (p, r) => new { product = p, rating = r }).Select(x => new ProductBO(x.product, x.rating, null));
            var completeCart     = cart.Join(completeProducts, ci => ci.ProductId, p => p.Id, (ci, p) => { ci.Product = p; return(ci); }).ToList();

            return(completeCart);
        }
コード例 #2
0
        /// <summary>
        /// Gets a page of product with applied filtering. The filtering occurs on the server side.
        /// </summary>
        /// <param name="parameters">Filter parameters</param>
        /// <returns>Page of products</returns>
        public AlzaAdminDTO <QueryResultWrapper> GetPage(QueryParametersWrapper parameters)
        {
            try
            {
                //get all children of the specified category
                var childCategoriesId = _categoryRelationshipRepository.GetAllRelationships().Where(c => c.Id == parameters.CategoryId).Select(c => c.ChildId);

                //get all products
                IQueryable <ProductBase> query = _productRepository.GetAllProducts();

                //return only products which belong in these categories
                query = query.Where(p => childCategoriesId.Contains(p.CategoryId));

                decimal             minPrice   = decimal.MaxValue;
                decimal             maxPrice   = 0;
                QueryResultWrapper  result     = new QueryResultWrapper();
                HashSet <Language>  languages  = new HashSet <Language>();
                HashSet <Publisher> publishers = new HashSet <Publisher>();
                HashSet <Format>    formats    = new HashSet <Format>();
                HashSet <Author>    authors    = new HashSet <Author>();

                //specifying filter options correspondingly
                foreach (ProductBase product in query)
                {
                    minPrice = product.Price < minPrice ? product.Price : minPrice;
                    maxPrice = product.Price > maxPrice ? product.Price : maxPrice;
                    languages.Add(product.Language);
                    publishers.Add(product.Publisher);
                    formats.Add(product.Format);
                    foreach (Author author in product.Book.AuthorsBooks.Select(ab => ab.Author))
                    {
                        authors.Add(author);
                    }
                }

                result.MinPrice = minPrice;
                result.MaxPrice = maxPrice;

                result.Authors = authors.OrderBy(a => a.Surname).ToList();

                result.Languages  = languages.OrderBy(l => l.Name).ToList();
                result.Publishers = publishers.OrderBy(p => p.Name).ToList();
                result.Formats    = formats.OrderBy(f => f.Name).ToList();

                //filtering
                if (parameters.MinPrice != null)
                {
                    query = query.Where(p => p.Price >= parameters.MinPrice);
                }
                if (parameters.MaxPrice != null)
                {
                    query = query.Where(p => p.Price <= parameters.MaxPrice);
                }
                if (parameters.Languages != null)
                {
                    query = query.Where(p => parameters.Languages.Contains(p.LanguageId));
                }
                if (parameters.Publishers != null)
                {
                    query = query.Where(p => parameters.Publishers.Contains(p.PublisherId));
                }
                if (parameters.Formats != null)
                {
                    query = query.Where(p => parameters.Formats.Contains(p.FormatId));
                }
                if (parameters.Authors != null)
                {
                    query = query.Where(p => p.Book.AuthorsBooks.Select(ab => ab.Author).Select(a => a.AuthorId).Intersect(parameters.Authors).Count() > 0);
                }


                //Add average ratings
                List <int> pIds = query.Select(p => p.Id).ToList();
                IQueryable <ProductRating> ratings = _productRatingRepository.GetRatings().Where(pr => pIds.Contains(pr.ProductId));
                var products = query.Join(ratings, q => q.Id, r => r.ProductId, (p, r) => new { product = p, rating = r }).Select(x => new ProductBO(x.product, x.rating, null));



                //sort
                Func <ProductBO, IComparable> sortingParameter;
                switch (parameters.SortingParameter)
                {
                case Enums.SortingParameter.Price:
                    sortingParameter = p => p.Price;
                    break;

                case Enums.SortingParameter.Rating:
                    sortingParameter = p => p.AverageRating;
                    break;

                case Enums.SortingParameter.Date:
                    sortingParameter = p => p.DateAdded;
                    break;

                case Enums.SortingParameter.Name:
                    sortingParameter = p => p.Name;
                    break;

                default:
                    sortingParameter = p => p.AverageRating;
                    break;
                }
                switch (parameters.SortingType)
                {
                case Enums.SortType.Asc:
                    products = products.OrderBy(sortingParameter).AsQueryable();
                    break;

                case Enums.SortType.Desc:
                    products = products.OrderByDescending(sortingParameter).AsQueryable();
                    break;

                default:
                    break;
                }

                //return a "page"
                result.ResultCount = products.Count();
                products           = products.Skip((parameters.PageNum - 1) * parameters.PageSize).Take(parameters.PageSize);
                result.Products    = products.ToList();
                return(AlzaAdminDTO <QueryResultWrapper> .Data(result));
            }
            catch (Exception e)
            {
                return(AlzaAdminDTO <QueryResultWrapper> .Error(e.Message + Environment.NewLine + e.StackTrace));
            }
        }