using FastEndpoints;
using Knots.DTO.User;

namespace Knots.Endpoints.User;

public class CreateUserEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint<CreateUserDto, GetUserDto>
{
    public override void Configure()
    {
        Post("/users");
        AllowAnonymous();
    }
    
    public override async Task HandleAsync(CreateUserDto req, CancellationToken ct)
    {
        Models.User? user = mapper.Map<Models.User>(req);
        db.Users.Add(user);
        await db.SaveChangesAsync(ct);
        await Send.NoContentAsync(ct);
    }
}