using FastEndpoints;
using TaxBaik.Application.DTOs;
using TaxBaik.Application.Services;

namespace TaxBaik.Web.Endpoints.Blog;

public class CreateEndpoint : Endpoint<CreateBlogPostDto, object>
{
    private readonly BlogService _blogService;

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

    public override void Configure()
    {
        Post("/api/blog");
        Policies("Bearer");
    }

    public override async Task HandleAsync(CreateBlogPostDto request, CancellationToken ct)
    {
        try
        {
            var result = await _blogService.CreateAsync(request);
            await SendAsync(result, 201, cancellation: ct);
        }
        catch (ValidationException ex)
        {
            ThrowError(ex.Message, statusCode: 400);
        }
    }
}
