hn-dotnet/Apps/Website/Controllers/CommentsController.cs

49 lines
1.2 KiB
C#

using HN.Application;
using Website.Models;
using Microsoft.AspNetCore.Mvc;
using MediatR;
using System.Threading.Tasks;
using HN.Domain;
using System;
namespace Website.Controllers
{
public sealed class CommentsController : BaseController
{
private readonly IMediator _bus;
public CommentsController(IMediator bus)
{
_bus = bus;
}
[ValidateAntiForgeryToken]
[HttpPost]
public async Task<IActionResult> Create(CommentLinkCommand command)
{
if (!ModelState.IsValid)
{
var link = await _bus.Send(new GetLinkQuery(command.LinkId));
var comments = await _bus.Send(new GetLinkCommentsQuery(command.LinkId));
return View("../Links/Show", new ShowLinkViewModel(link, command, comments));
}
await _bus.Send(command);
SetFlash("Comment added!");
return RedirectToAction(nameof(LinksController.Show), "Links", new { id = command.LinkId });
}
[HttpPost("{controller}/{id:guid}/vote")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Vote(Guid id, VoteType type, string redirectTo)
{
await _bus.Send(new VoteForCommentCommand(id, type));
SetFlash($"Comment {type} added!");
return Redirect(redirectTo);
}
}
}