public IActionResult CategoryDetail(long id, SearchOption searchOption) { var category = _categoryRepository.Query().FirstOrDefault(x => x.Id == id); if (category == null) { return(Redirect("~/Error/FindNotFound")); } var model = new ProductsByCategory { CategoryId = category.Id, ParentCategorId = category.ParentId, CategoryName = category.Name, CategorySlug = category.Slug, CategoryMetaTitle = category.MetaTitle, CategoryMetaKeywords = category.MetaKeywords, CategoryMetaDescription = category.MetaDescription, CurrentSearchOption = searchOption, FilterOption = new FilterOption() }; var query = _productRepository .Query() .Where(x => x.Categories.Any(c => c.CategoryId == category.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 brands = searchOption.GetBrands(); if (brands.Any()) { var brandIs = _brandRepository.Query().Where(x => brands.Contains(x.Slug)).Select(x => x.Id).ToList(); query = query.Where(x => x.BrandId.HasValue && brandIs.Contains(x.BrandId.Value)); } 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.Brand) .Include(x => x.ThumbnailImage); query = AppySort(searchOption, query); var products = query .Select(x => ProductThumbnail.FromProduct(x)) .Skip(offset) .Take(_pageSize) .ToList(); foreach (var product in products) { 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 CategoryDetail(long id, SearchOption searchOption) { var category = _categoryRepository.Query().FirstOrDefault(x => x.Id == id); if (category == null) { return(Redirect("~/Error/FindNotFound")); } var model = new ProductsByCategory { CategoryId = category.Id, ParentCategorId = category.ParentId, CategoryName = category.Name, CategorySeoTitle = category.SeoTitle, CurrentSearchOption = searchOption, FilterOption = new FilterOption() }; var query = _productRepository .Query() .Where(x => x.Categories.Any(c => c.CategoryId == category.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 brands = searchOption.GetBrands(); if (brands.Any()) { var brandIs = _brandRepository.Query().Where(x => brands.Contains(x.SeoTitle)).Select(x => x.Id).ToList(); query = query.Where(x => x.BrandId.HasValue && brandIs.Contains(x.BrandId.Value)); } 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.Brand) .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)); }
public IActionResult Index(SearchOption searchOption) { if (string.IsNullOrWhiteSpace(searchOption.Query)) { return(Redirect("~/")); } var brand = _brandRepository.Query().FirstOrDefault(x => x.Name == searchOption.Query && x.IsPublished); if (brand != null) { return(Redirect(string.Format("~/{0}", brand.SeoTitle))); } var model = new SearchResult { CurrentSearchOption = searchOption, FilterOption = new FilterOption() }; var query = _productRepository.Query().Where(x => x.Name.Contains(searchOption.Query) && 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); if (string.Compare(model.CurrentSearchOption.Category, "all", StringComparison.OrdinalIgnoreCase) != 0) { 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))); } } var brands = searchOption.GetBrands(); if (brands.Any()) { var brandIs = _brandRepository.Query().Where(x => brands.Contains(x.SeoTitle)).Select(x => x.Id).ToList(); query = query.Where(x => x.BrandId.HasValue && brandIs.Contains(x.BrandId.Value)); } model.TotalProduct = query.Count(); SaveSearchQuery(searchOption, model); 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 }).ToList(); foreach (var product in products) { product.ThumbnailUrl = _mediaService.GetThumbnailUrl(product.ThumbnailImage); } model.Products = products; return(View(model)); }
public IActionResult Index(SearchOption searchOption) { if (string.IsNullOrWhiteSpace(searchOption.Query)) { return(Redirect("~/")); } var brand = _brandRepository.Query().FirstOrDefault(x => x.Name == searchOption.Query && x.IsPublished); if (brand != null) { return(Redirect(string.Format("~/{0}", brand.Slug))); } var model = new SearchResult { CurrentSearchOption = searchOption, FilterOption = new FilterOption() }; var query = _productRepository.Query().Where(x => x.Name.Contains(searchOption.Query) && 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); } if (string.Compare(model.CurrentSearchOption.Category, "all", StringComparison.OrdinalIgnoreCase) != 0) { var categories = searchOption.GetCategories(); if (categories.Any()) { var categoryIds = _categoryRepository.Query().Where(x => categories.Contains(x.Slug)).Select(x => x.Id).ToList(); query = query.Where(x => x.Categories.Any(c => categoryIds.Contains(c.CategoryId))); } } var brands = searchOption.GetBrands(); if (brands.Any()) { var brandIs = _brandRepository.Query().Where(x => brands.Contains(x.Slug)).Select(x => x.Id).ToList(); query = query.Where(x => x.BrandId.HasValue && brandIs.Contains(x.BrandId.Value)); } 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; } SaveSearchQuery(searchOption, model); query = query .Include(x => x.ThumbnailImage); query = AppySort(searchOption, query); var products = query .Select(x => ProductThumbnail.FromProduct(x)) .Skip(offset) .Take(_pageSize) .ToList(); foreach (var product in products) { 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 ProductsByCategory(string catSeoTitle, SearchOption searchOption) { var category = categoryRepository.Query().FirstOrDefault(x => x.SeoTitle == catSeoTitle); if (category == null) { return(Redirect("~/Error/FindNotFound")); } var model = new ProductsByCategory { CategoryId = category.Id, ParentCategorId = category.ParentId, CategoryName = category.Name, CategorySeoTitle = category.SeoTitle, CurrentSearchOption = searchOption, FilterOption = new FilterOption() }; var query = productCategoryRepository .Query() .Where(x => x.CategoryId == category.Id && x.Product.IsPublished) .Select(x => x.Product); 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 brands = searchOption.GetBrands(); if (brands.Any()) { var brandIs = brandRepository.Query().Where(x => brands.Contains(x.SeoTitle)).Select(x => x.Id).ToList(); query = query.Where(x => x.BrandId.HasValue && brandIs.Contains(x.BrandId.Value)); } model.TotalProduct = query.Count(); query = query .Include(x => x.Brand) .Include(x => x.ThumbnailImage) .Include(x => x.Variations); query = AppySort(searchOption, query); var products = query .Select(x => new ProductListItem { Id = x.Id, Name = x.Name, SeoTitle = x.SeoTitle, Price = x.Price, OldPrice = x.OldPrice, ThumbnailImage = x.ThumbnailImage }).ToList(); foreach (var product in products) { product.ThumbnailUrl = mediaService.GetThumbnailUrl(product.ThumbnailImage); } model.Products = products; return(View(model)); }