forked from sanchezvem/PyroFetes
35 lines
1023 B
Plaintext
35 lines
1023 B
Plaintext
using FastEndpoints;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace API.Endpoints.Material;
|
|
public class DeleteMaterialRequest
|
|
{
|
|
public int Id { get; set; }
|
|
}
|
|
public class DeleteMaterialEndpoint(AppDbContext appDbContext) : Endpoint<DeleteMaterialRequest>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Delete("/materials/{@id}", x => new { x.Id });
|
|
AllowAnonymous();
|
|
}
|
|
|
|
public override async Task HandleAsync(DeleteMaterialRequest req, CancellationToken ct)
|
|
{
|
|
Models.Material? materialToDelete = await appDbContext
|
|
.Materials
|
|
.SingleOrDefaultAsync(a => a.Id == req.Id, cancellationToken: ct);
|
|
|
|
if (materialToDelete == null)
|
|
{
|
|
Console.WriteLine($"Aucun matériel avec l'ID {req.Id} trouvé.");
|
|
await Send.NotFoundAsync(ct);
|
|
return;
|
|
}
|
|
|
|
appDbContext.Materials.Remove(materialToDelete);
|
|
await appDbContext.SaveChangesAsync(ct);
|
|
|
|
await Send.NoContentAsync(ct);
|
|
}
|
|
} |