using FastEndpoints;
using Knots.DTO.Discussion;

namespace Knots.Endpoints.Discussion;

public class CreateDiscussionEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint<CreateDiscussionDto>
{
    public override void Configure()
    {
        Post("/discussions");
        AllowAnonymous();
    }
    
    public override async Task HandleAsync(CreateDiscussionDto req, CancellationToken ct)
    {
        Models.Discussion? discussion = mapper.Map<Models.Discussion>(req);
        db.Discussions.Add(discussion);
        await db.SaveChangesAsync(ct);
        await Send.NoContentAsync(ct);
    }
}