using FastEndpoints;
using Knots.DTO.Message;

namespace Knots.Endpoints.Message;

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