Blog comment comments

This commit is contained in:
Paul Schneider
2024-12-15 20:48:04 +00:00
parent d4d02b967d
commit ee25a01946
10 changed files with 39 additions and 117 deletions

View File

@ -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<ApplicationUser> _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
{

View File

@ -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());

View File

@ -1,94 +0,0 @@
//------------------------------------------------------------------------------
// <auto-generated>
// 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.
// </auto-generated>
//------------------------------------------------------------------------------
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);
}
}
}
}

View File

@ -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<CommentViewComponent> localizer;
public CommentViewComponent(IStringLocalizer<CommentViewComponent> localizer)
public CommentViewComponent(IStringLocalizer<CommentViewComponent> localizer,
ApplicationDbContext context
)
{
this.context = context;
this.localizer = localizer;
}
public IViewComponentResult Invoke(IIdentified<long> longCommentable)
public async Task<IViewComponentResult> 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();
}
}
}

View File

@ -65,13 +65,14 @@ $('#commentValidation').html(
<h1 class="blogtitle" ismarkdown>@Model.Title</h1>
<img class="blogphoto" alt="" src="@Model.Photo" >
@Html.DisplayFor(m=>m.Author)
@Html.DisplayFor(m=>m.Content)
<asciidoc>@Html.DisplayFor(m=>m.Content)</asciidoc>
</div>
<div id="comments">
@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 })
}
}
</div>

View File

@ -45,5 +45,5 @@
}
<div class="container">
@await Component.InvokeAsync("BlogIndex",new{ id = User.GetUserId() ?? "_anonymous_" })
@await Component.InvokeAsync("BlogIndex",new{ viewerId = User.GetUserId() ?? "_anonymous_" })
</div>

View File

@ -1 +0,0 @@
@model Comment

View File

@ -1,6 +1,6 @@
@model Comment
<div data-type="blogcomment" data-id="@Model.Id" data-allow-edit="@(User.GetUserId()==Model.AuthorId?"true":"false")"
<div data-type="blogcomment" data-id="@Model.Id" data-receiver-id="@Model.ReceiverId" data-allow-edit="@(User.GetUserId()==Model.AuthorId?"true":"false")"
data-allow-moderate="@ViewData["moderatoFlag"]" data-date="@Model.DateCreated.ToString("yyyy-MM-dd hh:mm:ss")" data-username="@Model.Author.UserName" >
<asciidoc>@Model.Content</asciidoc>
@ -8,9 +8,9 @@
<div class="subcomments">
@foreach (var comment in Model.Children)
{
Html.DisplayFor(model=>comment,"Comment","Comment");
@await this.Component.InvokeAsync("Comment", new { id = comment.Id});
}
</div>
}
</div>

View File

@ -17,7 +17,6 @@
@await RenderSectionAsync("header", false)
</head>
<body>
<main>
<partial name="_Nav" />
@RenderSection("ctxmenu", required: false)
@if (ViewData["Notify"] != null)
@ -37,13 +36,11 @@
<div class="container body-container">
@RenderBody()
</div>
</main>
<footer class="border-top footer text-muted">
<div class="container">
&copy; 2024 - Yavsc - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</div>
</footer>
@await RenderSectionAsync("scripts", false)

View File

@ -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();
$('<div data-type="blogcomment" data-id="' + data.Id + '" data-allow-edit="True" data-date="' + data.DateCreated + '" data-username="' + _this.options.authorName + '">' + data.Content + '</div>')
$('<div data-type="blogcomment" data-id="' + data.id + '" data-allow-edit="True" data-date="' + data.dateCreated + '" data-username="' + _this.options.authorName + '">' + data.content + '</div>')
.blogcomment().appendTo(_this.subCmts);
},
url: _this.options.apictrlr