using FastEndpoints;
using TaxBaik.Application.Services;

namespace TaxBaik.Web.Endpoints.Blog;

public class GetBySlugEndpoint : Endpoint<EmptyRequest, object>
{
    private readonly BlogService _blogService;

    public GetBySlugEndpoint(BlogService blogService)
    {
        _blogService = blogService;
    }

    public override void Configure()
    {
        Get("/api/blog/{slug}");
        AllowAnonymous();
    }

    public override async Task HandleAsync(EmptyRequest _, CancellationToken ct)
    {
        var slug = Route<string>("slug") ?? string.Empty;
        var post = await _blogService.GetBySlugAsync(slug);
        if (post == null)
        {
            ThrowError("포스트를 찾을 수 없습니다.", statusCode: 404);
        }

        await SendAsync(post, 200, cancellation: ct);
    }
}
