using FastEndpoints;
using Knots.DTO.Key;

namespace Knots.Endpoints.Key;

public class DeleteKeyEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint<DeleteKeyDto>
{
    public override void Configure()
    {
        Delete("/keys");
        AllowAnonymous();
    }
    
    public override async Task HandleAsync(DeleteKeyDto req, CancellationToken ct)
    {
        Models.Key? key = mapper.Map<Models.Key>(req);
        db.Keys.Remove(key);
        await db.SaveChangesAsync(ct);
        await Send.NoContentAsync(ct);
    }
}