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
Sidebar compare products - prepare new action / endpoint / request handler
KrzysztofPajakea998539d00d
diff --git a/src/Web/Grand.Web/Controllers/ProductController.cs b/src/Web/Grand.Web/Controllers/ProductController.csindex 68e6188c5..9ff3c51f8 100644--- a/src/Web/Grand.Web/Controllers/ProductController.cs+++ b/src/Web/Grand.Web/Controllers/ProductController.cs@@ -20,6 +20,7 @@ using Grand.Web.Commands.Models.Products;using Grand.Web.Common.Filters;using Grand.Web.Common.Security.Captcha;using Grand.Web.Events;+using Grand.Web.Features.Models.Catalog;using Grand.Web.Features.Models.Products;using Grand.Web.Models.Catalog;using MediatR;@@ -782,54 +783,26 @@ namespace Grand.Web.Controllers#region Comparing products- /// <summary>- /// Gets a "compare products" identifier list- /// </summary>- /// <returns>"compare products" identifier list</returns>- protected virtual List<string> GetComparedProductIds()+ public virtual async Task<IActionResult> SidebarCompareProducts([FromServices] MediaSettings mediaSettings){- //try to get cookie- if (!HttpContext.Request.Cookies.TryGetValue(CacheKey.PRODUCTS_COMPARE_COOKIE_NAME, out var productIdsCookie) || string.IsNullOrEmpty(productIdsCookie))- return new List<string>();+ if (!_catalogSettings.CompareProductsEnabled)+ return Content("");- //get array of string product identifiers from cookie- var productIds = productIdsCookie.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);+ var model = await _mediator.Send(new GetCompareProducts() { PictureProductThumbSize = mediaSettings.MiniCartThumbPictureSize });++ return View(model);- //return list of int product identifiers- return productIds.Select(productId => productId).Distinct().Take(10).ToList();}- public virtual async Task<IActionResult> CompareProducts()++ public virtual async Task<IActionResult> CompareProducts([FromServices] MediaSettings mediaSettings){if (!_catalogSettings.CompareProductsEnabled)return RedirectToRoute("HomePage");- var model = new CompareProductsModel {- IncludeShortDescriptionInCompareProducts = _catalogSettings.IncludeShortDescriptionInCompareProducts,- IncludeFullDescriptionInCompareProducts = _catalogSettings.IncludeFullDescriptionInCompareProducts,- };-- var products = new List<Product>();- var productIds = GetComparedProductIds();- foreach (var id in productIds)- {- var product = await _productService.GetProductById(id);- if (product != null && product.Published)- products.Add(product);- }-- //ACL and store acl- products = products.Where(p => _aclService.Authorize(p, _workContext.CurrentCustomer) && _aclService.Authorize(p, _workContext.CurrentStore.Id)).ToList();- //availability dates- products = products.Where(p => p.IsAvailable()).ToList();-- (await _mediator.Send(new GetProductOverview() {- PrepareSpecificationAttributes = true,- Products = products,- })).ToList().ForEach(model.Products.Add);+ var model = await _mediator.Send(new GetCompareProducts() { PictureProductThumbSize = mediaSettings.CartThumbPictureSize });return View(model);-}#endregiondiff --git a/src/Web/Grand.Web/Endpoints/EndpointProvider.cs b/src/Web/Grand.Web/Endpoints/EndpointProvider.csindex 6f32c6983..34065f149 100644--- a/src/Web/Grand.Web/Endpoints/EndpointProvider.cs+++ b/src/Web/Grand.Web/Endpoints/EndpointProvider.cs@@ -277,6 +277,11 @@ namespace Grand.Web.Endpointspattern + "newproducts/",new { controller = "Product", action = "NewProducts" });+ //sidebar compare products+ endpointRouteBuilder.MapControllerRoute("SidebarCompareProducts",+ pattern + "sidebarcompareproducts/",+ new { controller = "Product", action = "SidebarCompareProducts" });+//compare productsendpointRouteBuilder.MapControllerRoute("CompareProducts",pattern + "compareproducts/",diff --git a/src/Web/Grand.Web/Features/Handlers/Catalog/GetCompareProductsHandler.cs b/src/Web/Grand.Web/Features/Handlers/Catalog/GetCompareProductsHandler.csnew file mode 100644index 000000000..061bd890e--- /dev/null+++ b/src/Web/Grand.Web/Features/Handlers/Catalog/GetCompareProductsHandler.cs@@ -0,0 +1,82 @@+using Grand.Business.Catalog.Interfaces.Products;+using Grand.Business.Common.Interfaces.Security;+using Grand.Domain.Catalog;+using Grand.Infrastructure;+using Grand.Infrastructure.Caching.Constants;+using Grand.Web.Features.Models.Catalog;+using Grand.Web.Features.Models.Products;+using Grand.Web.Models.Catalog;+using MediatR;+using Microsoft.AspNetCore.Http;++namespace Grand.Web.Features.Handlers.Catalog+{+ public class GetCompareProductsHandler : IRequestHandler<GetCompareProducts, CompareProductsModel>+ {+ private readonly IProductService _productService;+ private readonly IWorkContext _workContext;+ private readonly IAclService _aclService;+ private readonly IMediator _mediator;+ private readonly IHttpContextAccessor _httpContextAccessor;+ private readonly CatalogSettings _catalogSettings;++ public GetCompareProductsHandler(+ IProductService productService,+ IWorkContext workContext,+ IAclService aclService,+ IMediator mediator,+ IHttpContextAccessor httpContextAccessor,+ CatalogSettings catalogSettings)+ {+ _productService = productService;+ _workContext = workContext;+ _aclService = aclService;+ _mediator = mediator;+ _httpContextAccessor = httpContextAccessor;+ _catalogSettings = catalogSettings;+ }++ public async Task<CompareProductsModel> Handle(GetCompareProducts request, CancellationToken cancellationToken)+ {+ var model = new CompareProductsModel {+ IncludeShortDescriptionInCompareProducts = _catalogSettings.IncludeShortDescriptionInCompareProducts,+ IncludeFullDescriptionInCompareProducts = _catalogSettings.IncludeFullDescriptionInCompareProducts,+ };++ var products = new List<Product>();+ var productIds = GetComparedProductIds();+ foreach (var id in productIds)+ {+ var product = await _productService.GetProductById(id);+ if (product != null && product.Published)+ products.Add(product);+ }++ //ACL and store acl+ products = products.Where(p => _aclService.Authorize(p, _workContext.CurrentCustomer) && _aclService.Authorize(p, _workContext.CurrentStore.Id)).ToList();+ //availability dates+ products = products.Where(p => p.IsAvailable()).ToList();++ (await _mediator.Send(new GetProductOverview() {+ PrepareSpecificationAttributes = true,+ Products = products,+ ProductThumbPictureSize = request.PictureProductThumbSize+ })).ToList().ForEach(model.Products.Add);++ return model;+ }++ protected virtual List<string> GetComparedProductIds()+ {+ //try to get cookie+ if (!_httpContextAccessor.HttpContext.Request.Cookies.TryGetValue(CacheKey.PRODUCTS_COMPARE_COOKIE_NAME, out var productIdsCookie) || string.IsNullOrEmpty(productIdsCookie))+ return new List<string>();++ //get array of string product identifiers from cookie+ var productIds = productIdsCookie.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);++ //return list of int product identifiers+ return productIds.Select(productId => productId).Distinct().Take(10).ToList();+ }+ }+}diff --git a/src/Web/Grand.Web/Features/Models/Catalog/GetCompareProducts.cs b/src/Web/Grand.Web/Features/Models/Catalog/GetCompareProducts.csnew file mode 100644index 000000000..5c9baf857--- /dev/null+++ b/src/Web/Grand.Web/Features/Models/Catalog/GetCompareProducts.cs@@ -0,0 +1,11 @@+using Grand.Domain.Customers;+using Grand.Web.Models.Catalog;+using MediatR;++namespace Grand.Web.Features.Models.Catalog+{+ public class GetCompareProducts : IRequest<CompareProductsModel>+ {+ public int? PictureProductThumbSize { get; set; }+ }+}diff --git a/src/Web/Grand.Web/Views/Shared/SidebarCompareProducts.cshtml b/src/Web/Grand.Web/Views/Shared/SidebarCompareProducts.cshtmlindex 9f2959936..02d1b550c 100644--- a/src/Web/Grand.Web/Views/Shared/SidebarCompareProducts.cshtml+++ b/src/Web/Grand.Web/Views/Shared/SidebarCompareProducts.cshtml@@ -6,7 +6,7 @@<ul class="px-0"><li class="nav-item"><a href="#" target="_self" class="nav-link">- <b-icon icon="shuffle" :variant="[darkMode ? 'white' : 'dark']" @@click="getCompareList('@Url.RouteUrl("CompareProducts")')" v-b-toggle.sidebar-compare></b-icon>+ <b-icon icon="shuffle" :variant="[darkMode ? 'white' : 'dark']" @@click="getCompareList('@Url.RouteUrl("SidebarCompareProducts")')" v-b-toggle.sidebar-compare></b-icon><span class="qty-indicator">{{compareProductsQty}}</span>