diff --git a/PyroFetes/Endpoints/Color/UpdateColorEndpoint b/PyroFetes/Endpoints/Color/UpdateColorEndpoint new file mode 100644 index 0000000..be82f4b --- /dev/null +++ b/PyroFetes/Endpoints/Color/UpdateColorEndpoint @@ -0,0 +1,42 @@ +using API.DTO.Color.Request; +using API.DTO.Color.Response; +using FastEndpoints; +using Microsoft.AspNetCore.Server.Kestrel; +using Microsoft.EntityFrameworkCore; + +namespace API.Endpoints.Color; + +public class UpdateColorEndpoint(AppDbContext appDbContext) : Endpoint +{ + public override void Configure() + { + Put("/colors/{@id}", x => new { x.Id }); + AllowAnonymous(); + } + + public override async Task HandleAsync(UpdateColorDto req, CancellationToken ct) + { + Models.Color? colorToEdit = await appDbContext + .Colors + .SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct); + + if (colorToEdit == null) + { + Console.WriteLine("Aucune couleur avec l'id {req.Id} trouvé."); + await Send.NotFoundAsync(ct); + return; + + } + colorToEdit.Label = req.Label; + await appDbContext.SaveChangesAsync(ct); + + GetColorDto responseDto = new() + { + Id = req.Id, + Label = req.Label, + }; + await Send.OkAsync(responseDto, ct); + + } + +} \ No newline at end of file