From 61f0a0f17de14c21bde20cdd2f28408f14b8ae39 Mon Sep 17 00:00:00 2001 From: malik masis Date: Fri, 9 Dec 2022 14:49:48 +0300 Subject: [PATCH] Added a new controller to web for removing the app layer --- .../CreateCommentWithParameteresInput.cs | 26 ++++++++++ .../CmsKitPublicCommentsController.cs | 47 +++++++++++++++++++ .../Commenting/CommentingViewComponent.cs | 5 +- .../Shared/Components/Commenting/default.js | 20 +++++--- 4 files changed, 88 insertions(+), 10 deletions(-) create mode 100644 modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Comments/CreateCommentWithParameteresInput.cs create mode 100644 modules/cms-kit/src/Volo.CmsKit.Public.Web/Controllers/CmsKitPublicCommentsController.cs diff --git a/modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Comments/CreateCommentWithParameteresInput.cs b/modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Comments/CreateCommentWithParameteresInput.cs new file mode 100644 index 0000000000..7e156ef375 --- /dev/null +++ b/modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Comments/CreateCommentWithParameteresInput.cs @@ -0,0 +1,26 @@ +using System; +using System.ComponentModel.DataAnnotations; +using Volo.Abp.Validation; +using Volo.CmsKit.Comments; + +namespace Volo.CmsKit.Public.Comments; + +[Serializable] +public class CreateCommentWithParameteresInput +{ + [Required] + [DynamicStringLength(typeof(CommentConsts), nameof(CommentConsts.MaxTextLength))] + public string Text { get; set; } + + [Required] + public string EntityType { get; set; } + + [Required] + public string EntityId { get; set; } + + public Guid? RepliedCommentId { get; set; } + + public Guid? CaptchaToken { get; set; } + + public int CaptchaAnswer { get; set; } +} diff --git a/modules/cms-kit/src/Volo.CmsKit.Public.Web/Controllers/CmsKitPublicCommentsController.cs b/modules/cms-kit/src/Volo.CmsKit.Public.Web/Controllers/CmsKitPublicCommentsController.cs new file mode 100644 index 0000000000..89dea5001f --- /dev/null +++ b/modules/cms-kit/src/Volo.CmsKit.Public.Web/Controllers/CmsKitPublicCommentsController.cs @@ -0,0 +1,47 @@ +using System.Threading.Tasks; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Options; +using Volo.Abp.AspNetCore.Mvc; +using Volo.CmsKit.Comments; +using Volo.CmsKit.Public.Comments; +using Volo.CmsKit.Public.Web.Pages.CmsKit.Shared.Components.Commenting; +using Volo.CmsKit.Public.Web.Security.Captcha; + +namespace Volo.CmsKit.Public.Web.Controllers; + +//[Route("cms-kit/public-comments")] +public class CmsKitPublicCommentsController : AbpController +{ + public ICommentPublicAppService CommentPublicAppService { get; } + protected CmsKitCommentOptions CmsKitCommentOptions { get; } + public SimpleMathsCaptchaGenerator SimpleMathsCaptchaGenerator { get; } + + public CmsKitPublicCommentsController( + ICommentPublicAppService commentPublicAppService, + IOptions cmsKitCommentOptions, + SimpleMathsCaptchaGenerator simpleMathsCaptchaGenerator) + { + CommentPublicAppService = commentPublicAppService; + CmsKitCommentOptions = cmsKitCommentOptions.Value; + SimpleMathsCaptchaGenerator = simpleMathsCaptchaGenerator; + } + + [HttpPost] + public async Task ValidateAsync([FromBody] CreateCommentWithParameteresInput input) + { + if (CmsKitCommentOptions.IsRecaptchaEnabled && input.CaptchaToken.HasValue) + { + SimpleMathsCaptchaGenerator.Validate(input.CaptchaToken.Value, input.CaptchaAnswer); + } + + await CommentPublicAppService.CreateAsync(input.EntityType, input.EntityId, + new CreateCommentInput() + { + Text = input.Text, + RepliedCommentId = input.RepliedCommentId, + CaptchaAnswer = input.CaptchaAnswer, + CaptchaToken = input.CaptchaToken + }); + } +} diff --git a/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Commenting/CommentingViewComponent.cs b/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Commenting/CommentingViewComponent.cs index 6e1db54a5b..bdf8d3ffa0 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Commenting/CommentingViewComponent.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Commenting/CommentingViewComponent.cs @@ -9,8 +9,8 @@ using Volo.Abp.AspNetCore.Mvc; using Volo.Abp.AspNetCore.Mvc.UI; using Volo.Abp.AspNetCore.Mvc.UI.Widgets; using Volo.CmsKit.Comments; -using Volo.CmsKit.Public.Application.Security.VoloCaptcha; using Volo.CmsKit.Public.Comments; +using Volo.CmsKit.Public.Web.Security.Captcha; using Volo.CmsKit.Web.Renderers; namespace Volo.CmsKit.Public.Web.Pages.CmsKit.Shared.Components.Commenting; @@ -129,9 +129,6 @@ public class CommentingViewComponent : AbpViewComponent public string Captcha { get; set; } public string CaptchaImageBase64 { get; set; } - - - } } diff --git a/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Commenting/default.js b/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Commenting/default.js index 7c4b86712e..0e8e99c2bc 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Commenting/default.js +++ b/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Commenting/default.js @@ -127,17 +127,25 @@ formAsObject.repliedCommentId = null; } - volo.cmsKit.public.comments.commentPublic.create( - $commentArea.attr('data-entity-type'), - $commentArea.attr('data-entity-id'), - { + $.ajax({ + type: 'POST', + url: 'CmsKitPublicComments/Validate', + contentType: 'application/json; charset=utf-8', + dataType: 'json', + data: JSON.stringify({ + entityId: $commentArea.attr('data-entity-id'), + entityType: $commentArea.attr('data-entity-type'), repliedCommentId: formAsObject.repliedCommentId, text: formAsObject.commentText, captchaToken: formAsObject.captchaId, captchaAnswer: formAsObject.input?.captcha + }), + success: function () { + widgetManager.refresh($widget); + }, + error: function (data) { + abp.message.error(data.responseJSON.error.message); } - ).then(function () { - widgetManager.refresh($widget); }); }); });