using System.ComponentModel.DataAnnotations; using HackerNet.Application; using HackerNet.Infrastructure.AspNet.Filters; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace HackerNet.Api.Controllers; [ApiController] [Route("/api/links")] public class LinksController : ControllerBase { private readonly LinkService _linkService; public LinksController(LinkService linkService) { _linkService = linkService; } /// /// Retourne les derniers liens publiés de la plateforme. /// /// [HttpGet] public ActionResult GetLinks() { return _linkService.GetPublishedLinks(); } /// /// Récupère un lien par son identifiant. /// /// /// [HttpGet("{id:guid}")] // https://docs.microsoft.com/fr-fr/aspnet/core/fundamentals/routing?view=aspnetcore-6.0#route-constraint-reference [CustomExceptionFilter] [ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status200OK)] public ActionResult GetLinkDetail(Guid id) { return _linkService.GetLinkDetail(id); } /// /// Récupère les commentaires associés au lien. /// /// /// [HttpGet("{id:guid}/comments")] public ActionResult GetLinkComments(Guid id) { return _linkService.GetLinkComments(id); } [HttpPost("{id:guid}/comments")] [Authorize] [ProducesResponseType(StatusCodes.Status201Created)] [ProducesResponseType(StatusCodes.Status400BadRequest)] public ActionResult PublishComment(Guid id, PublishCommentBody cmd) { var commentId = _linkService.PublishComment(new PublishCommentCommand { LinkId = id, Content = cmd.Content }); return CreatedAtAction( nameof(CommentsController.GetById), "Comments", new { id = commentId }, null ); } /// /// Publie un nouveau lien sur la plateforme /// /// /// [HttpPost] [Authorize] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status201Created)] public ActionResult PublishLink(PublishLinkCommand cmd) { var id = _linkService.PublishLink(cmd); return CreatedAtAction(nameof(GetLinkDetail), new { id = id }, null); } } public class PublishCommentBody { [Required] public string Content { get; set; } }