updating CreatePurchaseProductEndpoint.cs and GetPurchaseProductDto.cs
This commit is contained in:
@@ -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; }
|
||||
|
@@ -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,
|
||||
|
Reference in New Issue
Block a user