113 lines
3.1 KiB
C#
113 lines
3.1 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using Api.Models;
|
|
using HN.Application;
|
|
using HN.Domain;
|
|
using MediatR;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace Api.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public sealed class LinksController : ControllerBase
|
|
{
|
|
private readonly IMediator _bus;
|
|
|
|
public LinksController(IMediator bus)
|
|
{
|
|
_bus = bus;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Récupère tous les liens postés.
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[ProducesResponseType(typeof(LinkDto[]), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[AllowAnonymous]
|
|
public async Task<IActionResult> GetLinks()
|
|
{
|
|
return Ok(await _bus.Send(new ListLinksQuery()));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Récupère um lien particulier.
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
[HttpGet("{id}")]
|
|
[AllowAnonymous]
|
|
public async Task<ActionResult<LinkDto>> GetLinkById(Guid id)
|
|
{
|
|
return Ok(await _bus.Send(new GetLinkQuery(id)));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Upvote un lien particulier.
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
[HttpPut("{id}/upvote")]
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
public async Task<IActionResult> Upvote(Guid id)
|
|
{
|
|
await _bus.Send(new VoteForLinkCommand(id, VoteType.Up));
|
|
return NoContent();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Downvote un lien particulier.
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
[HttpPut("{id}/downvote")]
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
public async Task<IActionResult> Downvote(Guid id)
|
|
{
|
|
await _bus.Send(new VoteForLinkCommand(id, VoteType.Down));
|
|
return NoContent();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Récupère les commentaires d'un lien particulier.
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
[HttpGet("{id}/comments")]
|
|
[AllowAnonymous]
|
|
public async Task<ActionResult<CommentDto[]>> Comments(Guid id)
|
|
{
|
|
return Ok(await _bus.Send(new GetLinkCommentsQuery(id)));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Poste un nouveau commentaire sur un lien.
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="command"></param>
|
|
/// <returns></returns>
|
|
[HttpPost("{id}/comments")]
|
|
[ProducesResponseType(StatusCodes.Status201Created)]
|
|
public async Task<IActionResult> AddComment(Guid id, AddCommentViewModel command)
|
|
{
|
|
var commentId = await _bus.Send(new CommentLinkCommand(id, command.Content));
|
|
return Created($"comments/{commentId}", null);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Poste un nouveau lien.
|
|
/// </summary>
|
|
/// <param name="command"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
[ProducesResponseType(StatusCodes.Status201Created)]
|
|
public async Task<IActionResult> CreateLink(AddLinkCommand command)
|
|
{
|
|
var result = await _bus.Send(command);
|
|
|
|
return CreatedAtAction(nameof(GetLinkById), new { id = result }, null);
|
|
}
|
|
}
|
|
} |