diff --git a/PyroFetes/Endpoints/Color/DeleteColorEndpoint b/PyroFetes/Endpoints/Color/DeleteColorEndpoint new file mode 100644 index 0000000..6f7847a --- /dev/null +++ b/PyroFetes/Endpoints/Color/DeleteColorEndpoint @@ -0,0 +1,37 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; + +namespace API.Endpoints.Color; + +public class DeleteColorRequest +{ + public int Id { get; set; } +} + +public class DeleteColorEndpoint(AppDbContext appDbContext) : Endpoint +{ + public override void Configure() + { + Delete("/colors/{@id}", x => new { x.Id }); + AllowAnonymous(); + } + + public override async Task HandleAsync(DeleteColorRequest req, CancellationToken ct) + { + Models.Color? colorToDelete = await appDbContext + .Colors + .SingleOrDefaultAsync(a => a.Id == req.Id, cancellationToken: ct); + + if (colorToDelete == null) + { + Console.WriteLine($"Aucune couleur avec l'ID {req.Id} trouvé."); + await Send.NotFoundAsync(ct); + return; + } + + appDbContext.Colors.Remove(colorToDelete); + await appDbContext.SaveChangesAsync(ct); + + await Send.NoContentAsync(ct); + } +} \ No newline at end of file