using FastEndpoints;
using TaxBaik.Application.Services;

namespace TaxBaik.Web.Endpoints.Blog;

public class GetAllEndpoint : Endpoint<EmptyRequest, BlogPostListResponse>
{
    private readonly BlogService _blogService;

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

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

    public override async Task HandleAsync(EmptyRequest _, CancellationToken ct)
    {
        var posts = await _blogService.GetAllAsync();
        await SendAsync(new BlogPostListResponse
        {
            Posts = posts.Cast<object>().ToList()
        }, 200, cancellation: ct);
    }
}
