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 Index() { return View(await _bus.Send(new ListLinksQuery())); } [HttpGet("{controller}/{id:guid}")] public async Task Show(Guid id) { return View(new ShowLinkViewModel(await _bus.Send(new GetLinkQuery(id)), new CommentLinkCommand(id))); } public IActionResult Create() { return View(new AddLinkCommand()); } [HttpPost("{controller}/{id:guid}/vote")] [ValidateAntiForgeryToken] public async Task 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 Create(AddLinkCommand command) { if (!ModelState.IsValid) { return View(command); } await _bus.Send(command); SetFlash("Link added!"); return RedirectToAction(nameof(Index)); } } }