hn-dotnet/Apps/Website/Controllers/LinksController.cs
Julien Leicher 3cd5133f66 add-aspnet-identity (#26)
add exception filter when user not connected
default to needing authentication and apply anonymous to some actions
add user in get requests
add user relation in link, comment and vote
signup and in are ok now!
2020-12-11 17:59:35 +01:00

69 lines
1.6 KiB
C#

using Microsoft.AspNetCore.Mvc;
using HN.Application;
using MediatR;
using System.Threading.Tasks;
using System;
using HN.Domain;
using Website.Models;
using Microsoft.AspNetCore.Authorization;
namespace Website.Controllers
{
public class LinksController : BaseController
{
private readonly IMediator _bus;
public LinksController(IMediator bus)
{
_bus = bus;
}
[HttpGet]
[AllowAnonymous]
public async Task<IActionResult> Index()
{
return View(await _bus.Send(new ListLinksQuery()));
}
[HttpGet("{controller}/{id:guid}")]
[AllowAnonymous]
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));
}
[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);
}
public IActionResult Create()
{
return View(new AddLinkCommand());
}
[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));
}
}
}