using FastEndpoints;
using Knots.DTO.Group;
using Microsoft.EntityFrameworkCore;

namespace Knots.Endpoints.Group;

public class PatchGroupMembersAmountEndpoint(KnotsDbContext knotsDbContext) : Endpoint<UpdateGroupMembersAmountDto>
{
    public override void Configure()
    {
        Patch("/groups/{@Id}/membersAmount/", x => new {x.Id});
        AllowAnonymous();
    }

    public override async Task HandleAsync(UpdateGroupMembersAmountDto req, CancellationToken ct)
    {
        Models.Group? databaseGroup = await knotsDbContext.Groups.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct);

        if (databaseGroup is null)
        {
            await Send.NotFoundAsync(ct);
            return;
        }
        
        databaseGroup.MembersAmount = req.MembersAmount;
        await knotsDbContext.SaveChangesAsync(ct);
        await Send.NoContentAsync(ct);
    }
}