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.

Admin panel - OrderController.cs - add OrderAddressModelValidator

support5d7c76d8184a
diff --git a/src/Web/Grand.Web.Admin/Controllers/OrderController.cs b/src/Web/Grand.Web.Admin/Controllers/OrderController.cs
index de989c3ce..52c55d459 100644
--- a/src/Web/Grand.Web.Admin/Controllers/OrderController.cs
+++ b/src/Web/Grand.Web.Admin/Controllers/OrderController.cs
@@ -1136,26 +1136,25 @@ namespace Grand.Web.Admin.Controllers
}
var address = new Address();
- if (model.BillingAddress && order.BillingAddress != null)
- if (order.BillingAddress.Id == model.Address.Id)
- address = order.BillingAddress;
- if (!model.BillingAddress && order.ShippingAddress != null)
- if (order.ShippingAddress.Id == model.Address.Id)
- address = order.ShippingAddress;
-
- if (address == null)
- throw new ArgumentException("No address found with the specified id");
-
- //custom address attributes
- var customAttributes = await model.Address.ParseCustomAddressAttributes(addressAttributeParser, addressAttributeService);
- var customAttributeWarnings = await addressAttributeParser.GetAttributeWarnings(customAttributes);
- foreach (var error in customAttributeWarnings)
+ switch (model.BillingAddress)
{
- ModelState.AddModelError("", error);
+ case true when order.BillingAddress != null:
+ {
+ if (order.BillingAddress.Id == model.Address.Id)
+ address = order.BillingAddress;
+ break;
+ }
+ case false when order.ShippingAddress != null:
+ {
+ if (order.ShippingAddress.Id == model.Address.Id)
+ address = order.ShippingAddress;
+ break;
+ }
}
if (ModelState.IsValid)
{
+ var customAttributes = await model.Address.ParseCustomAddressAttributes(addressAttributeParser, addressAttributeService);
await _orderViewModelService.UpdateOrderAddress(order, address, model, customAttributes);
return RedirectToAction("AddressEdit", new { addressId = model.Address.Id, orderId = model.OrderId, model.BillingAddress });
}
diff --git a/src/Web/Grand.Web.Admin/Validators/Orders/OrderAddressModelValidator.cs b/src/Web/Grand.Web.Admin/Validators/Orders/OrderAddressModelValidator.cs
new file mode 100644
index 000000000..9367afb3a
--- /dev/null
+++ b/src/Web/Grand.Web.Admin/Validators/Orders/OrderAddressModelValidator.cs
@@ -0,0 +1,54 @@
+using FluentValidation;
+using Grand.Business.Core.Interfaces.Checkout.Orders;
+using Grand.Business.Core.Interfaces.Checkout.Shipping;
+using Grand.Business.Core.Interfaces.Common.Addresses;
+using Grand.Business.Core.Interfaces.Common.Localization;
+using Grand.Domain.Common;
+using Grand.Infrastructure.Validators;
+using Grand.Web.Admin.Extensions;
+using Grand.Web.Admin.Models.Orders;
+using Microsoft.AspNetCore.Mvc;
+
+public class OrderAddressModelValidator : BaseGrandValidator<OrderAddressModel>
+{
+ public OrderAddressModelValidator(
+ IEnumerable<IValidatorConsumer<OrderAddressModel>> validators,
+ IOrderService orderService,
+ IAddressAttributeService addressAttributeService,
+ IAddressAttributeParser addressAttributeParser,
+ ITranslationService translationService)
+ : base(validators)
+ {
+ RuleFor(x => x).CustomAsync(async (x, context, _) =>
+ {
+ var customAttributes = await x.Address.ParseCustomAddressAttributes(addressAttributeParser, addressAttributeService);
+ var customAttributeWarnings = await addressAttributeParser.GetAttributeWarnings(customAttributes);
+ foreach (var error in customAttributeWarnings)
+ {
+ context.AddFailure(error);
+ }
+ var order = await orderService.GetOrderById(x.OrderId);
+ if (order != null)
+ {
+ Address address = null;
+ switch (x.BillingAddress)
+ {
+ case true when order.BillingAddress != null:
+ {
+ if (order.BillingAddress.Id == x.Address.Id)
+ address = order.BillingAddress;
+ break;
+ }
+ case false when order.ShippingAddress != null:
+ {
+ if (order.ShippingAddress.Id == x.Address.Id)
+ address = order.ShippingAddress;
+ break;
+ }
+ }
+ if (address == null)
+ context.AddFailure("No address found with the specified id");
+ }
+ });
+ }
+}
\ No newline at end of file