using Application.Abstractions.Messaging;
using Web.Api.Common;
using Web.Api.Extensions;

namespace Web.Api.Endpoints.News;

internal sealed class Article : IEndpoint
{
    public void MapEndpoint(IEndpointRouteBuilder app)
    {
        app.MapGet("api/news/articles/{id}", async (
            int id,
            ISender sender,
            CancellationToken ct
        ) =>
        {
            var result = await sender.Send(new Application.Features.Api.News.GetArticle.Query(id), ct);

            return result.Match(
                data => ApiResponse.Ok(data),
                CustomResults.Problem
            );
        })
        .WithTags("News")
        .AllowAnonymous();
    }
}