forked from sanchezvem/PyroFetes
AJout des DTO et endpoint sur le nouveau git
This commit is contained in:
38
PyroFetes/Endpoints/Movement/DeleteMovementEndpoint.cs
Normal file
38
PyroFetes/Endpoints/Movement/DeleteMovementEndpoint.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user