Esempio n. 1
0
        private void AppendFilterOptionsToModel(ProductsByBrand model, IQueryable <Product> query)
        {
            model.FilterOption.Price.MaxPrice = query.Max(x => x.Price);
            model.FilterOption.Price.MinPrice = query.Min(x => x.Price);

            var getCategoryName = _contentLocalizationService.GetLocalizationFunction <Category>();

            model.FilterOption.Categories = query
                                            .SelectMany(x => x.Categories).Where(x => x.Category.IsPublished)
                                            .GroupBy(x => new {
                x.Category.Id,
                x.Category.Name,
                x.Category.Slug,
                x.Category.ParentId
            })
                                            .Select(g => new FilterCategory
            {
                Id       = (int)g.Key.Id,
                Name     = getCategoryName(g.Key.Id, nameof(g.Key.Name), g.Key.Name),
                Slug     = g.Key.Slug,
                ParentId = g.Key.ParentId,
                Count    = g.Count()
            })
                                            .ToList();
        }
 private static void AppendFilterOptionsToModel(ProductsByBrand model, IQueryable <Product> query)
 {
     model.FilterOption.Categories = query
                                     .SelectMany(x => x.Categories).Where(x => x.Category.Parent == null)
                                     .GroupBy(x => new {
         x.Category.Id,
         x.Category.Name,
         x.Category.SeoTitle
     })
                                     .Select(g => new FilterCategory
     {
         Id       = (int)g.Key.Id,
         Name     = g.Key.Name,
         SeoTitle = g.Key.SeoTitle,
         Count    = g.Count()
     }).ToList();
 }
Esempio n. 3
0
        public IActionResult BrandDetail(long id, SearchOption searchOption)
        {
            var brand = _brandRepository.Query().FirstOrDefault(x => x.Id == id);

            var model = new ProductsByBrand
            {
                BrandId             = id,
                BrandName           = brand.Name,
                BrandSlug           = brand.Slug,
                CurrentSearchOption = searchOption,
                FilterOption        = new FilterOption()
            };

            var query = _productRepository.Query().Where(x => x.BrandId == id && x.IsPublished);

            if (!query.Any())
            {
                model.TotalProduct = 0;
                return(View(model));
            }

            // todo: append filter option to model
            return(null);
        }
Esempio n. 4
0
        public IActionResult BrandDetail(long id, SearchOption searchOption)
        {
            var brand = _brandRepository.Query().FirstOrDefault(x => x.Id == id);

            if (brand == null)
            {
                return(Redirect("~/Error/FindNotFound"));
            }

            var model = new ProductsByBrand
            {
                BrandId             = id,
                BrandName           = brand.Name,
                BrandSlug           = brand.Slug,
                CurrentSearchOption = searchOption,
                FilterOption        = new FilterOption()
            };

            var query = _productRepository.Query().Where(x => x.BrandId == id && x.IsPublished && x.IsVisibleIndividually);

            if (query.Count() == 0)
            {
                model.TotalProduct = 0;
                return(View(model));
            }

            AppendFilterOptionsToModel(model, query);

            if (searchOption.MinPrice.HasValue)
            {
                query = query.Where(x => x.Price >= searchOption.MinPrice.Value);
            }

            if (searchOption.MaxPrice.HasValue)
            {
                query = query.Where(x => x.Price <= searchOption.MaxPrice.Value);
            }

            var categories = searchOption.GetCategories().ToArray();

            if (categories.Any())
            {
                query = query.Where(x => x.Categories.Any(c => categories.Contains(c.Category.Slug)));
            }

            model.TotalProduct = query.Count();
            var currentPageNum = searchOption.Page <= 0 ? 1 : searchOption.Page;
            var offset         = (_pageSize * currentPageNum) - _pageSize;

            while (currentPageNum > 1 && offset >= model.TotalProduct)
            {
                currentPageNum--;
                offset = (_pageSize * currentPageNum) - _pageSize;
            }

            query = ApplySort(searchOption, query);

            var products = query
                           .Include(x => x.ThumbnailImage)
                           .Skip(offset)
                           .Take(_pageSize)
                           .Select(x => ProductThumbnail.FromProduct(x))
                           .ToList();

            foreach (var product in products)
            {
                product.Name                   = _contentLocalizationService.GetLocalizedProperty(nameof(Product), product.Id, nameof(product.Name), product.Name);
                product.ThumbnailUrl           = _mediaService.GetThumbnailUrl(product.ThumbnailImage);
                product.CalculatedProductPrice = _productPricingService.CalculateProductPrice(product);
            }

            model.Products = products;
            model.CurrentSearchOption.PageSize = _pageSize;
            model.CurrentSearchOption.Page     = currentPageNum;

            return(View(model));
        }
        public IActionResult BrandDetail(long id, SearchOption searchOption)
        {
            var brand = _brandRepository.Query().FirstOrDefault(x => x.Id == id);

            var model = new ProductsByBrand
            {
                BrandId             = id,
                BrandName           = brand.Name,
                BrandSeoTitle       = brand.SeoTitle,
                CurrentSearchOption = searchOption,
                FilterOption        = new FilterOption()
            };

            var query = _productRepository.Query().Where(x => x.BrandId == id && x.IsPublished && x.IsVisibleIndividually);

            model.FilterOption.Price.MaxPrice = query.Max(x => x.Price);
            model.FilterOption.Price.MinPrice = query.Min(x => x.Price);

            if (searchOption.MinPrice.HasValue)
            {
                query = query.Where(x => x.Price >= searchOption.MinPrice.Value);
            }

            if (searchOption.MaxPrice.HasValue)
            {
                query = query.Where(x => x.Price <= searchOption.MaxPrice.Value);
            }

            AppendFilterOptionsToModel(model, query);
            var categories = searchOption.GetCategories();

            if (categories.Any())
            {
                var categoryIds = _categoryRepository.Query().Where(x => categories.Contains(x.SeoTitle)).Select(x => x.Id).ToList();
                query = query.Where(x => x.Categories.Any(c => categoryIds.Contains(c.CategoryId)));
            }

            model.TotalProduct = query.Count();
            var currentPageNum = searchOption.Page <= 0 ? 1 : searchOption.Page;
            var offset         = (PageSize * currentPageNum) - PageSize;

            while (currentPageNum > 1 && offset >= model.TotalProduct)
            {
                currentPageNum--;
                offset = (PageSize * currentPageNum) - PageSize;
            }

            query = query
                    .Include(x => x.ThumbnailImage);

            query = AppySort(searchOption, query);

            var products = query
                           .Select(x => new ProductThumbnail
            {
                Id              = x.Id,
                Name            = x.Name,
                SeoTitle        = x.SeoTitle,
                Price           = x.Price,
                OldPrice        = x.OldPrice,
                ThumbnailImage  = x.ThumbnailImage,
                NumberVariation = x.ProductLinks.Count,
                ReviewsCount    = x.ReviewsCount,
                RatingAverage   = x.RatingAverage
            })
                           .Skip(offset)
                           .Take(PageSize)
                           .ToList();

            foreach (var product in products)
            {
                product.ThumbnailUrl = _mediaService.GetThumbnailUrl(product.ThumbnailImage);
            }

            model.Products = products;
            model.CurrentSearchOption.PageSize = PageSize;
            model.CurrentSearchOption.Page     = currentPageNum;

            return(View(model));
        }