From 7d92f80de3672cfb6586d69c3bd7a9ff1dbeb5e6 Mon Sep 17 00:00:00 2001 From: sanchezvem Date: Sun, 9 Nov 2025 18:46:24 +0100 Subject: [PATCH] Actualiser PyroFetes/Endpoints/Price/CreatePriceEndpoint.cs --- .../Endpoints/Price/CreatePriceEndpoint.cs | 87 ++++++++++--------- 1 file changed, 45 insertions(+), 42 deletions(-) diff --git a/PyroFetes/Endpoints/Price/CreatePriceEndpoint.cs b/PyroFetes/Endpoints/Price/CreatePriceEndpoint.cs index 71dbb1e..d65ba2d 100644 --- a/PyroFetes/Endpoints/Price/CreatePriceEndpoint.cs +++ b/PyroFetes/Endpoints/Price/CreatePriceEndpoint.cs @@ -15,6 +15,7 @@ public class CreatePriceEndpoint(PyroFetesDbContext database) : Endpoint s.Id == req.SupplierId, ct); if (supplier == null) { @@ -27,56 +28,58 @@ public class CreatePriceEndpoint(PyroFetesDbContext database) : Endpoint p.Reference == req.ProductReferences, ct); - if (product != null) + + // Gestion du produit + var product = await database.Products.SingleOrDefaultAsync(p => p.Id == req.ProductId, ct); + if (product == null) { - await Send.NotFoundAsync(ct); + product = new Models.Product() + { + Reference = req.ProductReferences, + Name = req.ProductName, + Duration = req.ProductDuration, + Caliber = req.ProductCaliber, + ApprovalNumber = req.ProductApprovalNumber, + Weight = req.ProductWeight, + Nec = req.ProductNec, + Image = req.ProductImage, + Link = req.ProductLink, + MinimalQuantity = req.ProductMinimalQuantity + }; + database.Products.Add(product); + await database.SaveChangesAsync(ct); + } + + // Vérifie si le prix existe déjà pour ce fournisseur et produit + var existingPrice = await database.Prices + .SingleOrDefaultAsync(p => p.ProductId == product.Id && p.SupplierId == supplier.Id, ct); + + if (existingPrice != null) + { + await Send.ConflictAsync("Le fournisseur a déjà un prix pour ce produit.", ct); return; } - - var productAdded = new Models.Product() - { - Reference = req.ProductReferences, - Name = req.ProductName, - Duration = req.ProductDuration, - Caliber = req.ProductCaliber, - ApprovalNumber = req.ProductApprovalNumber, - Weight = req.ProductWeight, - Nec = req.ProductNec, - Image = req.ProductImage, - Link = req.ProductLink, - MinimalQuantity = req.ProductMinimalQuantity - }; - database.Products.Add(productAdded); - await database.SaveChangesAsync(ct); - - var price = await database.Prices.SingleOrDefaultAsync(p => p.ProductId == req.ProductId && p.SupplierId == req.SupplierId, ct); - if (price != null) - { - await Send.NotFoundAsync(ct); - return; - } - + + // Création du prix var priceAdded = new Models.Price() { SellingPrice = req.SellingPrice, SupplierId = supplier.Id, - ProductId = productAdded.Id, + ProductId = product.Id }; database.Prices.Add(priceAdded); await database.SaveChangesAsync(ct); - + + // Création du DTO de réponse var responseDto = new GetPriceDto() { SellingPrice = priceAdded.SellingPrice, SupplierId = supplier.Id, - ProductId = productAdded.Id, + ProductId = product.Id, SupplierName = supplier.Name, SupplierEmail = supplier.Email, SupplierPhone = supplier.Phone, @@ -84,16 +87,16 @@ public class CreatePriceEndpoint(PyroFetesDbContext database) : Endpoint