MAJ Mathilde

This commit is contained in:
2025-10-09 15:16:08 +02:00
parent f155d03559
commit d1fa3aca68
24 changed files with 628 additions and 211 deletions

View File

@@ -2,6 +2,7 @@
using API.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);
}
}
}