using System;
using System.Threading.Tasks;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MyHN.Application;
namespace Api.Controllers
{
[ApiController]
[Route("api/links")]
public class LinksController : ControllerBase
{
private readonly IMediator _bus;
public LinksController(IMediator bus)
{
_bus = bus;
}
///
/// Récupère tous les liens publiés depuis la nuit des temps.
///
///
[HttpGet]
public async Task GetLinks()
{
return await _bus.Send(new GetLinksQuery());
}
///
/// Récupère un lien unitairement.
///
///
///
[HttpGet("{id:guid}")]
[ProducesResponseType(typeof(LinkDto), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task GetLinkById(Guid id)
{
return await _bus.Send(new GetLinkByIdQuery(id));
}
///
/// Récupère tous les commentaires d'un lien.
///
///
///
[HttpGet("{id:guid}/comments")]
public async Task GetLinkComments(Guid id)
{
return await _bus.Send(new GetCommentsByLinkQuery(id));
}
///
/// Permet la publication d'un nouveau lien sur la plateforme.
///
///
///
[HttpPost]
[Authorize]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status201Created)]
public async Task CreateLink(CreateLinkCommand command)
{
var linkId = await _bus.Send(command);
return CreatedAtAction(nameof(GetLinkById), new { id = linkId }, null);
}
///
/// Permet d'upvoter un lien.
///
///
///
[HttpPut("{id:guid}/upvote")]
[Authorize]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task Upvote(Guid id)
{
await _bus.Send(new VoteForLinkCommand
{
LinkId = id,
Direction = MyHN.Domain.VoteType.Up,
});
return NoContent();
}
///
/// Permet de downvoter un lien.
///
///
///
[HttpPut("{id:guid}/downvote")]
[Authorize]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task Downvote(Guid id)
{
await _bus.Send(new VoteForLinkCommand
{
LinkId = id,
Direction = MyHN.Domain.VoteType.Down,
});
return NoContent();
}
}
}