using FastEndpoints;
using Knots.DTO.Role;
using Knots.DTO.User;

namespace Knots.Endpoints.Role;

public class CreateRoleEndpoint(KnotsDbContext knotsDbContext) : Endpoint<CreateRoleDto, GetRoleDto>
{
    public override void Configure()
    {
        Post("/roles");
        AllowAnonymous();
    }
    
    public override async Task HandleAsync(CreateRoleDto req, CancellationToken ct)
    {
        Models.Role role = new()
        {
            Libelle =  req.Libelle
        };
        knotsDbContext.Add(role);
        await knotsDbContext.SaveChangesAsync(ct);

        GetRoleDto response = new()
        {
            Libelle = role.Libelle
        };

        await Send.OkAsync(response, ct);

    }
}