From 018f7c5c25d2264055ab437a028a0812960223da Mon Sep 17 00:00:00 2001 From: kieva Date: Wed, 8 Oct 2025 16:45:05 +0200 Subject: [PATCH] Ajouter PyroFetes/Endpoints/Material/UpdateMaterialEndpoint --- .../Endpoints/Material/UpdateMaterialEndpoint | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 PyroFetes/Endpoints/Material/UpdateMaterialEndpoint diff --git a/PyroFetes/Endpoints/Material/UpdateMaterialEndpoint b/PyroFetes/Endpoints/Material/UpdateMaterialEndpoint new file mode 100644 index 0000000..a68e95b --- /dev/null +++ b/PyroFetes/Endpoints/Material/UpdateMaterialEndpoint @@ -0,0 +1,43 @@ +using API.DTO.Material.Request; +using API.DTO.Material.Response; +using FastEndpoints; +using Microsoft.AspNetCore.Server.Kestrel; +using Microsoft.EntityFrameworkCore; + +namespace API.Endpoints.Material; + +public class UpdateMaterialEndpoint(AppDbContext appDbContext) : Endpoint +{ + public override void Configure() + { + Put("/material/{@id}", x => new { x.Id }); + AllowAnonymous(); + } + + public override async Task HandleAsync(UpdateMaterialDto req, CancellationToken ct) + { + Models.Material? materialToEdit = await appDbContext + .Materials + .SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct); + + if (materialToEdit == null) + { + Console.WriteLine("Aucun matériel avec l'id {req.Id} trouvé."); + await Send.NotFoundAsync(ct); + return; + + } + materialToEdit.Name = req.Name; + materialToEdit.WarehouseId = req.WarehouseId; + await appDbContext.SaveChangesAsync(ct); + + GetMaterialDto responseDto = new() + { + Id = req.Id, + Name = req.Name, + WarehouseId = req.WarehouseId, + }; + await Send.OkAsync(responseDto, ct); + } + +} \ No newline at end of file