forked from sanchezvem/PyroFetes
Ajouter PyroFetes/Endpoints/Color/DeleteColorEndpoint
This commit is contained in:
37
PyroFetes/Endpoints/Color/DeleteColorEndpoint
Normal file
37
PyroFetes/Endpoints/Color/DeleteColorEndpoint
Normal file
@@ -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<DeleteColorRequest>
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user