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

65 lines
1.5 KiB
C#

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