diff --git a/PyroFetes/DTO/PurchaseProduct/Response/GetPurchaseProductDto.cs b/PyroFetes/DTO/PurchaseProduct/Response/GetPurchaseProductDto.cs index 8278f17..d086ed3 100644 --- a/PyroFetes/DTO/PurchaseProduct/Response/GetPurchaseProductDto.cs +++ b/PyroFetes/DTO/PurchaseProduct/Response/GetPurchaseProductDto.cs @@ -3,7 +3,7 @@ namespace PyroFetes.DTO.PurchaseProduct.Response; public class GetPurchaseProductDto { public int ProductId { get; set; } - public int ProductReferences { get; set; } + public string? ProductReferences { get; set; } public string? ProductName { get; set; } public decimal ProductDuration {get; set;} public decimal ProductCaliber { get; set; } diff --git a/PyroFetes/Endpoints/PurchaseProduct/CreatePurchaseProductEndpoint.cs b/PyroFetes/Endpoints/PurchaseProduct/CreatePurchaseProductEndpoint.cs index b688d4a..0578342 100644 --- a/PyroFetes/Endpoints/PurchaseProduct/CreatePurchaseProductEndpoint.cs +++ b/PyroFetes/Endpoints/PurchaseProduct/CreatePurchaseProductEndpoint.cs @@ -16,18 +16,15 @@ public class CreatePurchaseProductEndpoint(PyroFetesDbContext database) public override async Task HandleAsync(CreatePurchaseProductDto req, CancellationToken ct) { - // Vérifie que le produit existe var product = await database.Products.FirstOrDefaultAsync(p => p.Id == req.ProductId, ct); if (product == null) { await Send.NotFoundAsync(ct); return; } - - // Si la commande existe déjà, on la récupère + var purchaseOrder = await database.PurchaseOrders.FirstOrDefaultAsync(po => po.Id == req.PurchaseOrderId, ct); - - // Si elle n'existe pas, on la crée + if (purchaseOrder == null) { purchaseOrder = new Models.PurchaseOrder() @@ -35,25 +32,22 @@ public class CreatePurchaseProductEndpoint(PyroFetesDbContext database) PurchaseConditions = req.PurchaseOrderPurchaseConditions ?? "Conditions non précisées" }; database.PurchaseOrders.Add(purchaseOrder); - await database.SaveChangesAsync(ct); // important pour avoir l'Id + await database.SaveChangesAsync(ct); } - - // Création du lien produit-commande + var purchaseProduct = new Models.PurchaseProduct() { ProductId = product.Id, PurchaseOrderId = purchaseOrder.Id, Quantity = req.Quantity }; - database.PurchaseProducts.Add(purchaseProduct); await database.SaveChangesAsync(ct); - - // Prépare la réponse + var responseDto = new GetPurchaseProductDto() { ProductId = product.Id, - ProductReferences = int.TryParse(product.Reference, out var refInt) ? refInt : 0, + ProductReferences = product.Reference, ProductName = product.Name, ProductDuration = product.Duration, ProductCaliber = product.Caliber,