forked from sanchezvem/PyroFetes
17:17 09/10
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
using API.DTO.Supplier.Request;
|
||||
using API.DTO.Supplier.Response;
|
||||
using PyroFetes.DTO.Supplier.Request;
|
||||
using PyroFetes.DTO.Supplier.Response;
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes.Models;
|
||||
|
||||
namespace PyroFetes.Endpoints.Supplier;
|
||||
|
||||
@@ -15,18 +16,16 @@ public class UpdateSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End
|
||||
|
||||
public override async Task HandleAsync(UpdateSupplierDto req, CancellationToken ct)
|
||||
{
|
||||
Models.Supplier? supplierToEdit = await pyrofetesdbcontext
|
||||
var supplierToEdit = await pyrofetesdbcontext
|
||||
.Suppliers
|
||||
.SingleOrDefaultAsync(s => s.Id == req.Id, cancellationToken: ct);
|
||||
.Include(s => s.Prices)
|
||||
.SingleOrDefaultAsync(s => s.Id == req.Id, ct);
|
||||
|
||||
if (supplierToEdit == null)
|
||||
if (supplierToEdit is null)
|
||||
{
|
||||
Console.WriteLine($"Aucun fournisseur avec l'ID {req.Id} trouvé.");
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
// Mise à jour des propriétés
|
||||
supplierToEdit.Name = req.Name;
|
||||
supplierToEdit.Email = req.Email;
|
||||
supplierToEdit.Phone = req.PhoneNumber;
|
||||
@@ -34,9 +33,28 @@ public class UpdateSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End
|
||||
supplierToEdit.ZipCode = req.ZipCode;
|
||||
supplierToEdit.City = req.City;
|
||||
|
||||
if (req.Products is not null)
|
||||
{
|
||||
// Supprimer les anciennes liaisons
|
||||
var existingPrices = pyrofetesdbcontext.Prices
|
||||
.Where(p => p.SupplierId == supplierToEdit.Id);
|
||||
pyrofetesdbcontext.Prices.RemoveRange(existingPrices);
|
||||
|
||||
// Ajouter les nouvelles liaisons
|
||||
foreach (var p in req.Products)
|
||||
{
|
||||
pyrofetesdbcontext.Prices.Add(new Price
|
||||
{
|
||||
SupplierId = supplierToEdit.Id,
|
||||
ProductId = p.ProductId,
|
||||
SellingPrice = p.SellingPrice
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
await pyrofetesdbcontext.SaveChangesAsync(ct);
|
||||
|
||||
GetSupplierDto responseDto = new()
|
||||
var response = new GetSupplierDto
|
||||
{
|
||||
Id = supplierToEdit.Id,
|
||||
Name = supplierToEdit.Name,
|
||||
@@ -47,6 +65,6 @@ public class UpdateSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End
|
||||
City = supplierToEdit.City
|
||||
};
|
||||
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
await Send.OkAsync(response, ct);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user