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 Created($"comments/{commentId}", null);
}
///
/// 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();
}
[HttpGet("me")]
public IActionResult Me()
{
return Ok(new
{
UserId = User.Identity.Name,
Authenticated = User.Identity.IsAuthenticated
});
}
}
}