forked from sanchezvem/PyroFetes
Ajouter PyroFetes/Endpoints/Effect/DeleteEffectEndpoint
This commit is contained in:
36
PyroFetes/Endpoints/Effect/DeleteEffectEndpoint
Normal file
36
PyroFetes/Endpoints/Effect/DeleteEffectEndpoint
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
using FastEndpoints;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace API.Endpoints.Effect;
|
||||||
|
|
||||||
|
public class DeleteEffectRequest
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
}
|
||||||
|
public class DeleteEffectEndpoint(AppDbContext appDbContext) : Endpoint<DeleteEffectRequest>
|
||||||
|
{
|
||||||
|
public override void Configure()
|
||||||
|
{
|
||||||
|
Delete("/effects/{@id}", x => new { x.Id });
|
||||||
|
AllowAnonymous();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async Task HandleAsync(DeleteEffectRequest req, CancellationToken ct)
|
||||||
|
{
|
||||||
|
Models.Effect? effectToDelete = await appDbContext
|
||||||
|
.Effects
|
||||||
|
.SingleOrDefaultAsync(a => a.Id == req.Id, cancellationToken: ct);
|
||||||
|
|
||||||
|
if (effectToDelete == null)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Aucun effet avec l'ID {req.Id} trouvé.");
|
||||||
|
await Send.NotFoundAsync(ct);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
appDbContext.Effects.Remove(effectToDelete);
|
||||||
|
await appDbContext.SaveChangesAsync(ct);
|
||||||
|
|
||||||
|
await Send.NoContentAsync(ct);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user