Files
Knots/Knots/Endpoints/User/DeleteUserEndpoint.cs
2026-03-12 17:11:48 +01:00

28 lines
846 B
C#

using FastEndpoints;
using Knots.DTO.User;
using Microsoft.EntityFrameworkCore;
namespace Knots.Endpoints.User;
public class DeleteUserEndpoint(KnotsDbContext knotsDbContext) : Endpoint <GetUserDto, GetUserDetailsDto>
{
public override void Configure()
{
Delete ("/users/{@Id}", x => new { x.Username });
}
public override async Task HandleAsync(GetUserDto req, CancellationToken ct)
{
Models.User? databaseUser = await knotsDbContext.Users.SingleOrDefaultAsync(x => x.Username == req.Username, cancellationToken: ct);
if (databaseUser == null)
{
await Send.NotFoundAsync(ct);
return;
}
knotsDbContext.Users.Remove(databaseUser);
await knotsDbContext.SaveChangesAsync(ct);
await Send.NoContentAsync(ct);
}
}