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

namespace TaxBaik.Web.Endpoints.Blog;

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

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

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

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

            await SendAsync(result, 200, cancellation: ct);
        }
        catch (ValidationException ex)
        {
            ThrowError(ex.Message, statusCode: 400);
        }
    }
}
