using FastEndpoints;
using Knots.DTO.Discussion;

namespace Knots.Endpoints.Discussion;

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