56 lines
1.5 KiB
C#
56 lines
1.5 KiB
C#
using HackerNet.Application;
|
|
using HackerNet.Infrastructure.AspNet.Filters;
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retourne les derniers liens publiés de la plateforme.
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
public ActionResult<LinkHomePage[]> GetLinks()
|
|
{
|
|
return _linkService.GetPublishedLinks();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Récupère un lien par son identifiant.
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
[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<LinkHomePage> GetLinkDetail(Guid id)
|
|
{
|
|
return _linkService.GetLinkDetail(id);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Publie un nouveau lien sur la plateforme
|
|
/// </summary>
|
|
/// <param name="cmd"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status201Created)]
|
|
public ActionResult PublishLink(PublishLinkCommand cmd)
|
|
{
|
|
var id = _linkService.PublishLink(cmd);
|
|
|
|
return CreatedAtAction(nameof(GetLinkDetail), new { id = id }, null);
|
|
}
|
|
} |