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.

Address Account - move validator logic from AccountController/CheckoutController to Validator

supportcca91e09b54e
diff --git a/src/Core/Grand.Infrastructure/ModelBinding/JsonBodyModelBinder.cs b/src/Core/Grand.Infrastructure/ModelBinding/JsonBodyModelBinder.cs
index e7abbcf3a..68b29a3c2 100644
--- a/src/Core/Grand.Infrastructure/ModelBinding/JsonBodyModelBinder.cs
+++ b/src/Core/Grand.Infrastructure/ModelBinding/JsonBodyModelBinder.cs
@@ -35,7 +35,7 @@ public class JsonBodyModelBinder : IModelBinder
bindingContext.Result =
ModelBindingResult.Success(JsonSerializer.Deserialize(jsonPayload, bindingContext.ModelType, options));
}
- catch(Exception exception)
+ catch(Exception)
{
bindingContext.Result = ModelBindingResult.Success(Activator.CreateInstance(bindingContext.ModelType));
}
diff --git a/src/Core/Grand.Infrastructure/StartupBase.cs b/src/Core/Grand.Infrastructure/StartupBase.cs
index bf3a6c351..8a0ef6b8e 100644
--- a/src/Core/Grand.Infrastructure/StartupBase.cs
+++ b/src/Core/Grand.Infrastructure/StartupBase.cs
@@ -249,7 +249,6 @@ namespace Grand.Infrastructure
#endregion
-
#region Methods
/// <summary>
diff --git a/src/Web/Grand.Web/Controllers/AccountController.cs b/src/Web/Grand.Web/Controllers/AccountController.cs
index f3885cd58..1d8500949 100644
--- a/src/Web/Grand.Web/Controllers/AccountController.cs
+++ b/src/Web/Grand.Web/Controllers/AccountController.cs
@@ -1,7 +1,6 @@
using Grand.Business.Core.Events.Customers;
using Grand.Business.Core.Extensions;
using Grand.Business.Core.Interfaces.Authentication;
-using Grand.Business.Core.Interfaces.Common.Addresses;
using Grand.Business.Core.Interfaces.Common.Directory;
using Grand.Business.Core.Interfaces.Common.Localization;
using Grand.Business.Core.Interfaces.Customers;
@@ -368,7 +367,7 @@ namespace Grand.Web.Controllers
return RedirectToRoute("HomePage");
}
- if (ModelState is { IsValid: true, ErrorCount: 0 })
+ if (ModelState is { IsValid: true })
{
if (_customerSettings.UsernamesEnabled && model.Username != null)
{
@@ -599,8 +598,7 @@ namespace Grand.Web.Controllers
[HttpPost]
[AutoValidateAntiforgeryToken]
- public virtual async Task<IActionResult> RemoveExternalAssociation(string id,
- [FromServices] IExternalAuthenticationService openAuthenticationService)
+ public virtual async Task<IActionResult> RemoveExternalAssociation(string id, [FromServices] IExternalAuthenticationService openAuthenticationService)
{
if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
return Challenge();
@@ -702,27 +700,18 @@ namespace Grand.Web.Controllers
[HttpPost]
[AutoValidateAntiforgeryToken]
public virtual async Task<IActionResult> AddressAdd(CustomerAddressEditModel model,
- [FromServices] AddressSettings addressSettings,
- [FromServices] IAddressAttributeParser addressAttributeParser)
+ [FromServices] AddressSettings addressSettings)
{
if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
return Challenge();
var customer = _workContext.CurrentCustomer;
- //custom address attributes
- var customAttributes = await _mediator.Send(new GetParseCustomAddressAttributes
- { SelectedAttributes = model.Address.SelectedAttributes });
- var customAttributeWarnings = await addressAttributeParser.GetAttributeWarnings(customAttributes);
- foreach (var error in customAttributeWarnings)
- {
- ModelState.AddModelError("", error);
- }
-
- if (ModelState is { IsValid: true, ErrorCount: 0 })
+ if (ModelState is { IsValid: true })
{
var address = model.Address.ToEntity(_workContext.CurrentCustomer, addressSettings);
- address.Attributes = customAttributes;
+ address.Attributes = await _mediator.Send(new GetParseCustomAddressAttributes
+ { SelectedAttributes = model.Address.SelectedAttributes });;
address.CreatedOnUtc = DateTime.UtcNow;
customer.Addresses.Add(address);
@@ -731,8 +720,7 @@ namespace Grand.Web.Controllers
return RedirectToRoute("CustomerAddresses");
}
- var countries =
- await _countryService.GetAllCountries(_workContext.WorkingLanguage.Id, _workContext.CurrentStore.Id);
+ var countries = await _countryService.GetAllCountries(_workContext.WorkingLanguage.Id, _workContext.CurrentStore.Id);
//If we got this far, something failed, redisplay form
model.Address = await _mediator.Send(new GetAddressModel {
Language = _workContext.WorkingLanguage,
@@ -741,7 +729,9 @@ namespace Grand.Web.Controllers
Address = null,
ExcludeProperties = true,
Customer = _workContext.CurrentCustomer,
- LoadCountries = () => countries
+ LoadCountries = () => countries,
+ OverrideAttributes = await _mediator.Send(new GetParseCustomAddressAttributes
+ { SelectedAttributes = model.Address.SelectedAttributes })
});
return View(model);
@@ -769,7 +759,7 @@ namespace Grand.Web.Controllers
Address = address,
ExcludeProperties = false,
Customer = _workContext.CurrentCustomer,
- LoadCountries = () => countries
+ LoadCountries = () => countries,
});
return View(model);
@@ -778,32 +768,23 @@ namespace Grand.Web.Controllers
[HttpPost]
[AutoValidateAntiforgeryToken]
public virtual async Task<IActionResult> AddressEdit(CustomerAddressEditModel model,
- [FromServices] AddressSettings addressSettings,
- [FromServices] IAddressAttributeParser addressAttributeParser)
+ [FromServices] AddressSettings addressSettings)
{
if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
return Challenge();
var customer = _workContext.CurrentCustomer;
//find address (ensure that it belongs to the current customer)
- var address = customer.Addresses.FirstOrDefault(a => a.Id == model.AddressId);
+ var address = customer.Addresses.FirstOrDefault(a => a.Id == model.Address.Id);
if (address == null)
//address is not found
return RedirectToRoute("CustomerAddresses");
-
- //custom address attributes
- var customAttributes = await _mediator.Send(new GetParseCustomAddressAttributes
- { SelectedAttributes = model.Address.SelectedAttributes });
- var customAttributeWarnings = await addressAttributeParser.GetAttributeWarnings(customAttributes);
- foreach (var error in customAttributeWarnings)
- {
- ModelState.AddModelError("", error);
- }
-
- if (ModelState is { IsValid: true, ErrorCount: 0 })
+
+ if (ModelState is { IsValid: true })
{
address = model.Address.ToEntity(address, _workContext.CurrentCustomer, addressSettings);
- address.Attributes = customAttributes;
+ address.Attributes = await _mediator.Send(new GetParseCustomAddressAttributes
+ { SelectedAttributes = model.Address.SelectedAttributes });;
await _customerService.UpdateAddress(address, customer.Id);
if (customer.BillingAddress?.Id == address.Id)
@@ -825,7 +806,8 @@ namespace Grand.Web.Controllers
ExcludeProperties = true,
Customer = _workContext.CurrentCustomer,
LoadCountries = () => countries,
- OverrideAttributes = customAttributes
+ OverrideAttributes = await _mediator.Send(new GetParseCustomAddressAttributes
+ { SelectedAttributes = model.Address.SelectedAttributes })
});
return View(model);
diff --git a/src/Web/Grand.Web/Controllers/CheckoutController.cs b/src/Web/Grand.Web/Controllers/CheckoutController.cs
index ade3629ce..3be3771a4 100644
--- a/src/Web/Grand.Web/Controllers/CheckoutController.cs
+++ b/src/Web/Grand.Web/Controllers/CheckoutController.cs
@@ -5,7 +5,6 @@ using Grand.Business.Core.Interfaces.Catalog.Products;
using Grand.Business.Core.Interfaces.Checkout.Orders;
using Grand.Business.Core.Interfaces.Checkout.Payments;
using Grand.Business.Core.Interfaces.Checkout.Shipping;
-using Grand.Business.Core.Interfaces.Common.Addresses;
using Grand.Business.Core.Interfaces.Common.Directory;
using Grand.Business.Core.Interfaces.Common.Localization;
using Grand.Business.Core.Interfaces.Common.Logging;
@@ -46,7 +45,6 @@ namespace Grand.Web.Controllers
private readonly IPaymentTransactionService _paymentTransactionService;
private readonly ILogger _logger;
private readonly IOrderService _orderService;
- private readonly IAddressAttributeParser _addressAttributeParser;
private readonly ICustomerActivityService _customerActivityService;
private readonly IMediator _mediator;
private readonly IProductService _productService;
@@ -74,7 +72,6 @@ namespace Grand.Web.Controllers
IPaymentTransactionService paymentTransactionService,
ILogger logger,
IOrderService orderService,
- IAddressAttributeParser addressAttributeParser,
ICustomerActivityService customerActivityService,
IMediator mediator,
IProductService productService,
@@ -97,7 +94,6 @@ namespace Grand.Web.Controllers
_paymentTransactionService = paymentTransactionService;
_logger = logger;
_orderService = orderService;
- _addressAttributeParser = addressAttributeParser;
_customerActivityService = customerActivityService;
_mediator = mediator;
_productService = productService;
@@ -493,14 +489,6 @@ namespace Grand.Web.Controllers
}
else
{
- //custom address attributes
- var customAttributes = await _mediator.Send(new GetParseCustomAddressAttributes { SelectedAttributes = model.BillingNewAddress.SelectedAttributes });
- var customAttributeWarnings = await _addressAttributeParser.GetAttributeWarnings(customAttributes);
- foreach (var error in customAttributeWarnings)
- {
- ModelState.AddModelError("", error);
- }
-
if (!ModelState.IsValid)
{
//model is not valid. redisplay the form with errors
@@ -511,7 +499,7 @@ namespace Grand.Web.Controllers
Language = _workContext.WorkingLanguage,
Store = _workContext.CurrentStore,
SelectedCountryId = model.BillingNewAddress.CountryId,
- OverrideAttributes = customAttributes
+ OverrideAttributes = await _mediator.Send(new GetParseCustomAddressAttributes { SelectedAttributes = model.BillingNewAddress.SelectedAttributes })
});
billingAddressModel.NewAddressPreselected = true;
@@ -543,7 +531,7 @@ namespace Grand.Web.Controllers
? model.BillingNewAddress.ToEntity()
: model.BillingNewAddress.ToEntity(_workContext.CurrentCustomer, addressSettings);
- address.Attributes = customAttributes;
+ address.Attributes = await _mediator.Send(new GetParseCustomAddressAttributes { SelectedAttributes = model.BillingNewAddress.SelectedAttributes });
address.CreatedOnUtc = DateTime.UtcNow;
address.AddressType =
_addressSettings.AddressTypeEnabled ? AddressType.Billing : AddressType.Any;
@@ -655,15 +643,6 @@ namespace Grand.Web.Controllers
}
else
{
- //custom address attributes
- var customAttributes = await _mediator.Send(new GetParseCustomAddressAttributes { SelectedAttributes = model.ShippingNewAddress.SelectedAttributes });
- var customAttributeWarnings =
- await _addressAttributeParser.GetAttributeWarnings(customAttributes);
- foreach (var error in customAttributeWarnings)
- {
- ModelState.AddModelError("", error);
- }
-
if (!ModelState.IsValid)
{
//model is not valid. redisplay the form with errors
@@ -673,7 +652,7 @@ namespace Grand.Web.Controllers
Language = _workContext.WorkingLanguage,
Store = _workContext.CurrentStore,
SelectedCountryId = model.ShippingNewAddress.CountryId,
- OverrideAttributes = customAttributes
+ OverrideAttributes = await _mediator.Send(new GetParseCustomAddressAttributes { SelectedAttributes = model.ShippingNewAddress.SelectedAttributes })
});
shippingAddressModel.NewAddressPreselected = true;
@@ -704,7 +683,7 @@ namespace Grand.Web.Controllers
? model.ShippingNewAddress.ToEntity()
: model.ShippingNewAddress.ToEntity(_workContext.CurrentCustomer, addressSettings);
- address.Attributes = customAttributes;
+ address.Attributes = await _mediator.Send(new GetParseCustomAddressAttributes { SelectedAttributes = model.ShippingNewAddress.SelectedAttributes });
address.CreatedOnUtc = DateTime.UtcNow;
address.AddressType = _addressSettings.AddressTypeEnabled
? model.BillToTheSameAddress ? AddressType.Any : AddressType.Shipping
diff --git a/src/Web/Grand.Web/Models/Customer/CustomerAddressEditModel.cs b/src/Web/Grand.Web/Models/Customer/CustomerAddressEditModel.cs
index 28113c452..d2ffc0da9 100644
--- a/src/Web/Grand.Web/Models/Customer/CustomerAddressEditModel.cs
+++ b/src/Web/Grand.Web/Models/Customer/CustomerAddressEditModel.cs
@@ -10,6 +10,5 @@ namespace Grand.Web.Models.Customer
Address = new AddressModel();
}
public AddressModel Address { get; set; }
- public string AddressId { get; set; }
}
}
\ No newline at end of file
diff --git a/src/Web/Grand.Web/Validators/Common/AddressValidator.cs b/src/Web/Grand.Web/Validators/Common/AddressValidator.cs
index bca8a01ac..4160cb8f0 100644
--- a/src/Web/Grand.Web/Validators/Common/AddressValidator.cs
+++ b/src/Web/Grand.Web/Validators/Common/AddressValidator.cs
@@ -1,9 +1,12 @@
using FluentValidation;
+using Grand.Business.Core.Interfaces.Common.Addresses;
using Grand.Business.Core.Interfaces.Common.Directory;
using Grand.Business.Core.Interfaces.Common.Localization;
using Grand.Domain.Common;
using Grand.Infrastructure.Validators;
+using Grand.Web.Features.Models.Common;
using Grand.Web.Models.Common;
+using MediatR;
namespace Grand.Web.Validators.Common
{
@@ -11,6 +14,7 @@ namespace Grand.Web.Validators.Common
{
public AddressValidator(
IEnumerable<IValidatorConsumer<AddressModel>> validators,
+ IMediator mediator, IAddressAttributeParser addressAttributeParser,
ITranslationService translationService,
ICountryService countryService,
AddressSettings addressSettings)
@@ -84,6 +88,16 @@ namespace Grand.Web.Validators.Common
{
RuleFor(x => x.FaxNumber).NotEmpty().WithMessage(translationService.GetResource("Account.Fields.Fax.Required"));
}
+ RuleFor(x => x).CustomAsync(async (x, context, _) =>
+ {
+ var customAttributes = await mediator.Send(new GetParseCustomAddressAttributes
+ { SelectedAttributes = x.SelectedAttributes });
+ var customAttributeWarnings = await addressAttributeParser.GetAttributeWarnings(customAttributes);
+ foreach (var error in customAttributeWarnings)
+ {
+ context.AddFailure(error);
+ }
+ });
}
}
}
\ No newline at end of file
diff --git a/src/Web/Grand.Web/Validators/Customer/CheckoutBillingAddressValidator.cs b/src/Web/Grand.Web/Validators/Customer/CheckoutBillingAddressValidator.cs
index c8b268b38..57de84d45 100644
--- a/src/Web/Grand.Web/Validators/Customer/CheckoutBillingAddressValidator.cs
+++ b/src/Web/Grand.Web/Validators/Customer/CheckoutBillingAddressValidator.cs
@@ -1,10 +1,12 @@
-using Grand.Business.Core.Interfaces.Common.Directory;
+using Grand.Business.Core.Interfaces.Common.Addresses;
+using Grand.Business.Core.Interfaces.Common.Directory;
using Grand.Business.Core.Interfaces.Common.Localization;
using Grand.Domain.Common;
using Grand.Infrastructure.Validators;
using Grand.Web.Models.Checkout;
using Grand.Web.Models.Common;
using Grand.Web.Validators.Common;
+using MediatR;
namespace Grand.Web.Validators.Customer
{
@@ -13,12 +15,13 @@ namespace Grand.Web.Validators.Customer
public CheckoutBillingAddressValidator(
IEnumerable<IValidatorConsumer<CheckoutBillingAddressModel>> validators,
IEnumerable<IValidatorConsumer<AddressModel>> addressValidators,
+ IMediator mediator, IAddressAttributeParser addressAttributeParser,
ITranslationService translationService,
ICountryService countryService,
AddressSettings addressSettings)
: base(validators)
{
- RuleFor(x => x.BillingNewAddress).SetValidator(new AddressValidator(addressValidators, translationService, countryService, addressSettings));
+ RuleFor(x => x.BillingNewAddress).SetValidator(new AddressValidator(addressValidators, mediator, addressAttributeParser, translationService, countryService, addressSettings));
}
}
}
diff --git a/src/Web/Grand.Web/Validators/Customer/CheckoutShippingAddressValidator.cs b/src/Web/Grand.Web/Validators/Customer/CheckoutShippingAddressValidator.cs
index 7cb482398..76e322155 100644
--- a/src/Web/Grand.Web/Validators/Customer/CheckoutShippingAddressValidator.cs
+++ b/src/Web/Grand.Web/Validators/Customer/CheckoutShippingAddressValidator.cs
@@ -1,10 +1,12 @@
-using Grand.Business.Core.Interfaces.Common.Directory;
+using Grand.Business.Core.Interfaces.Common.Addresses;
+using Grand.Business.Core.Interfaces.Common.Directory;
using Grand.Business.Core.Interfaces.Common.Localization;
using Grand.Domain.Common;
using Grand.Infrastructure.Validators;
using Grand.Web.Models.Checkout;
using Grand.Web.Models.Common;
using Grand.Web.Validators.Common;
+using MediatR;
namespace Grand.Web.Validators.Customer
{
@@ -13,12 +15,13 @@ namespace Grand.Web.Validators.Customer
public CheckoutShippingAddressValidator(
IEnumerable<IValidatorConsumer<CheckoutShippingAddressModel>> validators,
IEnumerable<IValidatorConsumer<AddressModel>> addressValidators,
+ IMediator mediator, IAddressAttributeParser addressAttributeParser,
ITranslationService translationService,
ICountryService countryService,
AddressSettings addressSettings)
: base(validators)
{
- RuleFor(x => x.ShippingNewAddress).SetValidator(new AddressValidator(addressValidators, translationService, countryService, addressSettings));
+ RuleFor(x => x.ShippingNewAddress).SetValidator(new AddressValidator(addressValidators, mediator, addressAttributeParser, translationService, countryService, addressSettings));
}
}
}
diff --git a/src/Web/Grand.Web/Validators/Customer/CustomerAddressEditValidator.cs b/src/Web/Grand.Web/Validators/Customer/CustomerAddressEditValidator.cs
index 6f933e59e..45a122751 100644
--- a/src/Web/Grand.Web/Validators/Customer/CustomerAddressEditValidator.cs
+++ b/src/Web/Grand.Web/Validators/Customer/CustomerAddressEditValidator.cs
@@ -1,10 +1,12 @@
-using Grand.Business.Core.Interfaces.Common.Directory;
+using Grand.Business.Core.Interfaces.Common.Addresses;
+using Grand.Business.Core.Interfaces.Common.Directory;
using Grand.Business.Core.Interfaces.Common.Localization;
using Grand.Domain.Common;
using Grand.Infrastructure.Validators;
using Grand.Web.Models.Common;
using Grand.Web.Models.Customer;
using Grand.Web.Validators.Common;
+using MediatR;
namespace Grand.Web.Validators.Customer
{
@@ -12,13 +14,14 @@ namespace Grand.Web.Validators.Customer
{
public CustomerAddressEditValidator(
IEnumerable<IValidatorConsumer<CustomerAddressEditModel>> validators,
- IEnumerable<IValidatorConsumer<AddressModel>> addressvalidators,
+ IEnumerable<IValidatorConsumer<AddressModel>> addressValidators,
+ IMediator mediator, IAddressAttributeParser addressAttributeParser,
ITranslationService translationService,
ICountryService countryService,
AddressSettings addressSettings)
: base(validators)
{
- RuleFor(x => x.Address).SetValidator(new AddressValidator(addressvalidators, translationService, countryService, addressSettings));
+ RuleFor(x => x.Address).SetValidator(new AddressValidator(addressValidators, mediator, addressAttributeParser, translationService, countryService, addressSettings));
}
}
}