using System.Net;
using Dpz.Core.EnumLibrary;
using Dpz.Core.Hangfire;
using Dpz.Core.Web.Models;
using Hangfire;
using Microsoft.AspNetCore.Mvc.ModelBinding;
namespace Dpz.Core.Web.Controllers;
public class CommentController(ICommentService commentService) : Controller
{
[HttpGet("Comment/{node}/{relation}")]
public async Task<IActionResult> Index(CommentNode node, string relation, int pageIndex = 1, int pageSize = 5)
{
relation = WebUtility.UrlDecode(relation);
ViewData["node"] = node;
ViewData["relation"] = relation;
var list = await commentService.GetCommentsAsync(node, relation, pageIndex, pageSize);
var count = await commentService.GetCommentCountAsync(node, relation);
var model = new CommentPage { Page = list, Count = count };
Response.Headers.Append("CommentCount", count.ToString());
return PartialView(model);
}
[HttpGet("Comment/Page/{node}/{relation}")]
public async Task<IActionResult> Page(CommentNode node, string relation, int pageIndex = 1, int pageSize = 5)
{
relation = WebUtility.UrlDecode(relation);
ViewData["node"] = node;
ViewData["relation"] = relation;
var list = await commentService.GetCommentsAsync(node, relation, pageIndex, pageSize);
var count = await commentService.GetCommentCountAsync(node, relation);
var model = new CommentPage { Page = list, Count = count };
Response.Headers.Append("CommentCount", count.ToString());
return PartialView("_CommentListPartial", model);
}
[HttpPost, ValidateAntiForgeryToken]
public async Task<IActionResult> Publish(VmPublishComment comment)
{
if (!ModelState.IsValid)
{
var errors = ModelState
.SelectMany(x => x.Value?.Errors ?? new ModelErrorCollection())
.Select(x => x.ErrorMessage)
.Where(x => !string.IsNullOrEmpty(x))
.ToArray();
return Json(new ResultInfo(string.Join("\n", errors)));
}
await commentService.PublishCommentAsync(comment);
var list = await commentService.GetCommentsAsync(comment.Node, comment.Relation, 1, 5);
var count = await commentService.GetCommentCountAsync(comment.Node, comment.Relation);
var model = new CommentPage { Page = list, Count = count };
ViewData["node"] = comment.Node;
ViewData["relation"] = comment.Relation;
SetCookie(comment.NickName, comment.Email, comment.Site);
BackgroundJob.Enqueue<EmailSenderActivator>(x => x.SendMasterAsync(comment));
if (!string.IsNullOrEmpty(comment.ReplyId))
{
BackgroundJob.Enqueue<EmailSenderActivator>(x => x.SendCommenterAsync(comment));
}
ViewData["node"] = comment.Node;
ViewData["relation"] = comment.Relation;
Response.Headers.Append("CommentCount", count.ToString());
return PartialView("_CommentListPartial", model);
}
[NonAction]
private void SetCookie(string nickname, string email, string? site)
{
var cookieNickname = Request.Cookies[nameof(nickname)];
if (string.IsNullOrEmpty(cookieNickname) || WebUtility.UrlDecode(cookieNickname) != nickname)
{
Response.Cookies.Append(nameof(nickname), WebUtility.UrlEncode(nickname),
new CookieOptions
{ HttpOnly = true, Secure = true, IsEssential = true, Expires = DateTimeOffset.Now.AddYears(1) });
}
var cookieEmail = Request.Cookies[nameof(email)];
if (string.IsNullOrEmpty(cookieEmail) || WebUtility.UrlDecode(cookieEmail) != email)
{
Response.Cookies.Append(nameof(email), WebUtility.UrlEncode(email),
new CookieOptions
{ HttpOnly = true, Secure = true, IsEssential = true, Expires = DateTimeOffset.Now.AddYears(1) });
}
var cookieSite = Request.Cookies[nameof(site)];
if (!string.IsNullOrEmpty(site) &&
(string.IsNullOrEmpty(cookieSite) || WebUtility.UrlDecode(cookieSite) != site))
{
Response.Cookies.Append(nameof(site), WebUtility.UrlEncode(site),
new CookieOptions
{ HttpOnly = true, Secure = true, IsEssential = true, Expires = DateTimeOffset.Now.AddYears(1) });
}
}
[HttpPost, ValidateAntiForgeryToken, CheckAuthorize]
public async Task<IActionResult> Send(MembleComment comment)
{
if (!ModelState.IsValid)
{
var errors = ModelState
.SelectMany(x => x.Value?.Errors ?? new ModelErrorCollection())
.Select(x => x.ErrorMessage)
.Where(x => !string.IsNullOrEmpty(x))
.ToArray();
return Json(new ResultInfo(string.Join("\n", errors)));
}
var user = User.GetIdentity();
comment.User = user;
await commentService.PublishCommentAsync(comment);
var list = await commentService.GetCommentsAsync(comment.Node, comment.Relation, 1, 5);
var count = await commentService.GetCommentCountAsync(comment.Node, comment.Relation);
var model = new CommentPage { Page = list, Count = count };
Response.Headers.Append("CommentCount", count.ToString());
if (!string.IsNullOrEmpty(comment.ReplyId))
{
BackgroundJob.Enqueue<EmailSenderActivator>(x => x.SendCommenterAsync(comment));
}
ViewData["node"] = comment.Node;
ViewData["relation"] = comment.Relation;
return PartialView("_CommentListPartial", model);
}
}