using System; using System.Threading.Tasks; using Api.Models; using HN.Application; using HN.Domain; using MediatR; 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; } /// /// Récupère tous les liens postés. /// /// [ProducesResponseType(typeof(LinkDto[]), StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] public async Task GetLinks() { return Ok(await _bus.Send(new ListLinksQuery())); } /// /// Récupère um lien particulier. /// /// [HttpGet("{id}")] public async Task> GetLinkById(Guid id) { return Ok(await _bus.Send(new GetLinkQuery(id))); } /// /// Upvote un lien particulier. /// /// /// [HttpPut("{id}/upvote")] [ProducesResponseType(StatusCodes.Status204NoContent)] public async Task Upvote(Guid id) { await _bus.Send(new VoteForLinkCommand(id, VoteType.Up)); return NoContent(); } /// /// Downvote un lien particulier. /// /// /// [HttpPut("{id}/downvote")] [ProducesResponseType(StatusCodes.Status204NoContent)] public async Task Downvote(Guid id) { await _bus.Send(new VoteForLinkCommand(id, VoteType.Down)); return NoContent(); } /// /// Récupère les commentaires d'un lien particulier. /// /// /// [HttpGet("{id}/comments")] public async Task> Comments(Guid id) { return Ok(await _bus.Send(new GetLinkCommentsQuery(id))); } /// /// Poste un nouveau commentaire sur un lien. /// /// /// /// [HttpPost("{id}/comments")] [ProducesResponseType(StatusCodes.Status201Created)] public async Task AddComment(Guid id, AddCommentViewModel command) { var commentId = await _bus.Send(new CommentLinkCommand(id, command.Content)); return CreatedAtAction("", "", new { id = commentId }, null); } /// /// Poste un nouveau lien. /// /// /// [HttpPost] public async Task CreateLink(AddLinkCommand command) { var result = await _bus.Send(command); return CreatedAtAction(nameof(GetLinkById), new { id = result }); } } }