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.

Minor refactoring with add new comments on the blog

supportd9801c1202d2
diff --git a/src/Web/Grand.Web.Common/Page/Paging/BasePageableModel.cs b/src/Web/Grand.Web.Common/Page/Paging/BasePageableModel.cs
index bdc3211f5..d0d884fd3 100644
--- a/src/Web/Grand.Web.Common/Page/Paging/BasePageableModel.cs
+++ b/src/Web/Grand.Web.Common/Page/Paging/BasePageableModel.cs
@@ -1,6 +1,6 @@
//Contributor : MVCContrib
-
using Grand.Domain;
+using Grand.SharedKernel.Attributes;
namespace Grand.Web.Common.Page.Paging
{
@@ -23,15 +23,20 @@ namespace Grand.Web.Common.Page.Paging
#endregion
#region Properties
-
+ [IgnoreApiUrl]
+ [IgnoreApi]
public int FirstItem { get; set; }
-
+ [IgnoreApiUrl]
+ [IgnoreApi]
public bool HasNextPage { get; set; }
-
+ [IgnoreApiUrl]
+ [IgnoreApi]
public bool HasPreviousPage { get; set; }
-
+ [IgnoreApiUrl]
+ [IgnoreApi]
public int LastItem { get; set; }
-
+ [IgnoreApiUrl]
+ [IgnoreApi]
public int PageIndex
{
get
@@ -42,13 +47,11 @@ namespace Grand.Web.Common.Page.Paging
return 0;
}
}
-
public int PageNumber { get; set; }
-
public int PageSize { get; set; }
-
+ [IgnoreApiUrl]
public int TotalItems { get; set; }
-
+ [IgnoreApiUrl]
public int TotalPages { get; set; }
#endregion
diff --git a/src/Web/Grand.Web.Common/Page/Paging/IPageableModel.cs b/src/Web/Grand.Web.Common/Page/Paging/IPageableModel.cs
index 79eded2fe..bedc502aa 100644
--- a/src/Web/Grand.Web.Common/Page/Paging/IPageableModel.cs
+++ b/src/Web/Grand.Web.Common/Page/Paging/IPageableModel.cs
@@ -1,5 +1,7 @@
//Contributor : MVCContrib
+using Grand.SharedKernel.Attributes;
+
namespace Grand.Web.Common.Page.Paging
{
/// <summary>
@@ -10,6 +12,7 @@ namespace Grand.Web.Common.Page.Paging
/// <summary>
/// The current page index (starts from 0)
/// </summary>
+ [IgnoreApi]
int PageIndex { get; }
/// <summary>
/// The current page number (starts from 1)
@@ -30,18 +33,22 @@ namespace Grand.Web.Common.Page.Paging
/// <summary>
/// The index of the first item in the page.
/// </summary>
+ [IgnoreApi]
int FirstItem { get; }
/// <summary>
/// The index of the last item in the page.
/// </summary>
+ [IgnoreApi]
int LastItem { get; }
/// <summary>
/// Whether there are pages before the current page.
/// </summary>
+ [IgnoreApi]
bool HasPreviousPage { get; }
/// <summary>
/// Whether there are pages after the current page.
/// </summary>
+ [IgnoreApi]
bool HasNextPage { get; }
}
diff --git a/src/Web/Grand.Web/Commands/Handler/Blogs/InsertBlogCommentCommandHandler.cs b/src/Web/Grand.Web/Commands/Handler/Blogs/InsertBlogCommentCommandHandler.cs
index 6d39ef093..a57deca0c 100644
--- a/src/Web/Grand.Web/Commands/Handler/Blogs/InsertBlogCommentCommandHandler.cs
+++ b/src/Web/Grand.Web/Commands/Handler/Blogs/InsertBlogCommentCommandHandler.cs
@@ -46,7 +46,7 @@ namespace Grand.Web.Commands.Handler.Blogs
BlogPostId = request.BlogPost.Id,
CustomerId = customer.Id,
StoreId = _workContext.CurrentStore.Id,
- CommentText = request.Model.AddNewComment.CommentText,
+ CommentText = request.Model.CommentText,
CreatedOnUtc = DateTime.UtcNow,
BlogPostTitle = request.BlogPost.Title
};
diff --git a/src/Web/Grand.Web/Commands/Models/Blogs/InsertBlogCommentCommand.cs b/src/Web/Grand.Web/Commands/Models/Blogs/InsertBlogCommentCommand.cs
index d5278a382..603142dd5 100644
--- a/src/Web/Grand.Web/Commands/Models/Blogs/InsertBlogCommentCommand.cs
+++ b/src/Web/Grand.Web/Commands/Models/Blogs/InsertBlogCommentCommand.cs
@@ -6,7 +6,7 @@ namespace Grand.Web.Commands.Models.Blogs
{
public class InsertBlogCommentCommand : IRequest<BlogComment>
{
- public BlogPostModel Model { get; set; }
+ public AddBlogCommentModel Model { get; set; }
public BlogPost BlogPost { get; set; }
}
}
diff --git a/src/Web/Grand.Web/Controllers/BlogController.cs b/src/Web/Grand.Web/Controllers/BlogController.cs
index e5d559b20..6191b64f8 100644
--- a/src/Web/Grand.Web/Controllers/BlogController.cs
+++ b/src/Web/Grand.Web/Controllers/BlogController.cs
@@ -14,6 +14,7 @@ using Grand.Web.Events;
using Grand.Web.Features.Models.Blogs;
using Grand.Web.Models.Blogs;
using MediatR;
+using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Grand.Web.Controllers
@@ -26,9 +27,7 @@ namespace Grand.Web.Controllers
private readonly IBlogService _blogService;
private readonly ITranslationService _translationService;
private readonly IWorkContext _workContext;
- private readonly IGroupService _groupService;
private readonly BlogSettings _blogSettings;
- private readonly CaptchaSettings _captchaSettings;
#endregion
@@ -38,17 +37,13 @@ namespace Grand.Web.Controllers
IMediator mediator,
IBlogService blogService,
ITranslationService translationService,
- IGroupService groupService,
IWorkContext workContext,
- BlogSettings blogSettings,
- CaptchaSettings captchaSettings)
+ BlogSettings blogSettings)
{
_mediator = mediator;
_blogService = blogService;
_translationService = translationService;
_blogSettings = blogSettings;
- _captchaSettings = captchaSettings;
- _groupService = groupService;
_workContext = workContext;
}
@@ -57,6 +52,7 @@ namespace Grand.Web.Controllers
#region Methods
[HttpGet]
+ [ProducesResponseType(typeof(BlogPostListModel), StatusCodes.Status200OK)]
public virtual async Task<IActionResult> List(BlogPagingFilteringModel command)
{
if (!_blogSettings.Enabled)
@@ -66,6 +62,7 @@ namespace Grand.Web.Controllers
return View("List", model);
}
[HttpGet]
+ [ProducesResponseType(typeof(BlogPostListModel), StatusCodes.Status200OK)]
public virtual async Task<IActionResult> BlogByTag(BlogPagingFilteringModel command)
{
if (!_blogSettings.Enabled)
@@ -75,6 +72,7 @@ namespace Grand.Web.Controllers
return View("List", model);
}
[HttpGet]
+ [ProducesResponseType(typeof(BlogPostListModel), StatusCodes.Status200OK)]
public virtual async Task<IActionResult> BlogByMonth(BlogPagingFilteringModel command)
{
if (!_blogSettings.Enabled)
@@ -84,6 +82,7 @@ namespace Grand.Web.Controllers
return View("List", model);
}
[HttpGet]
+ [ProducesResponseType(typeof(BlogPostListModel), StatusCodes.Status200OK)]
public virtual async Task<IActionResult> BlogByCategory(BlogPagingFilteringModel command)
{
if (!_blogSettings.Enabled)
@@ -93,6 +92,7 @@ namespace Grand.Web.Controllers
return View("List", model);
}
[HttpGet]
+ [ProducesResponseType(typeof(BlogPostListModel), StatusCodes.Status200OK)]
public virtual async Task<IActionResult> BlogByKeyword(BlogPagingFilteringModel command)
{
if (!_blogSettings.Enabled)
@@ -102,6 +102,7 @@ namespace Grand.Web.Controllers
return View("List", model);
}
[HttpGet]
+ [ProducesResponseType(typeof(BlogPostModel), StatusCodes.Status200OK)]
public virtual async Task<IActionResult> BlogPost(string blogPostId,
[FromServices] IAclService aclService,
[FromServices] IPermissionService permissionService)
@@ -131,33 +132,43 @@ namespace Grand.Web.Controllers
[HttpPost]
[AutoValidateAntiforgeryToken]
[DenySystemAccount]
- public virtual async Task<IActionResult> BlogPost(BlogPostModel model,
+ [ProducesResponseType(typeof(AddBlogCommentModel), StatusCodes.Status200OK)]
+ public virtual async Task<IActionResult> BlogPost(AddBlogCommentModel model,
[FromServices] IAclService aclService)
{
- var blogPost = await _blogService.GetBlogPostById(model.BlogPostId);
+ var blogPost = await _blogService.GetBlogPostById(model.Id);
if (blogPost == null ||
(blogPost.StartDateUtc.HasValue && blogPost.StartDateUtc.Value >= DateTime.UtcNow) ||
(blogPost.EndDateUtc.HasValue && blogPost.EndDateUtc.Value <= DateTime.UtcNow))
- return RedirectToRoute("HomePage");
+ return Json(new
+ {
+ success = false
+ });
if (!aclService.Authorize(blogPost, _workContext.CurrentStore.Id))
- return InvokeHttp404();
+ return Json(new
+ {
+ success = false
+ });
if (ModelState.IsValid)
{
await _mediator.Send(new InsertBlogCommentCommand { Model = model, BlogPost = blogPost });
//notification
- await _mediator.Publish(new BlogCommentEvent(blogPost, model.AddNewComment));
+ await _mediator.Publish(new BlogCommentEvent(blogPost, model));
- //The text boxes should be cleared after a comment has been posted
- TempData["Grand.blog.addcomment.result"] = _translationService.GetResource("Blog.Comments.SuccessfullyAdded");
- return RedirectToRoute("BlogPost", new { SeName = blogPost.GetSeName(_workContext.WorkingLanguage.Id) });
+ return Json(new
+ {
+ success = true
+ });
}
-
- //If we got this far, something failed, redisplay form
- model = await _mediator.Send(new GetBlogPost { BlogPost = blogPost });
- return View(model);
+ return Json(new
+ {
+ success = false,
+ message = string.Join(',', ModelState.Values.SelectMany(x => x.Errors.Select(x => x.ErrorMessage)))
+ });
+
}
#endregion
}
diff --git a/src/Web/Grand.Web/Models/Blogs/AddBlogCommentModel.cs b/src/Web/Grand.Web/Models/Blogs/AddBlogCommentModel.cs
index 7e30ed1dd..91926d1a3 100644
--- a/src/Web/Grand.Web/Models/Blogs/AddBlogCommentModel.cs
+++ b/src/Web/Grand.Web/Models/Blogs/AddBlogCommentModel.cs
@@ -1,5 +1,6 @@
using Grand.Infrastructure.ModelBinding;
using Grand.Infrastructure.Models;
+using Grand.Web.Models.Common;
namespace Grand.Web.Models.Blogs
{
@@ -9,5 +10,7 @@ namespace Grand.Web.Models.Blogs
public string CommentText { get; set; }
public bool DisplayCaptcha { get; set; }
+
+ public ICaptchaValidModel Captcha { get; set; } = new CaptchaModel();
}
}
\ No newline at end of file
diff --git a/src/Web/Grand.Web/Models/Blogs/BlogPostModel.cs b/src/Web/Grand.Web/Models/Blogs/BlogPostModel.cs
index 4e7f55f1b..fc42cb2a8 100644
--- a/src/Web/Grand.Web/Models/Blogs/BlogPostModel.cs
+++ b/src/Web/Grand.Web/Models/Blogs/BlogPostModel.cs
@@ -6,7 +6,6 @@ namespace Grand.Web.Models.Blogs
{
public class BlogPostModel : BaseEntityModel
{
-
public BlogPostModel()
{
Tags = new List<string>();
@@ -14,13 +13,10 @@ namespace Grand.Web.Models.Blogs
AddNewComment = new AddBlogCommentModel();
PictureModel = new PictureModel();
}
-
- public string BlogPostId { get; set; }
public string MetaKeywords { get; set; }
public string MetaDescription { get; set; }
public string MetaTitle { get; set; }
public string SeName { get; set; }
-
public string Title { get; set; }
public PictureModel PictureModel { get; set; }
public string Body { get; set; }
@@ -28,13 +24,9 @@ namespace Grand.Web.Models.Blogs
public bool AllowComments { get; set; }
public int NumberOfComments { get; set; }
public DateTime CreatedOn { get; set; }
-
public IList<string> Tags { get; set; }
-
public IList<BlogCommentModel> Comments { get; set; }
public AddBlogCommentModel AddNewComment { get; set; }
- public ICaptchaValidModel Captcha { get; set; } = new CaptchaModel();
-
}
}
\ No newline at end of file
diff --git a/src/Web/Grand.Web/Validators/Blogs/AddBlogCommentValidator.cs b/src/Web/Grand.Web/Validators/Blogs/AddBlogCommentValidator.cs
index fda110a63..7d3bd066b 100644
--- a/src/Web/Grand.Web/Validators/Blogs/AddBlogCommentValidator.cs
+++ b/src/Web/Grand.Web/Validators/Blogs/AddBlogCommentValidator.cs
@@ -1,7 +1,16 @@
using FluentValidation;
+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.Security;
+using Grand.Domain.Blogs;
+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.Blogs;
+using Microsoft.AspNetCore.Http;
namespace Grand.Web.Validators.Blogs;
@@ -9,10 +18,47 @@ public class AddBlogCommentValidator : BaseGrandValidator<AddBlogCommentModel>
{
public AddBlogCommentValidator(
IEnumerable<IValidatorConsumer<AddBlogCommentModel>> validators,
+ IEnumerable<IValidatorConsumer<ICaptchaValidModel>> validatorsCaptcha,
+ CaptchaSettings captchaSettings, IHttpContextAccessor contextAccessor, GoogleReCaptchaValidator googleReCaptchaValidator,
+ BlogSettings blogSettings,
+ IGroupService groupService, IWorkContext workContext, IBlogService blogService, IAclService aclService,
ITranslationService translationService)
: base(validators)
{
RuleFor(x => x.CommentText).NotEmpty()
.WithMessage(translationService.GetResource("Blog.Comments.CommentText.Required"));
+
+ RuleFor(x => x).CustomAsync(async (x, context, _) =>
+ {
+ if (await groupService.IsGuest(workContext.CurrentCustomer) &&
+ !blogSettings.AllowNotRegisteredUsersToLeaveComments)
+ {
+ context.AddFailure(
+ translationService.GetResource("Blog.Comments.OnlyRegisteredUsersLeaveComments"));
+ }
+
+ if (!blogSettings.Enabled)
+ context.AddFailure(
+ translationService.GetResource("Blog.Disabled"));
+
+ var blogPost = await blogService.GetBlogPostById(x.Id);
+ if (blogPost is not { AllowComments: true })
+ context.AddFailure(translationService.GetResource("Blog.Comments.NotAllowed"));
+
+ if (!aclService.Authorize(blogPost, workContext.CurrentStore.Id))
+ context.AddFailure(translationService.GetResource("Blog.Comments.NotAllowed"));
+
+ if (blogPost == null ||
+ (blogPost.StartDateUtc.HasValue && blogPost.StartDateUtc.Value >= DateTime.UtcNow) ||
+ (blogPost.EndDateUtc.HasValue && blogPost.EndDateUtc.Value <= DateTime.UtcNow))
+ context.AddFailure(translationService.GetResource("Blog.Comments.NotAllowed"));
+ });
+
+ if (captchaSettings.Enabled && captchaSettings.ShowOnBlogCommentPage)
+ {
+ 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 file
diff --git a/src/Web/Grand.Web/Validators/Blogs/BlogPostValidator.cs b/src/Web/Grand.Web/Validators/Blogs/BlogPostValidator.cs
deleted file mode 100644
index d89c562ca..000000000
--- a/src/Web/Grand.Web/Validators/Blogs/BlogPostValidator.cs
+++ /dev/null
@@ -1,64 +0,0 @@
-using FluentValidation;
-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.Security;
-using Grand.Domain.Blogs;
-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.Blogs;
-using Microsoft.AspNetCore.Http;
-
-namespace Grand.Web.Validators.Blogs;
-
-public class BlogPostValidator : BaseGrandValidator<BlogPostModel>
-{
- public BlogPostValidator(
- IEnumerable<IValidatorConsumer<BlogPostModel>> validators,
- IEnumerable<IValidatorConsumer<ICaptchaValidModel>> validatorsCaptcha,
- CaptchaSettings captchaSettings, BlogSettings blogSettings,
- IGroupService groupService, IWorkContext workContext, IBlogService blogService, IAclService aclService,
- IHttpContextAccessor contextAccessor, GoogleReCaptchaValidator googleReCaptchaValidator,
- ITranslationService translationService)
- : base(validators)
- {
- RuleFor(x => x.AddNewComment.CommentText).NotEmpty()
- .WithMessage(translationService.GetResource("Blog.Comments.CommentText.Required"));
-
- RuleFor(x => x).CustomAsync(async (x, context, _) =>
- {
- if (await groupService.IsGuest(workContext.CurrentCustomer) &&
- !blogSettings.AllowNotRegisteredUsersToLeaveComments)
- {
- context.AddFailure(
- translationService.GetResource("Blog.Comments.OnlyRegisteredUsersLeaveComments"));
- }
-
- if (!blogSettings.Enabled)
- context.AddFailure(
- translationService.GetResource("Blog.Disabled"));
-
- var blogPost = await blogService.GetBlogPostById(x.BlogPostId);
- if (blogPost is not { AllowComments: true })
- context.AddFailure(translationService.GetResource("Blog.Comments.NotAllowed"));
-
- if (!aclService.Authorize(blogPost, workContext.CurrentStore.Id))
- context.AddFailure(translationService.GetResource("Blog.Comments.NotAllowed"));
-
- if (blogPost == null ||
- (blogPost.StartDateUtc.HasValue && blogPost.StartDateUtc.Value >= DateTime.UtcNow) ||
- (blogPost.EndDateUtc.HasValue && blogPost.EndDateUtc.Value <= DateTime.UtcNow))
- context.AddFailure(translationService.GetResource("Blog.Comments.NotAllowed"));
- });
-
- if (captchaSettings.Enabled && captchaSettings.ShowOnBlogCommentPage)
- {
- 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 file
diff --git a/src/Web/Grand.Web/Views/Blog/BlogPost.cshtml b/src/Web/Grand.Web/Views/Blog/BlogPost.cshtml
index d3fcc7a4b..7f2854d08 100644
--- a/src/Web/Grand.Web/Views/Blog/BlogPost.cshtml
+++ b/src/Web/Grand.Web/Views/Blog/BlogPost.cshtml
@@ -70,16 +70,17 @@
}
</div>
<validation-observer v-slot="{ handleSubmit }">
- <form asp-controller="Blog" asp-action="BlogPost" asp-route-id="@Model.Id" method="post" ref="form" v-on:submit.prevent="handleSubmit(formSubmit)">
- <input type="hidden" name="blogPostId" value="@Model.Id" />
+ <form id="addBlogComment" asp-controller="Blog" asp-action="BlogPost" asp-route-id="@Model.Id" method="post" v-on:submit.prevent="handleSubmit(blog.submitBlogComment)">
+ <input type="hidden" name="Id" value="@Model.Id" />
<div class="form-fields">
<div class="form-group">
<validation-provider tag="div" rules="required" v-slot="{ errors, classes }">
- <label asp-for="AddNewComment.CommentText" class="col-form-label">@Loc["Blog.Comments.CommentText"]:</label>
+ <label for="CommentText" class="col-form-label">@Loc["Blog.Comments.CommentText"]:</label>
<span class="required">*</span>
- <textarea asp-for="AddNewComment.CommentText" v-model="blog.CommentText" v-bind:class="[classes , 'form-control']" autofocus="autofocus" rows="4"></textarea>
+ <textarea name="CommentText" v-model="blog.CommentText"
+ data-val-required="Enter comment"
+ v-bind:class="[classes , 'form-control']" autofocus="autofocus" rows="4"></textarea>
<span class="field-validation-error">{{ errors[0] }}</span>
- <span asp-validation-for="AddNewComment.CommentText"></span>
</validation-provider>
</div>
@if (Model.AddNewComment.DisplayCaptcha)
@@ -102,7 +103,27 @@
CommentText: '',
</text>
}
- })
+ }),
+ methods: {
+ submitBlogComment() {
+ var form = document.getElementById("addBlogComment");
+ var url = form.getAttribute("action");
+ var bodyFormData = new FormData(form);
+ axios({
+ url: url,
+ data: bodyFormData,
+ method: 'post',
+ headers: {
+ 'Accept': 'application/json'
+ }
+ }).then(function (response) {
+ if (response.data.success == true){
+ location.reload();
+ }
+ vm.displayBarNotification(response.data.message, '', 'error', 3500);
+ })
+ }
+ }
});
</script>
</div>