hn-dotnet/Apps/Website/Controllers/LinksController.cs
2020-12-10 12:49:06 +01:00

62 lines
1.3 KiB
C#

using Microsoft.AspNetCore.Mvc;
using HN.Application;
using MediatR;
using System.Threading.Tasks;
using System;
using HN.Domain;
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)
{
return View(await _bus.Send(new GetLinkQuery(id)));
}
public IActionResult Create()
{
return View(new AddLinkCommand());
}
[HttpPost("{controller}/{id:guid}/vote")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Vote(Guid id, string url, VoteType type)
{
await _bus.Send(new VoteForLinkCommand(id, type));
SetFlash($"Successfuly {type} for {url}!");
return RedirectToAction(nameof(Index));
}
[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));
}
}
}