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
Add component ProductEmailAFriend
KrzysztofPajakfa219459a475
diff --git a/src/Web/Grand.Web/Components/ProductEmailAFriend.cs b/src/Web/Grand.Web/Components/ProductEmailAFriend.csnew file mode 100644index 000000000..2f5f98cf5--- /dev/null+++ b/src/Web/Grand.Web/Components/ProductEmailAFriend.cs@@ -0,0 +1,61 @@+using Grand.Business.Catalog.Interfaces.Products;+using Grand.Business.Common.Extensions;+using Grand.Domain.Catalog;+using Grand.Infrastructure;+using Grand.Web.Common.Components;+using Grand.Web.Common.Security.Captcha;+using Grand.Web.Models.Catalog;+using Microsoft.AspNetCore.Mvc;+using System.Threading.Tasks;++namespace Grand.Web.Components+{+ public class ProductEmailAFriendViewComponent : BaseViewComponent+ {+ #region Fields+ private readonly IProductService _productService;+ private readonly IWorkContext _workContext;+ private readonly CatalogSettings _catalogSettings;+ private readonly CaptchaSettings _captchaSettings;++ #endregion++ #region Constructors++ public ProductEmailAFriendViewComponent(+ IProductService productService,+ IWorkContext workContext,+ CatalogSettings catalogSettings,+ CaptchaSettings captchaSettings)+ {+ _productService = productService;+ _workContext = workContext;+ _catalogSettings = catalogSettings;+ _captchaSettings = captchaSettings;+ }++ #endregion++ #region Invoker++ public async Task<IViewComponentResult> InvokeAsync(string productId)+ {+ var product = await _productService.GetProductById(productId);+ if (product == null || !product.Published || !_catalogSettings.EmailAFriendEnabled)+ return Content("");++ var model = new ProductEmailAFriendModel();+ model.ProductId = product.Id;+ model.ProductName = product.GetTranslation(x => x.Name, _workContext.WorkingLanguage.Id);+ model.ProductSeName = product.GetSeName(_workContext.WorkingLanguage.Id);+ model.YourEmailAddress = _workContext.CurrentCustomer.Email;+ model.DisplayCaptcha = _captchaSettings.Enabled && _captchaSettings.ShowOnEmailProductToFriendPage;++ return View(model);++ }++ #endregion++ }+}diff --git a/src/Web/Grand.Web/Controllers/ProductController.cs b/src/Web/Grand.Web/Controllers/ProductController.csindex c96031869..b8a1bbb70 100644--- a/src/Web/Grand.Web/Controllers/ProductController.cs+++ b/src/Web/Grand.Web/Controllers/ProductController.cs@@ -649,21 +649,6 @@ namespace Grand.Web.Controllers#region Email a friend- public virtual async Task<IActionResult> ProductEmailAFriend(string productId)- {- var product = await _productService.GetProductById(productId);- if (product == null || !product.Published || !_catalogSettings.EmailAFriendEnabled)- return RedirectToRoute("HomePage");-- var model = new ProductEmailAFriendModel();- model.ProductId = product.Id;- model.ProductName = product.GetTranslation(x => x.Name, _workContext.WorkingLanguage.Id);- model.ProductSeName = product.GetSeName(_workContext.WorkingLanguage.Id);- model.YourEmailAddress = _workContext.CurrentCustomer.Email;- model.DisplayCaptcha = _captchaSettings.Enabled && _captchaSettings.ShowOnEmailProductToFriendPage;- return View(model);- }-[HttpPost][AutoValidateAntiforgeryToken][ValidateCaptcha]@@ -672,7 +657,7 @@ namespace Grand.Web.Controllers{var product = await _productService.GetProductById(model.ProductId);if (product == null || !product.Published || !_catalogSettings.EmailAFriendEnabled)- return RedirectToRoute("HomePage");+ return Content("");//validate CAPTCHAif (_captchaSettings.Enabled && _captchaSettings.ShowOnEmailProductToFriendPage && !captchaValid)@@ -704,7 +689,7 @@ namespace Grand.Web.Controllersmodel.SuccessfullySent = true;model.Result = _translationService.GetResource("Products.EmailAFriend.SuccessfullySent");- return View(model);+ return Json(model);}//If we got this far, something failed, redisplay form@@ -712,7 +697,10 @@ namespace Grand.Web.Controllersmodel.ProductName = product.GetTranslation(x => x.Name, _workContext.WorkingLanguage.Id);model.ProductSeName = product.GetSeName(_workContext.WorkingLanguage.Id);model.DisplayCaptcha = _captchaSettings.Enabled && _captchaSettings.ShowOnEmailProductToFriendPage;- return View(model);+ model.SuccessfullySent = false;+ model.Result = string.Join(",", ModelState.Values.SelectMany(v => v.Errors).Select(x => x.ErrorMessage));++ return Json(model);}#endregion