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 new validator

support520b6051ccb7
diff --git a/src/Web/Grand.Web.Admin/Controllers/OrderController.cs b/src/Web/Grand.Web.Admin/Controllers/OrderController.cs
index bd0b451f8..de989c3ce 100644
--- a/src/Web/Grand.Web.Admin/Controllers/OrderController.cs
+++ b/src/Web/Grand.Web.Admin/Controllers/OrderController.cs
@@ -18,7 +18,6 @@ using Grand.Web.Admin.Models.Orders;
using Grand.Web.Common.DataSource;
using Grand.Web.Common.Security.Authorization;
using MediatR;
-using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Grand.Business.Core.Interfaces.ExportImport;
@@ -390,33 +389,28 @@ namespace Grand.Web.Admin.Controllers
[PermissionAuthorizeAction(PermissionActionName.Delete)]
[HttpPost]
- public async Task<IActionResult> Delete(string id,
- [FromServices] ICustomerActivityService customerActivityService,
- [FromServices] IShipmentService shipmentService)
+ public async Task<IActionResult> Delete(OrderDeleteModel model,
+ [FromServices] ICustomerActivityService customerActivityService)
{
- var order = await _orderService.GetOrderById(id);
+ var order = await _orderService.GetOrderById(model.Id);
if (order == null || await CheckSalesManager(order))
//No order found with the specified id
return RedirectToAction("List");
//a vendor or staff does not have access to this functionality
if (_workContext.CurrentVendor != null || await _groupService.IsStaff(_workContext.CurrentCustomer))
- return RedirectToAction("Edit", "Order", new { id });
-
- var shipments = await shipmentService.GetShipmentsByOrder(order.Id);
- if (shipments.Any())
- ModelState.AddModelError("", "This order is in associated with shipment. Please delete it first.");
+ return RedirectToAction("Edit", "Order", new { model.Id });
if (ModelState.IsValid)
{
await _mediator.Send(new DeleteOrderCommand { Order = order });
- _ = customerActivityService.InsertActivity("DeleteOrder", id,
+ _ = customerActivityService.InsertActivity("DeleteOrder", model.Id,
_workContext.CurrentCustomer, HttpContext.Connection?.RemoteIpAddress?.ToString(),
_translationService.GetResource("ActivityLog.DeleteOrder"), order.Id);
return RedirectToAction("List");
}
Error(ModelState);
- return RedirectToAction("Edit", "Order", new { id });
+ return RedirectToAction("Edit", "Order", new { model.Id });
}
[PermissionAuthorizeAction(PermissionActionName.Delete)]
diff --git a/src/Web/Grand.Web.Admin/Models/Orders/OrderDeleteModel.cs b/src/Web/Grand.Web.Admin/Models/Orders/OrderDeleteModel.cs
new file mode 100644
index 000000000..2f389cf67
--- /dev/null
+++ b/src/Web/Grand.Web.Admin/Models/Orders/OrderDeleteModel.cs
@@ -0,0 +1,3 @@
+namespace Grand.Web.Admin.Models.Orders;
+
+public record OrderDeleteModel(string Id);
\ No newline at end of file
diff --git a/src/Web/Grand.Web.Admin/Validators/Orders/OrderDeleteModelValidator.cs b/src/Web/Grand.Web.Admin/Validators/Orders/OrderDeleteModelValidator.cs
new file mode 100644
index 000000000..b38a3bc23
--- /dev/null
+++ b/src/Web/Grand.Web.Admin/Validators/Orders/OrderDeleteModelValidator.cs
@@ -0,0 +1,21 @@
+using FluentValidation;
+using Grand.Business.Core.Interfaces.Checkout.Shipping;
+using Grand.Business.Core.Interfaces.Common.Localization;
+using Grand.Infrastructure.Validators;
+using Grand.Web.Admin.Models.Orders;
+
+public class OrderDeleteModelValidator : BaseGrandValidator<OrderDeleteModel>
+{
+ public OrderDeleteModelValidator(
+ IEnumerable<IValidatorConsumer<OrderDeleteModel>> validators,
+ IShipmentService shipmentService, ITranslationService translationService)
+ : base(validators)
+ {
+ RuleFor(x => x).CustomAsync(async (x, context, _) =>
+ {
+ var shipments = await shipmentService.GetShipmentsByOrder(x.Id);
+ if (shipments.Any())
+ context.AddFailure("This order is in associated with shipment. Please delete it first.");
+ });
+ }
+}
\ No newline at end of file