Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.
C# aspnet-corecsharpdockerdotnetecommerceecommerce-platformmongodbmulti-storemulti-vendornosqlonline-shopshoppingcart
GetSearchProductsQuery - allow filtering by rating
supportddcd1b060822
diff --git a/src/Business/Grand.Business.Catalog/Queries/Handlers/GetSearchProductsQueryHandler.cs b/src/Business/Grand.Business.Catalog/Queries/Handlers/GetSearchProductsQueryHandler.csindex 39808069a..973ab4f60 100644--- a/src/Business/Grand.Business.Catalog/Queries/Handlers/GetSearchProductsQueryHandler.cs+++ b/src/Business/Grand.Business.Catalog/Queries/Handlers/GetSearchProductsQueryHandler.cs@@ -45,6 +45,77 @@ namespace Grand.Business.Catalog.Queries.Handlersvar query = from p in _productRepository.Tableselect p;+ query = FilterQueryable(request, query, allowedCustomerGroupsIds);++ var querySpecification = query;++ //search by specs+ if (request.FilteredSpecs != null && request.FilteredSpecs.Any())+ {+ var spec = new HashSet<string>();+ Dictionary<string, List<string>> dictionary = new Dictionary<string, List<string>>();+ foreach (var key in request.FilteredSpecs)+ {+ var specification = await _specificationAttributeService.GetSpecificationAttributeByOptionId(key);+ if (specification == null) continue;+ spec.Add(specification.Id);+ if (!dictionary.ContainsKey(specification.Id))+ {+ //add+ dictionary.Add(specification.Id, new List<string>());+ querySpecification = querySpecification.Where(x =>+ x.ProductSpecificationAttributes.Any(y =>+ y.SpecificationAttributeId == specification.Id && y.AllowFiltering));+ }++ dictionary[specification.Id].Add(key);+ }++ foreach (var item in dictionary)+ {+ query = query.Where(x => x.ProductSpecificationAttributes.Any(y =>+ y.SpecificationAttributeId == item.Key && y.AllowFiltering+ && item.Value.Contains(+ y.SpecificationAttributeOptionId)));+ }+ }++ if (request.SpecificationOptions != null && request.SpecificationOptions.Any())+ {+ query = query.Where(x => x.ProductSpecificationAttributes.Any(y =>+ request.SpecificationOptions.Contains(y.SpecificationAttributeOptionId)));+ }++ query = OrderByQueryable(request, query);++ var products = await PagedList<Product>.Create(query, request.PageIndex, request.PageSize);++ if (!request.LoadFilterableSpecificationAttributeOptionIds ||+ _catalogSettings.IgnoreFilterableSpecAttributeOption)+ return (products, filterableSpecificationAttributeOptionIds);+ {+ var filterSpecExists =+ querySpecification.Where(x => x.ProductSpecificationAttributes.Any(x => x.AllowFiltering));++ var spec = from p in filterSpecExists+ from item in p.ProductSpecificationAttributes+ select item;++ var groupQuerySpec = spec.Where(x => x.AllowFiltering).GroupBy(x =>+ new { SpecificationAttributeOptionId = x.SpecificationAttributeOptionId }).ToList();+ IList<string> specification =+ groupQuerySpec.Select(item => item.Key.SpecificationAttributeOptionId).ToList();++ filterableSpecificationAttributeOptionIds = specification.ToList();+ }++ return (products, filterableSpecificationAttributeOptionIds);++ #endregion+ }++ private IQueryable<Product> FilterQueryable(GetSearchProductsQuery request, IQueryable<Product> query, string[] allowedCustomerGroupsIds)+ {//category filteringif (request.CategoryIds != null && request.CategoryIds.Any()){@@ -107,8 +178,7 @@ namespace Grand.Business.Catalog.Queries.Handlersif (request.ProductType.HasValue){- var productTypeId = (int)request.ProductType.Value;- query = query.Where(p => p.ProductTypeId == (ProductType)productTypeId);+ query = query.Where(p => p.ProductTypeId == request.ProductType);}if (request.ShowOnHomePage.HasValue)@@ -116,8 +186,6 @@ namespace Grand.Business.Catalog.Queries.Handlersquery = query.Where(p => p.ShowOnHomePage == request.ShowOnHomePage.Value);}- //The function 'CurrentUtcDateTime' is not supported by SQL Server Compact.- //That's why we pass the date valuevar nowUtc = DateTime.UtcNow;if (request.PriceMin.HasValue){@@ -130,6 +198,12 @@ namespace Grand.Business.Catalog.Queries.Handlersquery = query.Where(p => p.Price <= request.PriceMax.Value);}+ if (request.Rating.HasValue)+ {+ //avg rating+ query = query.Where(p => p.AvgRating >= request.Rating);+ }+if (!request.ShowHidden && !_catalogSettings.IgnoreFilterableAvailableStartEndDateTime){query = query.Where(p =>@@ -209,45 +283,11 @@ namespace Grand.Business.Catalog.Queries.Handlersquery = query.Where(x => x.ProductTags.Any(y => y == request.ProductTag));}- var querySpecification = query;-- //search by specs- if (request.FilteredSpecs != null && request.FilteredSpecs.Any())- {- var spec = new HashSet<string>();- Dictionary<string, List<string>> dictionary = new Dictionary<string, List<string>>();- foreach (var key in request.FilteredSpecs)- {- var specification = await _specificationAttributeService.GetSpecificationAttributeByOptionId(key);- if (specification == null) continue;- spec.Add(specification.Id);- if (!dictionary.ContainsKey(specification.Id))- {- //add- dictionary.Add(specification.Id, new List<string>());- querySpecification = querySpecification.Where(x =>- x.ProductSpecificationAttributes.Any(y =>- y.SpecificationAttributeId == specification.Id && y.AllowFiltering));- }-- dictionary[specification.Id].Add(key);- }-- foreach (var item in dictionary)- {- query = query.Where(x => x.ProductSpecificationAttributes.Any(y =>- y.SpecificationAttributeId == item.Key && y.AllowFiltering- && item.Value.Contains(- y.SpecificationAttributeOptionId)));- }- }-- if (request.SpecificationOptions != null && request.SpecificationOptions.Any())- {- query = query.Where(x => x.ProductSpecificationAttributes.Any(y =>- request.SpecificationOptions.Contains(y.SpecificationAttributeOptionId)));- }+ return query;+ }+ private IQueryable<Product> OrderByQueryable(GetSearchProductsQuery request, IQueryable<Product> query)+ {switch (request.OrderBy){case ProductSortingEnum.Position when request.CategoryIds != null && request.CategoryIds.Any():@@ -332,35 +372,9 @@ namespace Grand.Business.Catalog.Queries.Handlers? query.OrderBy(x => x.LowStock).ThenByDescending(x => x.AvgRating): query.OrderByDescending(x => x.AvgRating).ThenByDescending(x => x.ApprovedTotalReviews);break;- default:- break;- }-- var products = await PagedList<Product>.Create(query, request.PageIndex, request.PageSize);--- if (!request.LoadFilterableSpecificationAttributeOptionIds ||- _catalogSettings.IgnoreFilterableSpecAttributeOption)- return (products, filterableSpecificationAttributeOptionIds);- {- var filterSpecExists =- querySpecification.Where(x => x.ProductSpecificationAttributes.Any(x => x.AllowFiltering));-- var qspec = from p in filterSpecExists- from item in p.ProductSpecificationAttributes- select item;-- var groupQuerySpec = qspec.Where(x => x.AllowFiltering).GroupBy(x =>- new { SpecificationAttributeOptionId = x.SpecificationAttributeOptionId }).ToList();- IList<string> specyfication =- groupQuerySpec.Select(item => item.Key.SpecificationAttributeOptionId).ToList();-- filterableSpecificationAttributeOptionIds = specyfication.ToList();}- return (products, filterableSpecificationAttributeOptionIds);-- #endregion+ return query;}}}\ No newline at end of filediff --git a/src/Business/Grand.Business.Core/Queries/Catalog/GetSearchProductsQuery.cs b/src/Business/Grand.Business.Core/Queries/Catalog/GetSearchProductsQuery.csindex 72629da0a..bf0a5d2c1 100644--- a/src/Business/Grand.Business.Core/Queries/Catalog/GetSearchProductsQuery.cs+++ b/src/Business/Grand.Business.Core/Queries/Catalog/GetSearchProductsQuery.cs@@ -12,29 +12,30 @@ namespace Grand.Business.Core.Queries.Catalogpublic bool LoadFilterableSpecificationAttributeOptionIds { get; set; } = false;public int PageIndex { get; set; } = 0;public int PageSize { get; set; } = int.MaxValue;- public IList<string> CategoryIds { get; set; } = null;+ public IList<string> CategoryIds { get; set; }public string BrandId { get; set; } = "";public string CollectionId { get; set; } = "";public string StoreId { get; set; } = "";public string VendorId { get; set; } = "";public string WarehouseId { get; set; } = "";- public ProductType? ProductType { get; set; } = null;- public bool VisibleIndividuallyOnly { get; set; } = false;- public bool MarkedAsNewOnly { get; set; } = false;- public bool? ShowOnHomePage { get; set; } = null;- public bool? FeaturedProducts { get; set; } = null;- public double? PriceMin { get; set; } = null;- public double? PriceMax { get; set; } = null;+ public ProductType? ProductType { get; set; }+ public bool VisibleIndividuallyOnly { get; set; }+ public bool MarkedAsNewOnly { get; set; }+ public bool? ShowOnHomePage { get; set; }+ public bool? FeaturedProducts { get; set; }+ public double? PriceMin { get; set; }+ public double? PriceMax { get; set; }+ public double? Rating { get; set; }public string ProductTag { get; set; } = "";- public string Keywords { get; set; } = null;- public bool SearchDescriptions { get; set; } = false;+ public string Keywords { get; set; }+ public bool SearchDescriptions { get; set; }public bool SearchSku { get; set; } = true;- public bool SearchProductTags { get; set; } = false;+ public bool SearchProductTags { get; set; }public string LanguageId { get; set; } = "";- public IList<string> FilteredSpecs { get; set; } = null;- public IList<string> SpecificationOptions { get; set; } = null;+ public IList<string> FilteredSpecs { get; set; }+ public IList<string> SpecificationOptions { get; set; }public ProductSortingEnum OrderBy { get; set; } = ProductSortingEnum.Position;- public bool ShowHidden { get; set; } = false;- public bool? OverridePublished { get; set; } = null;+ public bool ShowHidden { get; set; }+ public bool? OverridePublished { get; set; }}}