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.
C# aspnet-corecsharpdockerdotnetecommerceecommerce-platformmongodbmulti-storemulti-vendornosqlonline-shopshoppingcart
NewsController - move validator logic from Controller to Validator
support1fdc51cf3e6a
diff --git a/src/Web/Grand.Web/Controllers/NewsController.cs b/src/Web/Grand.Web/Controllers/NewsController.csindex caa701248..4f05214b8 100644--- a/src/Web/Grand.Web/Controllers/NewsController.cs+++ b/src/Web/Grand.Web/Controllers/NewsController.cs@@ -1,6 +1,5 @@using Grand.Business.Core.Extensions;using Grand.Business.Core.Interfaces.Cms;-using Grand.Business.Core.Interfaces.Common.Directory;using Grand.Business.Core.Interfaces.Common.Localization;using Grand.Business.Core.Interfaces.Common.Logging;using Grand.Business.Core.Interfaces.Common.Security;@@ -9,7 +8,6 @@ using Grand.Domain.News;using Grand.Infrastructure;using Grand.Web.Commands.Models.News;using Grand.Web.Common.Filters;-using Grand.Web.Common.Security.Captcha;using Grand.Web.Events;using Grand.Web.Features.Models.News;using Grand.Web.Models.News;@@ -30,7 +28,6 @@ namespace Grand.Web.Controllersprivate readonly IPermissionService _permissionService;private readonly IMediator _mediator;private readonly NewsSettings _newsSettings;- private readonly CaptchaSettings _captchaSettings;#endregion@@ -43,8 +40,7 @@ namespace Grand.Web.ControllersIAclService aclService,IPermissionService permissionService,IMediator mediator,- NewsSettings newsSettings,- CaptchaSettings captchaSettings)+ NewsSettings newsSettings){_newsService = newsService;_workContext = workContext;@@ -54,7 +50,6 @@ namespace Grand.Web.Controllers_permissionService = permissionService;_mediator = mediator;_newsSettings = newsSettings;- _captchaSettings = captchaSettings;}#endregion@@ -93,31 +88,16 @@ namespace Grand.Web.Controllers[HttpPost][AutoValidateAntiforgeryToken]- [ValidateCaptcha][DenySystemAccount]- public virtual async Task<IActionResult> NewsCommentAdd(string newsItemId,- NewsItemModel model, bool captchaValid,- [FromServices] IGroupService groupService- )+ public virtual async Task<IActionResult> NewsCommentAdd(NewsItemModel model){if (!_newsSettings.Enabled)return RedirectToRoute("HomePage");- var newsItem = await _newsService.GetNewsById(newsItemId);+ var newsItem = await _newsService.GetNewsById(model.NewsItemId);if (newsItem is not { Published: true } || !newsItem.AllowComments)return RedirectToRoute("HomePage");- //validate CAPTCHA- if (_captchaSettings.Enabled && _captchaSettings.ShowOnNewsCommentPage && !captchaValid)- {- ModelState.AddModelError("", _captchaSettings.GetWrongCaptchaMessage(_translationService));- }-- if (await groupService.IsGuest(_workContext.CurrentCustomer) && !_newsSettings.AllowNotRegisteredUsersToLeaveComments)- {- ModelState.AddModelError("", _translationService.GetResource("News.Comments.OnlyRegisteredUsersLeaveComments"));- }-if (ModelState.IsValid){await _mediator.Send(new InsertNewsCommentCommand { NewsItem = newsItem, Model = model });diff --git a/src/Web/Grand.Web/Models/News/NewsItemModel.cs b/src/Web/Grand.Web/Models/News/NewsItemModel.csindex 6b99b000a..783928995 100644--- a/src/Web/Grand.Web/Models/News/NewsItemModel.cs+++ b/src/Web/Grand.Web/Models/News/NewsItemModel.cs@@ -1,4 +1,5 @@using Grand.Infrastructure.Models;+using Grand.Web.Models.Common;using Grand.Web.Models.Media;namespace Grand.Web.Models.News@@ -11,6 +12,7 @@ namespace Grand.Web.Models.NewsAddNewComment = new AddNewsCommentModel();PictureModel = new PictureModel();}+ public string NewsItemId { get; set; }public string MetaKeywords { get; set; }public string MetaDescription { get; set; }public string MetaTitle { get; set; }@@ -24,5 +26,6 @@ namespace Grand.Web.Models.Newspublic DateTime CreatedOn { get; set; }public IList<NewsCommentModel> Comments { get; set; }public AddNewsCommentModel AddNewComment { get; set; }+ public ICaptchaValidModel Captcha { get; set; } = new CaptchaModel();}}\ No newline at end of filediff --git a/src/Web/Grand.Web/Validators/News/AddNewsCommentValidator.cs b/src/Web/Grand.Web/Validators/News/AddNewsCommentValidator.csnew file mode 100644index 000000000..f2f6ce364--- /dev/null+++ b/src/Web/Grand.Web/Validators/News/AddNewsCommentValidator.cs@@ -0,0 +1,18 @@+using FluentValidation;+using Grand.Business.Core.Interfaces.Common.Localization;+using Grand.Infrastructure.Validators;+using Grand.Web.Models.News;++namespace Grand.Web.Validators.News;++public class AddNewsCommentValidator : BaseGrandValidator<AddNewsCommentModel>+{+ public AddNewsCommentValidator(+ IEnumerable<IValidatorConsumer<AddNewsCommentModel>> validators,+ ITranslationService translationService)+ : base(validators)+ {+ RuleFor(x => x.CommentTitle).NotEmpty().WithMessage(translationService.GetResource("News.Comments.CommentTitle.Required"));+ RuleFor(x => x.CommentTitle).Length(1, 200).WithMessage(string.Format(translationService.GetResource("News.Comments.CommentTitle.MaxLengthValidation"), 200));+ RuleFor(x => x.CommentText).NotEmpty().WithMessage(translationService.GetResource("News.Comments.CommentText.Required"));+ }}\ No newline at end of filediff --git a/src/Web/Grand.Web/Validators/News/NewsItemValidator.cs b/src/Web/Grand.Web/Validators/News/NewsItemValidator.csindex 2c27d9ab3..3fa4f65e4 100644--- a/src/Web/Grand.Web/Validators/News/NewsItemValidator.cs+++ b/src/Web/Grand.Web/Validators/News/NewsItemValidator.cs@@ -1,19 +1,48 @@using FluentValidation;+using Grand.Business.Core.Interfaces.Cms;+using Grand.Business.Core.Interfaces.Common.Directory;using Grand.Business.Core.Interfaces.Common.Localization;+using Grand.Domain.News;+using Grand.Infrastructure;+using Grand.Infrastructure.Models;using Grand.Infrastructure.Validators;+using Grand.Web.Common.Security.Captcha;+using Grand.Web.Common.Validators;using Grand.Web.Models.News;+using Microsoft.AspNetCore.Http;namespace Grand.Web.Validators.News{- public class NewsItemValidator : BaseGrandValidator<AddNewsCommentModel>+ public class NewsItemValidator : BaseGrandValidator<NewsItemModel>{public NewsItemValidator(- IEnumerable<IValidatorConsumer<AddNewsCommentModel>> validators,+ IEnumerable<IValidatorConsumer<NewsItemModel>> validators,+ IEnumerable<IValidatorConsumer<ICaptchaValidModel>> validatorsCaptcha,+ IWorkContext workContext, IGroupService groupService, INewsService newsService,+ CaptchaSettings captchaSettings, NewsSettings newsSettings,+ IHttpContextAccessor contextAccessor, GoogleReCaptchaValidator googleReCaptchaValidator,ITranslationService translationService): base(validators){- RuleFor(x => x.CommentTitle).NotEmpty().WithMessage(translationService.GetResource("News.Comments.CommentTitle.Required"));- RuleFor(x => x.CommentTitle).Length(1, 200).WithMessage(string.Format(translationService.GetResource("News.Comments.CommentTitle.MaxLengthValidation"), 200));- RuleFor(x => x.CommentText).NotEmpty().WithMessage(translationService.GetResource("News.Comments.CommentText.Required"));+ RuleFor(x => x.AddNewComment.CommentTitle).NotEmpty().WithMessage(translationService.GetResource("News.Comments.CommentTitle.Required"));+ RuleFor(x => x.AddNewComment.CommentTitle).Length(1, 200).WithMessage(string.Format(translationService.GetResource("News.Comments.CommentTitle.MaxLengthValidation"), 200));+ RuleFor(x => x.AddNewComment.CommentText).NotEmpty().WithMessage(translationService.GetResource("News.Comments.CommentText.Required"));+ RuleFor(x => x).CustomAsync(async (x, context, _) =>+ {+ if (await groupService.IsGuest(workContext.CurrentCustomer) && !newsSettings.AllowNotRegisteredUsersToLeaveComments)+ {+ context.AddFailure(translationService.GetResource("News.Comments.OnlyRegisteredUsersLeaveComments"));+ }+ var newsItem = await newsService.GetNewsById(x.NewsItemId);+ if (newsItem is not { Published: true } || !newsItem.AllowComments)+ context.AddFailure(translationService.GetResource("News.Comments.NotAllowed"));+ });+ if (captchaSettings.Enabled && captchaSettings.ShowOnNewsCommentPage)+ {+ RuleFor(x => x.Captcha).NotNull()+ .WithMessage(translationService.GetResource("Account.Captcha.Required"));+ RuleFor(x => x.Captcha)+ .SetValidator(new CaptchaValidator(validatorsCaptcha, contextAccessor, googleReCaptchaValidator));+ }}}}\ No newline at end of filediff --git a/src/Web/Grand.Web/Views/News/NewsItem.cshtml b/src/Web/Grand.Web/Views/News/NewsItem.cshtmlindex 0cabdb4db..bad155837 100644--- a/src/Web/Grand.Web/Views/News/NewsItem.cshtml+++ b/src/Web/Grand.Web/Views/News/NewsItem.cshtml@@ -33,7 +33,7 @@<fieldset class="new-comment py-3 px-0" id="comments"><h5><strong>@Loc["News.Comments.LeaveYourComment"]</strong></h5><div class="notifications">- <div asp-validation-summary="ModelOnly" class="message-error"></div>+ <div asp-validation-summary="All" class="message-error"></div>@{var result = TempData["Grand.news.addcomment.result"] as string;}