using FastEndpoints;
using Knots.DTO.Message;

namespace Knots.Endpoints.Message;

public class DeleteMessageEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint<DeleteMessageDto>
{
    public override void Configure()
    {
        Delete("/messages");
        AllowAnonymous();
    }
    
    public override async Task HandleAsync(DeleteMessageDto req, CancellationToken ct)
    {
        Models.Message? message = mapper.Map<Models.Message>(req);
        db.Messages.Remove(message);
        await db.SaveChangesAsync(ct);
        await Send.NoContentAsync(ct);
    }
}