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 - AffiliateController - Moving model validation to the validator from the controller.

supporta5b3e346f967
diff --git a/src/Web/Grand.Web.Admin/Controllers/AffiliateController.cs b/src/Web/Grand.Web.Admin/Controllers/AffiliateController.cs
index 6b033bc92..369145fae 100644
--- a/src/Web/Grand.Web.Admin/Controllers/AffiliateController.cs
+++ b/src/Web/Grand.Web.Admin/Controllers/AffiliateController.cs
@@ -1,15 +1,12 @@
-using Grand.Business.Core.Queries.Checkout.Orders;
-using Grand.Business.Core.Interfaces.Common.Localization;
+using Grand.Business.Core.Interfaces.Common.Localization;
using Grand.Business.Core.Interfaces.Common.Security;
using Grand.Business.Core.Utilities.Common.Security;
using Grand.Business.Core.Interfaces.Customers;
-using Grand.Business.Core.Queries.Customers;
using Grand.Web.Admin.Interfaces;
using Grand.Web.Admin.Models.Affiliates;
using Grand.Web.Common.DataSource;
using Grand.Web.Common.Filters;
using Grand.Web.Common.Security.Authorization;
-using MediatR;
using Microsoft.AspNetCore.Mvc;
namespace Grand.Web.Admin.Controllers
@@ -22,7 +19,6 @@ namespace Grand.Web.Admin.Controllers
private readonly ITranslationService _translationService;
private readonly IAffiliateService _affiliateService;
private readonly IAffiliateViewModelService _affiliateViewModelService;
- private readonly IMediator _mediator;
private readonly IPermissionService _permissionService;
#endregion
@@ -31,13 +27,11 @@ namespace Grand.Web.Admin.Controllers
public AffiliateController(ITranslationService translationService,
IAffiliateService affiliateService, IAffiliateViewModelService affiliateViewModelService,
- IMediator mediator,
IPermissionService permissionService)
{
_translationService = translationService;
_affiliateService = affiliateService;
_affiliateViewModelService = affiliateViewModelService;
- _mediator = mediator;
_permissionService = permissionService;
}
@@ -46,7 +40,10 @@ namespace Grand.Web.Admin.Controllers
#region Methods
//list
- public IActionResult Index() => RedirectToAction("List");
+ public IActionResult Index()
+ {
+ return RedirectToAction("List");
+ }
public IActionResult List()
{
@@ -140,31 +137,13 @@ namespace Grand.Web.Admin.Controllers
//delete
[PermissionAuthorizeAction(PermissionActionName.Delete)]
[HttpPost]
- public async Task<IActionResult> Delete(string id)
+ public async Task<IActionResult> Delete(AffiliateDeleteModel model)
{
- var affiliate = await _affiliateService.GetAffiliateById(id);
+ var affiliate = await _affiliateService.GetAffiliateById(model.Id);
if (affiliate == null)
//No affiliate found with the specified id
return RedirectToAction("List");
- var customers = new GetCustomerQuery {
- AffiliateId = affiliate.Id,
- PageSize = 1
- };
- var query_customer = (await _mediator.Send(customers)).Count();
- if (query_customer > 0)
- ModelState.AddModelError("", "There are exist customers related with affiliate");
-
- var orders = new GetOrderQuery {
- AffiliateId = affiliate.Id,
- PageSize = 1
- };
-
- var query_order = (await _mediator.Send(orders)).Count();
- if (query_order > 0)
- ModelState.AddModelError("", "There are exist orders related with affiliate");
-
-
if (ModelState.IsValid)
{
await _affiliateService.DeleteAffiliate(affiliate);
@@ -172,7 +151,7 @@ namespace Grand.Web.Admin.Controllers
return RedirectToAction("List");
}
Error(ModelState);
- return RedirectToAction("Edit", new { id });
+ return RedirectToAction("Edit", new { model.Id });
}
[PermissionAuthorizeAction(PermissionActionName.Preview)]
diff --git a/src/Web/Grand.Web.Admin/Models/Affiliates/AffiliateDeleteModel.cs b/src/Web/Grand.Web.Admin/Models/Affiliates/AffiliateDeleteModel.cs
new file mode 100644
index 000000000..61b4c481b
--- /dev/null
+++ b/src/Web/Grand.Web.Admin/Models/Affiliates/AffiliateDeleteModel.cs
@@ -0,0 +1,3 @@
+namespace Grand.Web.Admin.Models.Affiliates;
+
+public record AffiliateDeleteModel(string Id);
\ No newline at end of file
diff --git a/src/Web/Grand.Web.Admin/Validators/Affiliate/AffiliateDeleteValidator.cs b/src/Web/Grand.Web.Admin/Validators/Affiliate/AffiliateDeleteValidator.cs
new file mode 100644
index 000000000..1d373feac
--- /dev/null
+++ b/src/Web/Grand.Web.Admin/Validators/Affiliate/AffiliateDeleteValidator.cs
@@ -0,0 +1,44 @@
+using FluentValidation;
+using Grand.Business.Core.Interfaces.Common.Localization;
+using Grand.Business.Core.Interfaces.Customers;
+using Grand.Business.Core.Queries.Checkout.Orders;
+using Grand.Business.Core.Queries.Customers;
+using Grand.Infrastructure.Validators;
+using Grand.Web.Admin.Models.Affiliates;
+using MediatR;
+
+namespace Grand.Web.Admin.Validators.Affiliate
+{
+ public class AffiliateDeleteValidator : BaseGrandValidator<AffiliateDeleteModel>
+ {
+ public AffiliateDeleteValidator(IEnumerable<IValidatorConsumer<AffiliateDeleteModel>> validators,
+ IAffiliateService affiliateService, IMediator mediator,
+ ITranslationService translationService)
+ : base(validators)
+ {
+ RuleFor(x => x).CustomAsync(async (x, context, _) =>
+ {
+ var affiliate = await affiliateService.GetAffiliateById(x.Id);
+ if (affiliate == null)
+ context.AddFailure("No affiliate found with the specified id");
+
+ var customers = new GetCustomerQuery {
+ AffiliateId = affiliate.Id,
+ PageSize = 1
+ };
+ var queryCustomer = (await mediator.Send(customers)).Count();
+ if (queryCustomer > 0)
+ context.AddFailure("There are exist customers related with affiliate");
+
+ var orders = new GetOrderQuery {
+ AffiliateId = affiliate.Id,
+ PageSize = 1
+ };
+
+ var queryOrder = (await mediator.Send(orders)).Count();
+ if (queryOrder > 0)
+ context.AddFailure("There are exist orders related with affiliate");
+ });
+ }
+ }
+}
\ No newline at end of file