forked from sanchezvem/PyroFetes
38 lines
1.0 KiB
C#
38 lines
1.0 KiB
C#
using FastEndpoints;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace PyroFetes.Endpoints.Movement;
|
|
|
|
public class DeleteMovementRequest
|
|
{
|
|
public int Id { get; set; }
|
|
}
|
|
|
|
public class DeleteMovementEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<DeleteMovementRequest>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Delete("/api/Movements/{@id}", x => new { x.Id });
|
|
AllowAnonymous();
|
|
}
|
|
|
|
public override async Task HandleAsync(DeleteMovementRequest req, CancellationToken ct)
|
|
{
|
|
|
|
Models.Movement? movementToDelete = await pyrofetesdbcontext
|
|
.Movements
|
|
.SingleOrDefaultAsync(a => a.Id == req.Id, cancellationToken: ct);
|
|
|
|
if (movementToDelete == null)
|
|
{
|
|
Console.WriteLine($"Aucune mouvement avec l'ID {req.Id} trouvé.");
|
|
await Send.NotFoundAsync(ct);
|
|
return;
|
|
}
|
|
|
|
pyrofetesdbcontext.Movements.Remove(movementToDelete);
|
|
await pyrofetesdbcontext.SaveChangesAsync(ct);
|
|
|
|
await Send.NoContentAsync(ct);
|
|
}
|
|
} |