using FastEndpoints;
using TaxBaik.Application.Services;

namespace TaxBaik.Web.Endpoints.Blog;

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

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

    public override void Configure()
    {
        Get("/api/blog/admin/{id}");
        Policies("Bearer");
    }

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

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