Skip to content

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.

Grand.Business.Messages.Commands - remove IHttpContextAccessor

KrzysztofPajak9eaf2d8c74f4
diff --git a/src/Business/Grand.Business.Messages/Commands/Handlers/Common/InsertContactUsCommandHandler.cs b/src/Business/Grand.Business.Messages/Commands/Handlers/Common/InsertContactUsCommandHandler.cs
index 659e2a9d5..622c49272 100644
--- a/src/Business/Grand.Business.Messages/Commands/Handlers/Common/InsertContactUsCommandHandler.cs
+++ b/src/Business/Grand.Business.Messages/Commands/Handlers/Common/InsertContactUsCommandHandler.cs
@@ -2,7 +2,6 @@
using Grand.Domain.Data;
using Grand.Domain.Messages;
using MediatR;
-using Microsoft.AspNetCore.Http;
using System;
using System.Threading;
using System.Threading.Tasks;
@@ -12,20 +11,16 @@ namespace Grand.Business.Messages.Commands.Handlers
public class InsertContactUsCommandHandler : IRequestHandler<InsertContactUsCommand, bool>
{
private readonly IRepository<ContactUs> _repository;
- private readonly IHttpContextAccessor _httpContextAccessor;
public InsertContactUsCommandHandler(
- IRepository<ContactUs> repository,
- IHttpContextAccessor httpContextAccessor)
+ IRepository<ContactUs> repository)
{
_repository = repository;
- _httpContextAccessor = httpContextAccessor;
}
public async Task<bool> Handle(InsertContactUsCommand request, CancellationToken cancellationToken)
{
- var contactus = new ContactUs()
- {
+ var contactus = new ContactUs() {
CreatedOnUtc = DateTime.UtcNow,
CustomerId = request.CustomerId,
StoreId = request.StoreId,
@@ -37,7 +32,7 @@ namespace Grand.Business.Messages.Commands.Handlers
EmailAccountId = request.EmailAccountId,
Attributes = request.ContactAttributes,
ContactAttributeDescription = request.ContactAttributeDescription,
- IpAddress = _httpContextAccessor.HttpContext?.Connection?.RemoteIpAddress?.ToString()
+ IpAddress = request.RemoteIpAddress
};
await _repository.InsertAsync(contactus);
diff --git a/src/Business/Grand.Business.Messages/Commands/Models/Common/InsertContactUsCommand.cs b/src/Business/Grand.Business.Messages/Commands/Models/Common/InsertContactUsCommand.cs
index 4412d9693..d22c3c19a 100644
--- a/src/Business/Grand.Business.Messages/Commands/Models/Common/InsertContactUsCommand.cs
+++ b/src/Business/Grand.Business.Messages/Commands/Models/Common/InsertContactUsCommand.cs
@@ -13,6 +13,7 @@ namespace Grand.Business.Messages.Commands.Models
public string FullName { get; set; }
public string Subject { get; set; }
public string Enquiry { get; set; }
+ public string RemoteIpAddress { get; set; }
public string ContactAttributeDescription { get; set; }
public IList<CustomAttribute> ContactAttributes { get; set; }
public string EmailAccountId { get; set; }
diff --git a/src/Business/Grand.Business.Messages/Interfaces/IMessageProviderService.cs b/src/Business/Grand.Business.Messages/Interfaces/IMessageProviderService.cs
index 828bcb949..53262e60a 100644
--- a/src/Business/Grand.Business.Messages/Interfaces/IMessageProviderService.cs
+++ b/src/Business/Grand.Business.Messages/Interfaces/IMessageProviderService.cs
@@ -279,16 +279,9 @@ namespace Grand.Business.Messages.Interfaces
/// <summary>
/// Sends "email a friend" message
/// </summary>
- /// <param name="customer">Customer instance</param>
- /// <param name="store">Store</param>
- /// <param name="languageId">Message language identifier</param>
- /// <param name="product">Product instance</param>
- /// <param name="customerEmail">Customer's email</param>
- /// <param name="friendsEmail">Friend's email</param>
- /// <param name="personalMessage">Personal message</param>
/// <returns>Queued email identifier</returns>
Task<int> SendProductQuestionMessage(Customer customer, Store store, string languageId,
- Product product, string customerEmail, string fullName, string phone, string message);
+ Product product, string customerEmail, string fullName, string phone, string message, string ipaddress);
#endregion
@@ -453,21 +446,24 @@ namespace Grand.Business.Messages.Interfaces
/// <param name="body">Email body</param>
/// <param name="attrInfo">Attr info</param>
/// <param name="customAttributes">Custom Attributes</param>
+ /// <param name="ipaddress">ip address</param>
/// <returns>Queued email identifier</returns>
- Task<int> SendContactUsMessage(Customer customer, Store store, string languageId, string senderEmail, string senderName, string subject, string body, string attrInfo, IList<CustomAttribute> customAttributes);
+ Task<int> SendContactUsMessage(Customer customer, Store store, string languageId, string senderEmail, string senderName, string subject, string body, string attrInfo, IList<CustomAttribute> customAttributes, string ipaddress);
/// <summary>
/// Sends "contact vendor" message
/// </summary>
- /// <param name="vendor">Vendor</param>
+ /// <param name="customer">Customer</param>
/// <param name="store">Store</param>
+ /// <param name="vendor">Vendor</param>
/// <param name="languageId">Message language identifier</param>
/// <param name="senderEmail">Sender email</param>
/// <param name="senderName">Sender name</param>
/// <param name="subject">Email subject. Pass null if you want a message template subject to be used.</param>
/// <param name="body">Email body</param>
+ /// <param name="ipaddress">Ip Address</param>
/// <returns>Queued email identifier</returns>
- Task<int> SendContactVendorMessage(Customer customer, Store store, Vendor vendor, string languageId, string senderEmail, string senderName, string subject, string body);
+ Task<int> SendContactVendorMessage(Customer customer, Store store, Vendor vendor, string languageId, string senderEmail, string senderName, string subject, string body, string ipaddress);
/// <summary>
/// Sends a customer action event - Add to cart notification to a customer
diff --git a/src/Business/Grand.Business.Messages/Services/MessageProviderService.cs b/src/Business/Grand.Business.Messages/Services/MessageProviderService.cs
index d98241735..bb07a4b29 100644
--- a/src/Business/Grand.Business.Messages/Services/MessageProviderService.cs
+++ b/src/Business/Grand.Business.Messages/Services/MessageProviderService.cs
@@ -828,16 +828,9 @@ namespace Grand.Business.Messages.Services
/// <summary>
/// Sends "email a friend" message
/// </summary>
- /// <param name="customer">Customer instance</param>
- /// <param name="store">Store</param>
- /// <param name="languageId">Message language identifier</param>
- /// <param name="product">Product instance</param>
- /// <param name="customerEmail">Customer's email</param>
- /// <param name="friendsEmail">Friend's email</param>
- /// <param name="personalMessage">Personal message</param>
/// <returns>Queued email identifier</returns>
public virtual async Task<int> SendProductQuestionMessage(Customer customer, Store store, string languageId,
- Product product, string customerEmail, string fullName, string phone, string message)
+ Product product, string customerEmail, string fullName, string phone, string message, string ipaddress)
{
if (customer == null)
throw new ArgumentNullException(nameof(customer));
@@ -881,7 +874,8 @@ namespace Grand.Business.Messages.Services
Enquiry = bodyReplaced,
FullName = fullName,
Subject = subjectReplaced,
- EmailAccountId = emailAccount.Id
+ EmailAccountId = emailAccount.Id,
+ RemoteIpAddress = ipaddress
});
}
@@ -1601,7 +1595,7 @@ namespace Grand.Business.Messages.Services
/// <param name="customAttributes">CustomAttributes</param>
/// <returns>Queued email identifier</returns>
public virtual async Task<int> SendContactUsMessage(Customer customer, Store store, string languageId, string senderEmail,
- string senderName, string subject, string body, string attrInfo, IList<CustomAttribute> customAttributes)
+ string senderName, string subject, string body, string attrInfo, IList<CustomAttribute> customAttributes, string ipaddress)
{
var language = await EnsureLanguageIsActive(languageId, store.Id);
var messageTemplate = await GetMessageTemplate("Service.ContactUs", store.Id);
@@ -1652,7 +1646,8 @@ namespace Grand.Business.Messages.Services
Subject = string.IsNullOrEmpty(subject) ? "Contact Us (form)" : subject,
ContactAttributeDescription = attrInfo,
ContactAttributes = customAttributes,
- EmailAccountId = emailAccount.Id
+ EmailAccountId = emailAccount.Id,
+ RemoteIpAddress = ipaddress
});
}
return await SendNotification(messageTemplate, emailAccount, languageId, liquidObject, toEmail, toName,
@@ -1666,16 +1661,18 @@ namespace Grand.Business.Messages.Services
/// <summary>
/// Sends "contact vendor" message
/// </summary>
- /// <param name="vendor">Vendor</param>
+ /// <param name="customer">Customer</param>
/// <param name="store">Store</param>
+ /// <param name="vendor">Vendor</param>
/// <param name="languageId">Message language identifier</param>
/// <param name="senderEmail">Sender email</param>
/// <param name="senderName">Sender name</param>
/// <param name="subject">Email subject. Pass null if you want a message template subject to be used.</param>
/// <param name="body">Email body</param>
+ /// <param name="ipaddress">Ip address</param>
/// <returns>Queued email identifier</returns>
public virtual async Task<int> SendContactVendorMessage(Customer customer, Store store, Vendor vendor, string languageId, string senderEmail,
- string senderName, string subject, string body)
+ string senderName, string subject, string body, string ipaddress)
{
if (vendor == null)
throw new ArgumentNullException(nameof(vendor));
@@ -1727,6 +1724,7 @@ namespace Grand.Business.Messages.Services
VendorId = vendor.Id,
Email = senderEmail,
Enquiry = body,
+ RemoteIpAddress = ipaddress,
FullName = senderName,
Subject = String.IsNullOrEmpty(subject) ? "Contact Us (form)" : subject,
EmailAccountId = emailAccount.Id
diff --git a/src/Tests/Grand.Business.Messages.Tests/Commands/InsertContactUsCommandHandlerTests.cs b/src/Tests/Grand.Business.Messages.Tests/Commands/InsertContactUsCommandHandlerTests.cs
index c707a0820..1d2c45078 100644
--- a/src/Tests/Grand.Business.Messages.Tests/Commands/InsertContactUsCommandHandlerTests.cs
+++ b/src/Tests/Grand.Business.Messages.Tests/Commands/InsertContactUsCommandHandlerTests.cs
@@ -3,7 +3,6 @@ using Grand.Business.Messages.Commands.Models;
using Grand.Domain.Common;
using Grand.Domain.Data;
using Grand.Domain.Messages;
-using Microsoft.AspNetCore.Http;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System.Collections.Generic;
@@ -15,22 +14,18 @@ namespace Grand.Business.Messages.Tests.Commands
public class InsertContactUsCommandHandlerTests
{
private Mock<IRepository<ContactUs>> _repositoryMock;
- private Mock<IHttpContextAccessor> _accessor;
private InsertContactUsCommandHandler _handler;
[TestInitialize]
public void Init()
{
_repositoryMock = new Mock<IRepository<ContactUs>>();
- _accessor = new Mock<IHttpContextAccessor>();
- _handler = new InsertContactUsCommandHandler(_repositoryMock.Object, _accessor.Object);
+ _handler = new InsertContactUsCommandHandler(_repositoryMock.Object);
}
[TestMethod]
public async Task Handler_InsertEntity()
{
- var httpContextMock = new Mock<HttpContext>();
- _accessor.Setup(c => c.HttpContext).Returns(httpContextMock.Object);
var command = new InsertContactUsCommand() {
ContactAttributeDescription = "d",
Email = "[email protected]",
diff --git a/src/Tests/Grand.Business.Messages.Tests/Services/MessageProviderServiceTest.cs b/src/Tests/Grand.Business.Messages.Tests/Services/MessageProviderServiceTest.cs
index cf26afa42..9b548af48 100644
--- a/src/Tests/Grand.Business.Messages.Tests/Services/MessageProviderServiceTest.cs
+++ b/src/Tests/Grand.Business.Messages.Tests/Services/MessageProviderServiceTest.cs
@@ -75,7 +75,6 @@ namespace Grand.Business.Messages.Tests.Services
_queuedEmailServiceMock.Object,
_languageServiceMock.Object,
_emailAccountServiceMock.Object,
- _messageTokenProviderMock.Object,
_storeServiceMock.Object,
_storeHelperServiceMock.Object,
_groupService.Object,
diff --git a/src/Web/Grand.Web/Commands/Handler/Common/ContactUsSendCommandHandler.cs b/src/Web/Grand.Web/Commands/Handler/Common/ContactUsSendCommandHandler.cs
index 80ddfc452..473addc47 100644
--- a/src/Web/Grand.Web/Commands/Handler/Common/ContactUsSendCommandHandler.cs
+++ b/src/Web/Grand.Web/Commands/Handler/Common/ContactUsSendCommandHandler.cs
@@ -70,7 +70,7 @@ namespace Grand.Web.Commands.Handler.Common
{
request.Model.ContactAttribute = attributes;
request.Model.ContactAttributeInfo = await _contactAttributeParser.FormatAttributes(_workContext.WorkingLanguage, attributes, _workContext.CurrentCustomer);
- request.Model = await SendContactUs(request.Model, request.Store);
+ request.Model = await SendContactUs(request, request.Store);
//activity log
await _customerActivityService.InsertActivity("PublicStore.ContactUs", "", _translationService.GetResource("ActivityLog.PublicStore.ContactUs"));
@@ -408,17 +408,19 @@ namespace Grand.Web.Commands.Handler.Common
return model;
}
- private async Task<ContactUsModel> SendContactUs(ContactUsModel model, Store store)
+ private async Task<ContactUsModel> SendContactUs(ContactUsSendCommand request, Store store)
{
- var subject = _commonSettings.SubjectFieldOnContactUsForm ? model.Subject : null;
- var body = FormatText.ConvertText(model.Enquiry);
+ var subject = _commonSettings.SubjectFieldOnContactUsForm ? request.Model.Subject : null;
+ var body = FormatText.ConvertText(request.Model.Enquiry);
- await _messageProviderService.SendContactUsMessage(_workContext.CurrentCustomer, store, _workContext.WorkingLanguage.Id, model.Email.Trim(), model.FullName, subject, body, model.ContactAttributeInfo, model.ContactAttribute);
+ await _messageProviderService.SendContactUsMessage
+ (_workContext.CurrentCustomer, store, _workContext.WorkingLanguage.Id, request.Model.Email.Trim(), request.Model.FullName, subject,
+ body, request.Model.ContactAttributeInfo, request.Model.ContactAttribute, request.IpAddress);
- model.SuccessfullySent = true;
- model.Result = _translationService.GetResource("ContactUs.YourEnquiryHasBeenSent");
+ request.Model.SuccessfullySent = true;
+ request.Model.Result = _translationService.GetResource("ContactUs.YourEnquiryHasBeenSent");
- return model;
+ return request.Model;
}
}
}
diff --git a/src/Web/Grand.Web/Commands/Handler/Products/SendProductAskQuestionMessageCommandHandler.cs b/src/Web/Grand.Web/Commands/Handler/Products/SendProductAskQuestionMessageCommandHandler.cs
index c0e7dcd1e..42183688a 100644
--- a/src/Web/Grand.Web/Commands/Handler/Products/SendProductAskQuestionMessageCommandHandler.cs
+++ b/src/Web/Grand.Web/Commands/Handler/Products/SendProductAskQuestionMessageCommandHandler.cs
@@ -20,7 +20,7 @@ namespace Grand.Web.Commands.Handler.Products
{
await _messageProviderService.SendProductQuestionMessage(request.Customer, request.Store,
request.Language.Id, request.Product, request.Model.Email, request.Model.FullName, request.Model.Phone,
- FormatText.ConvertText(request.Model.Message));
+ FormatText.ConvertText(request.Model.Message), request.RemoteIpAddress);
return true;
}
diff --git a/src/Web/Grand.Web/Commands/Handler/Vendors/ContactVendorCommandHandler.cs b/src/Web/Grand.Web/Commands/Handler/Vendors/ContactVendorCommandHandler.cs
index 5759722c6..6ab4a4686 100644
--- a/src/Web/Grand.Web/Commands/Handler/Vendors/ContactVendorCommandHandler.cs
+++ b/src/Web/Grand.Web/Commands/Handler/Vendors/ContactVendorCommandHandler.cs
@@ -34,7 +34,7 @@ namespace Grand.Web.Commands.Handler.Vendors
var body = FormatText.ConvertText(request.Model.Enquiry);
await _messageProviderService.SendContactVendorMessage(_workContext.CurrentCustomer, request.Store, request.Vendor, _workContext.WorkingLanguage.Id,
- request.Model.Email.Trim(), request.Model.FullName, subject, body);
+ request.Model.Email.Trim(), request.Model.FullName, subject, body, request.IpAddress);
request.Model.SuccessfullySent = true;
request.Model.Result = _translationService.GetResource("ContactVendor.YourEnquiryHasBeenSent");
diff --git a/src/Web/Grand.Web/Commands/Models/Common/ContactUsSendCommand.cs b/src/Web/Grand.Web/Commands/Models/Common/ContactUsSendCommand.cs
index cdcb59955..f675baa40 100644
--- a/src/Web/Grand.Web/Commands/Models/Common/ContactUsSendCommand.cs
+++ b/src/Web/Grand.Web/Commands/Models/Common/ContactUsSendCommand.cs
@@ -11,6 +11,7 @@ namespace Grand.Web.Commands.Models.Common
public ContactUsModel Model { get; set; }
public IFormCollection Form { get; set; }
public bool CaptchaValid { get; set; }
+ public string IpAddress { get; set; }
public Store Store { get; set; }
}
}
diff --git a/src/Web/Grand.Web/Commands/Models/Products/SendProductAskQuestionMessageCommand.cs b/src/Web/Grand.Web/Commands/Models/Products/SendProductAskQuestionMessageCommand.cs
index 882c47ed3..3f9b4fe1e 100644
--- a/src/Web/Grand.Web/Commands/Models/Products/SendProductAskQuestionMessageCommand.cs
+++ b/src/Web/Grand.Web/Commands/Models/Products/SendProductAskQuestionMessageCommand.cs
@@ -14,5 +14,6 @@ namespace Grand.Web.Commands.Models.Products
public Language Language { get; set; }
public Product Product { get; set; }
public ProductAskQuestionModel Model { get; set; }
+ public string RemoteIpAddress { get; set; }
}
}
diff --git a/src/Web/Grand.Web/Commands/Models/Vendors/ContactVendorSendCommand.cs b/src/Web/Grand.Web/Commands/Models/Vendors/ContactVendorSendCommand.cs
index f84b3b31e..a0571b695 100644
--- a/src/Web/Grand.Web/Commands/Models/Vendors/ContactVendorSendCommand.cs
+++ b/src/Web/Grand.Web/Commands/Models/Vendors/ContactVendorSendCommand.cs
@@ -10,6 +10,7 @@ namespace Grand.Web.Commands.Models.Vendors
public Vendor Vendor { get; set; }
public Store Store { get; set; }
public ContactVendorModel Model { get; set; }
+ public string IpAddress { get; set; }
}
}
diff --git a/src/Web/Grand.Web/Controllers/CommonController.cs b/src/Web/Grand.Web/Controllers/CommonController.cs
index 5214730c1..9fb80f875 100644
--- a/src/Web/Grand.Web/Controllers/CommonController.cs
+++ b/src/Web/Grand.Web/Controllers/CommonController.cs
@@ -342,7 +342,8 @@ namespace Grand.Web.Controllers
CaptchaValid = captchaValid,
Form = form,
Model = model,
- Store = _workContext.CurrentStore
+ Store = _workContext.CurrentStore,
+ IpAddress = HttpContext?.Connection?.RemoteIpAddress?.ToString()
});
if (result.errors.Any())
diff --git a/src/Web/Grand.Web/Controllers/ProductController.cs b/src/Web/Grand.Web/Controllers/ProductController.cs
index b56dbafbd..a30da100f 100644
--- a/src/Web/Grand.Web/Controllers/ProductController.cs
+++ b/src/Web/Grand.Web/Controllers/ProductController.cs
@@ -770,7 +770,8 @@ namespace Grand.Web.Controllers
Language = _workContext.WorkingLanguage,
Store = _workContext.CurrentStore,
Model = model,
- Product = product
+ Product = product,
+ RemoteIpAddress = HttpContext.Connection?.RemoteIpAddress?.ToString()
});
//activity log
@@ -830,7 +831,8 @@ namespace Grand.Web.Controllers
Language = _workContext.WorkingLanguage,
Store = _workContext.CurrentStore,
Model = productaskqestionmodel,
- Product = product
+ Product = product,
+ RemoteIpAddress = HttpContext.Connection?.RemoteIpAddress?.ToString()
});
//activity log
diff --git a/src/Web/Grand.Web/Controllers/VendorController.cs b/src/Web/Grand.Web/Controllers/VendorController.cs
index b20880a18..4b6f34529 100644
--- a/src/Web/Grand.Web/Controllers/VendorController.cs
+++ b/src/Web/Grand.Web/Controllers/VendorController.cs
@@ -421,7 +421,7 @@ namespace Grand.Web.Controllers
if (ModelState.IsValid)
{
- model = await _mediator.Send(new ContactVendorSendCommand() { Model = model, Vendor = vendor, Store = _workContext.CurrentStore }); ;
+ model = await _mediator.Send(new ContactVendorSendCommand() { Model = model, Vendor = vendor, Store = _workContext.CurrentStore, IpAddress = HttpContext.Connection?.RemoteIpAddress?.ToString() });
return View(model);
}