forked from sanchezvem/PyroFetes
09/10
This commit is contained in:
37
PyroFetes/Endpoints/Supplier/DeleteSupplierEndpoint.cs
Normal file
37
PyroFetes/Endpoints/Supplier/DeleteSupplierEndpoint.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace PyroFetes.Endpoints.Supplier;
|
||||
|
||||
public class DeleteSupplierRequest
|
||||
{
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
||||
public class DeleteSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<DeleteSupplierRequest>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Delete("/api/suppliers/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(DeleteSupplierRequest req, CancellationToken ct)
|
||||
{
|
||||
Models.Supplier? supplierToDelete = await pyrofetesdbcontext
|
||||
.Suppliers
|
||||
.SingleOrDefaultAsync(s => s.Id == req.Id, cancellationToken: ct);
|
||||
|
||||
if (supplierToDelete == null)
|
||||
{
|
||||
Console.WriteLine($"Aucun fournisseur avec l'ID {req.Id} trouvé.");
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
pyrofetesdbcontext.Suppliers.Remove(supplierToDelete);
|
||||
await pyrofetesdbcontext.SaveChangesAsync(ct);
|
||||
|
||||
await Send.NoContentAsync(ct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user