From ee25a019467d8217f2fab5a1e8d2f2f3714c11a6 Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Sun, 15 Dec 2024 20:48:04 +0000 Subject: [PATCH] Blog comment comments --- .../Accounting/ManageController.cs | 6 +- .../Communicating/BlogspotController.cs | 3 +- ...Resources.CommentViewComponent.Designer.cs | 94 ------------------- .../ViewComponents/CommentViewComponent.cs | 26 +++-- src/Yavsc/Views/Blogspot/Details.cshtml | 5 +- src/Yavsc/Views/Blogspot/Index.cshtml | 2 +- .../Shared/Components/Comment/BlogPost.cshtml | 1 - .../Comment/Default.cshtml} | 6 +- src/Yavsc/Views/Shared/_Layout.cshtml | 3 - src/Yavsc/wwwroot/js/comment.js | 10 +- 10 files changed, 39 insertions(+), 117 deletions(-) delete mode 100644 src/Yavsc/Resources/Yavsc.Resources.CommentViewComponent.Designer.cs delete mode 100644 src/Yavsc/Views/Shared/Components/Comment/BlogPost.cshtml rename src/Yavsc/Views/Shared/{DisplayTemplates/Comment.cshtml => Components/Comment/Default.cshtml} (61%) diff --git a/src/Yavsc/Controllers/Accounting/ManageController.cs b/src/Yavsc/Controllers/Accounting/ManageController.cs index ab12234d..24c3f8fa 100644 --- a/src/Yavsc/Controllers/Accounting/ManageController.cs +++ b/src/Yavsc/Controllers/Accounting/ManageController.cs @@ -15,10 +15,11 @@ using Yavsc.Models; using Yavsc.Services; using Yavsc.ViewModels.Manage; using Microsoft.AspNetCore.Identity.UI.Services; +using Microsoft.AspNetCore.Authorization; namespace Yavsc.Controllers { - + [Authorize] public class ManageController : Controller { private readonly UserManager _userManager; @@ -91,7 +92,10 @@ namespace Yavsc.Controllers : ""; var user = await GetCurrentUserAsync(); + long pc = _dbContext.Blogspot.Count(x => x.AuthorId == user.Id); + + var model = new IndexViewModel { diff --git a/src/Yavsc/Controllers/Communicating/BlogspotController.cs b/src/Yavsc/Controllers/Communicating/BlogspotController.cs index 1aadee04..4c4e2cf9 100644 --- a/src/Yavsc/Controllers/Communicating/BlogspotController.cs +++ b/src/Yavsc/Controllers/Communicating/BlogspotController.cs @@ -228,7 +228,8 @@ namespace Yavsc.Controllers [ValidateAntiForgeryToken] public IActionResult DeleteConfirmed(long id) { - BlogPost blog = _context.Blogspot.Single(m => m.Id == id && m.GetOwnerId()== User.GetUserId()); + var uid = User.GetUserId(); + BlogPost blog = _context.Blogspot.Single(m => m.Id == id && m.AuthorId == uid ); _context.Blogspot.Remove(blog); _context.SaveChanges(User.GetUserId()); diff --git a/src/Yavsc/Resources/Yavsc.Resources.CommentViewComponent.Designer.cs b/src/Yavsc/Resources/Yavsc.Resources.CommentViewComponent.Designer.cs deleted file mode 100644 index f62f8775..00000000 --- a/src/Yavsc/Resources/Yavsc.Resources.CommentViewComponent.Designer.cs +++ /dev/null @@ -1,94 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by a tool. -// Runtime Version:4.0.30319.42000 -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -namespace Yavsc.ViewComponents { - using System; - using System.Reflection; - - - [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] - [System.Diagnostics.DebuggerNonUserCodeAttribute()] - [System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - public partial class CommentViewComponent { - - private static System.Resources.ResourceManager resourceMan; - - private static System.Globalization.CultureInfo resourceCulture; - - [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] - public static System.Resources.ResourceManager ResourceManager { - get { - if (object.Equals(null, resourceMan)) { - System.Resources.ResourceManager temp = new System.Resources.ResourceManager(("Yavsc.Resources." + "Yavsc.ViewComponents.CommentViewComponent"), typeof(CommentViewComponent).GetTypeInfo().Assembly); - resourceMan = temp; - } - return resourceMan; - } - } - - [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] - public static System.Globalization.CultureInfo Culture { - get { - return resourceCulture; - } - set { - resourceCulture = value; - } - } - - public static string apiRouteCommentBlogPost { - get { - return ResourceManager.GetString("apiRouteCommentBlogPost", resourceCulture); - } - } - - public static string CommmentId { - get { - return ResourceManager.GetString("CommmentId", resourceCulture); - } - } - - public static string Comment { - get { - return ResourceManager.GetString("Comment", resourceCulture); - } - } - - public static string LastModificationDate { - get { - return ResourceManager.GetString("LastModificationDate", resourceCulture); - } - } - - public static string CreationDate { - get { - return ResourceManager.GetString("CreationDate", resourceCulture); - } - } - - public static string PostId { - get { - return ResourceManager.GetString("PostId", resourceCulture); - } - } - - public static string UserCreated { - get { - return ResourceManager.GetString("UserCreated", resourceCulture); - } - } - - public static string UserModified { - get { - return ResourceManager.GetString("UserModified", resourceCulture); - } - } - } -} diff --git a/src/Yavsc/ViewComponents/CommentViewComponent.cs b/src/Yavsc/ViewComponents/CommentViewComponent.cs index 1c2b63b6..17fc0fd7 100644 --- a/src/Yavsc/ViewComponents/CommentViewComponent.cs +++ b/src/Yavsc/ViewComponents/CommentViewComponent.cs @@ -1,21 +1,35 @@ +using System.Diagnostics; using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Localization; +using Yavsc.Models; using Yavsc.Models.Blog; namespace Yavsc.ViewComponents { - public partial class CommentViewComponent : ViewComponent + public class CommentViewComponent : ViewComponent { + private readonly ApplicationDbContext context; private readonly IStringLocalizer localizer; - public CommentViewComponent(IStringLocalizer localizer) + public CommentViewComponent(IStringLocalizer localizer, + ApplicationDbContext context + ) { + this.context = context; this.localizer = localizer; } - public IViewComponentResult Invoke(IIdentified longCommentable) + public async Task InvokeAsync(long id) { - // for a BlogPost, it results in the localization 'apiRouteCommentBlogPost': blogcomments - ViewData["apictlr"] = "/api/"+localizer["apiRouteComment"+longCommentable.GetType().Name]; - return View(longCommentable.GetType().Name, new Comment{ ReceiverId = longCommentable.Id }); + var comment = await context.Comment.Include(c=>c.Children).FirstOrDefaultAsync(c => c.Id==id); + if (comment == null) + throw new InvalidOperationException(); + ViewData["apictlr"] = "/api/blogcomments"; + return View("Default", comment); + } + + private string GetDebuggerDisplay() + { + return ToString(); } } } diff --git a/src/Yavsc/Views/Blogspot/Details.cshtml b/src/Yavsc/Views/Blogspot/Details.cshtml index da8cd242..4d884a29 100644 --- a/src/Yavsc/Views/Blogspot/Details.cshtml +++ b/src/Yavsc/Views/Blogspot/Details.cshtml @@ -65,13 +65,14 @@ $('#commentValidation').html(

@Model.Title

@Html.DisplayFor(m=>m.Author) - @Html.DisplayFor(m=>m.Content) + @Html.DisplayFor(m=>m.Content) +
@if (Model.Comments!=null) { foreach (var comment in Model.Comments.Where(c=>c.ParentId==null)) { - @Html.DisplayFor(model=>comment,"Comment","Comment") + @await Component.InvokeAsync("Comment", new { id = comment.Id }) } }
diff --git a/src/Yavsc/Views/Blogspot/Index.cshtml b/src/Yavsc/Views/Blogspot/Index.cshtml index e3f8d930..e5b8134c 100644 --- a/src/Yavsc/Views/Blogspot/Index.cshtml +++ b/src/Yavsc/Views/Blogspot/Index.cshtml @@ -45,5 +45,5 @@ }
- @await Component.InvokeAsync("BlogIndex",new{ id = User.GetUserId() ?? "_anonymous_" }) + @await Component.InvokeAsync("BlogIndex",new{ viewerId = User.GetUserId() ?? "_anonymous_" })
diff --git a/src/Yavsc/Views/Shared/Components/Comment/BlogPost.cshtml b/src/Yavsc/Views/Shared/Components/Comment/BlogPost.cshtml deleted file mode 100644 index 10735153..00000000 --- a/src/Yavsc/Views/Shared/Components/Comment/BlogPost.cshtml +++ /dev/null @@ -1 +0,0 @@ -@model Comment diff --git a/src/Yavsc/Views/Shared/DisplayTemplates/Comment.cshtml b/src/Yavsc/Views/Shared/Components/Comment/Default.cshtml similarity index 61% rename from src/Yavsc/Views/Shared/DisplayTemplates/Comment.cshtml rename to src/Yavsc/Views/Shared/Components/Comment/Default.cshtml index c07cc48d..d4ff0307 100644 --- a/src/Yavsc/Views/Shared/DisplayTemplates/Comment.cshtml +++ b/src/Yavsc/Views/Shared/Components/Comment/Default.cshtml @@ -1,6 +1,6 @@ @model Comment -
@Model.Content @@ -8,9 +8,9 @@
@foreach (var comment in Model.Children) { - Html.DisplayFor(model=>comment,"Comment","Comment"); + @await this.Component.InvokeAsync("Comment", new { id = comment.Id}); }
} -
+ diff --git a/src/Yavsc/Views/Shared/_Layout.cshtml b/src/Yavsc/Views/Shared/_Layout.cshtml index 25b75fe3..cbd3c27b 100644 --- a/src/Yavsc/Views/Shared/_Layout.cshtml +++ b/src/Yavsc/Views/Shared/_Layout.cshtml @@ -17,7 +17,6 @@ @await RenderSectionAsync("header", false) -
@RenderSection("ctxmenu", required: false) @if (ViewData["Notify"] != null) @@ -37,13 +36,11 @@
@RenderBody()
-
- @await RenderSectionAsync("scripts", false) diff --git a/src/Yavsc/wwwroot/js/comment.js b/src/Yavsc/wwwroot/js/comment.js index c7a75afc..a302ff44 100644 --- a/src/Yavsc/wwwroot/js/comment.js +++ b/src/Yavsc/wwwroot/js/comment.js @@ -107,13 +107,13 @@ $.widget("psc.blogcomment", { }); }, doCoC: function(_this, ev) { - var postid = $('#cmtBtn').data('receiverid'); + var postId = this.element.data('receiver-id'); var comment = _this.cmtInput.val(); - var cmtid = $(_this.element).data("id"); + var cmtId = $(_this.element).data("id"); var data = { Content: comment, - ReceiverId: postid, - ParentId: cmtid, + ReceiverId: postId, + ParentId: cmtId, AuthorId: _this.options.authorId }; @@ -136,7 +136,7 @@ $.widget("psc.blogcomment", { success: function(data) { _this.cmtInput.val(''); $('span.field-validation-valid[data-valmsg-for="Content"]').empty(); - $('
' + data.Content + '
') + $('
' + data.content + '
') .blogcomment().appendTo(_this.subCmts); }, url: _this.options.apictrlr