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/comments")] public class CommentsController : ControllerBase { private readonly IMediator _bus; public CommentsController(IMediator bus) { _bus = bus; } /// /// Permet de poster un commentaire sur un lien. /// /// /// [HttpPost] [Authorize] [ProducesResponseType(StatusCodes.Status201Created)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task CreateComment(CommentLinkCommand command) { var commentId = await _bus.Send(command); return CreatedAtAction(nameof(GetById), new { id = commentId }, null); } [HttpGet("{id:guid}")] [ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(typeof(CommentDto), StatusCodes.Status200OK)] public async Task GetById(Guid id) { return await _bus.Send(new GetCommentByIdQuery(id)); } /// /// Permet d'upvoter un commentaire. /// /// /// [HttpPut("{id:guid}/upvote")] [Authorize] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task Upvote(Guid id) { await _bus.Send(new VoteForCommentCommand { CommentId = id, Direction = MyHN.Domain.VoteType.Up, }); return NoContent(); } /// /// Permet de downvoter un commentaire. /// /// /// [HttpPut("{id:guid}/downvote")] [Authorize] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task Downvote(Guid id) { await _bus.Send(new VoteForCommentCommand { CommentId = id, Direction = MyHN.Domain.VoteType.Down, }); return NoContent(); } } }