forked from sanchezvem/PyroFetes
MAJ Mathilde
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes.Models;
|
||||
|
||||
namespace PyroFetes.Endpoints.Supplier;
|
||||
|
||||
@@ -18,19 +19,32 @@ public class DeleteSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End
|
||||
|
||||
public override async Task HandleAsync(DeleteSupplierRequest req, CancellationToken ct)
|
||||
{
|
||||
Models.Supplier? supplierToDelete = await pyrofetesdbcontext
|
||||
var supplierToDelete = await pyrofetesdbcontext
|
||||
.Suppliers
|
||||
.SingleOrDefaultAsync(s => s.Id == req.Id, cancellationToken: ct);
|
||||
.SingleOrDefaultAsync(s => s.Id == req.Id, ct);
|
||||
|
||||
if (supplierToDelete == null)
|
||||
if (supplierToDelete is null)
|
||||
{
|
||||
Console.WriteLine($"Aucun fournisseur avec l'ID {req.Id} trouvé.");
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
// Supprimer les liaisons Price avant le fournisseur
|
||||
var relatedPrices = await pyrofetesdbcontext.Prices
|
||||
.Where(p => p.SupplierId == req.Id)
|
||||
.ToListAsync(ct);
|
||||
|
||||
if (relatedPrices.Any())
|
||||
{
|
||||
pyrofetesdbcontext.Prices.RemoveRange(relatedPrices);
|
||||
}
|
||||
|
||||
// Supprimer le fournisseur
|
||||
pyrofetesdbcontext.Suppliers.Remove(supplierToDelete);
|
||||
|
||||
await pyrofetesdbcontext.SaveChangesAsync(ct);
|
||||
Console.WriteLine($"Fournisseur {req.Id} et ses prix liés supprimés avec succès.");
|
||||
|
||||
await Send.NoContentAsync(ct);
|
||||
}
|
||||
|
Reference in New Issue
Block a user