updating CreatePurchaseProductEndpoint.cs and GetPurchaseProductDto.cs
This commit is contained in:
@@ -3,7 +3,7 @@ namespace PyroFetes.DTO.PurchaseProduct.Response;
|
|||||||
public class GetPurchaseProductDto
|
public class GetPurchaseProductDto
|
||||||
{
|
{
|
||||||
public int ProductId { get; set; }
|
public int ProductId { get; set; }
|
||||||
public int ProductReferences { get; set; }
|
public string? ProductReferences { get; set; }
|
||||||
public string? ProductName { get; set; }
|
public string? ProductName { get; set; }
|
||||||
public decimal ProductDuration {get; set;}
|
public decimal ProductDuration {get; set;}
|
||||||
public decimal ProductCaliber { get; set; }
|
public decimal ProductCaliber { get; set; }
|
||||||
|
@@ -16,7 +16,6 @@ public class CreatePurchaseProductEndpoint(PyroFetesDbContext database)
|
|||||||
|
|
||||||
public override async Task HandleAsync(CreatePurchaseProductDto req, CancellationToken ct)
|
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);
|
var product = await database.Products.FirstOrDefaultAsync(p => p.Id == req.ProductId, ct);
|
||||||
if (product == null)
|
if (product == null)
|
||||||
{
|
{
|
||||||
@@ -24,10 +23,8 @@ public class CreatePurchaseProductEndpoint(PyroFetesDbContext database)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Si la commande existe déjà, on la récupère
|
|
||||||
var purchaseOrder = await database.PurchaseOrders.FirstOrDefaultAsync(po => po.Id == req.PurchaseOrderId, ct);
|
var purchaseOrder = await database.PurchaseOrders.FirstOrDefaultAsync(po => po.Id == req.PurchaseOrderId, ct);
|
||||||
|
|
||||||
// Si elle n'existe pas, on la crée
|
|
||||||
if (purchaseOrder == null)
|
if (purchaseOrder == null)
|
||||||
{
|
{
|
||||||
purchaseOrder = new Models.PurchaseOrder()
|
purchaseOrder = new Models.PurchaseOrder()
|
||||||
@@ -35,25 +32,22 @@ public class CreatePurchaseProductEndpoint(PyroFetesDbContext database)
|
|||||||
PurchaseConditions = req.PurchaseOrderPurchaseConditions ?? "Conditions non précisées"
|
PurchaseConditions = req.PurchaseOrderPurchaseConditions ?? "Conditions non précisées"
|
||||||
};
|
};
|
||||||
database.PurchaseOrders.Add(purchaseOrder);
|
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()
|
var purchaseProduct = new Models.PurchaseProduct()
|
||||||
{
|
{
|
||||||
ProductId = product.Id,
|
ProductId = product.Id,
|
||||||
PurchaseOrderId = purchaseOrder.Id,
|
PurchaseOrderId = purchaseOrder.Id,
|
||||||
Quantity = req.Quantity
|
Quantity = req.Quantity
|
||||||
};
|
};
|
||||||
|
|
||||||
database.PurchaseProducts.Add(purchaseProduct);
|
database.PurchaseProducts.Add(purchaseProduct);
|
||||||
await database.SaveChangesAsync(ct);
|
await database.SaveChangesAsync(ct);
|
||||||
|
|
||||||
// Prépare la réponse
|
|
||||||
var responseDto = new GetPurchaseProductDto()
|
var responseDto = new GetPurchaseProductDto()
|
||||||
{
|
{
|
||||||
ProductId = product.Id,
|
ProductId = product.Id,
|
||||||
ProductReferences = int.TryParse(product.Reference, out var refInt) ? refInt : 0,
|
ProductReferences = product.Reference,
|
||||||
ProductName = product.Name,
|
ProductName = product.Name,
|
||||||
ProductDuration = product.Duration,
|
ProductDuration = product.Duration,
|
||||||
ProductCaliber = product.Caliber,
|
ProductCaliber = product.Caliber,
|
||||||
|
Reference in New Issue
Block a user