forked from sanchezvem/PyroFetes
37 lines
1.0 KiB
C#
37 lines
1.0 KiB
C#
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);
|
|
}
|
|
} |