From 52f0e84323e13508f3270114b9a20d1b4f067ab7 Mon Sep 17 00:00:00 2001 From: kieva Date: Wed, 8 Oct 2025 16:42:33 +0200 Subject: [PATCH] Ajouter PyroFetes/Endpoints/Material/DeleteMaterialEndpoint --- .../Endpoints/Material/DeleteMaterialEndpoint | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 PyroFetes/Endpoints/Material/DeleteMaterialEndpoint diff --git a/PyroFetes/Endpoints/Material/DeleteMaterialEndpoint b/PyroFetes/Endpoints/Material/DeleteMaterialEndpoint new file mode 100644 index 0000000..eba120b --- /dev/null +++ b/PyroFetes/Endpoints/Material/DeleteMaterialEndpoint @@ -0,0 +1,35 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; + +namespace API.Endpoints.Material; +public class DeleteMaterialRequest +{ + public int Id { get; set; } +} +public class DeleteMaterialEndpoint(AppDbContext appDbContext) : Endpoint +{ + public override void Configure() + { + Delete("/materials/{@id}", x => new { x.Id }); + AllowAnonymous(); + } + + public override async Task HandleAsync(DeleteMaterialRequest req, CancellationToken ct) + { + Models.Material? materialToDelete = await appDbContext + .Materials + .SingleOrDefaultAsync(a => a.Id == req.Id, cancellationToken: ct); + + if (materialToDelete == null) + { + Console.WriteLine($"Aucun matériel avec l'ID {req.Id} trouvé."); + await Send.NotFoundAsync(ct); + return; + } + + appDbContext.Materials.Remove(materialToDelete); + await appDbContext.SaveChangesAsync(ct); + + await Send.NoContentAsync(ct); + } +} \ No newline at end of file