Creating purchaseproduct's endpoint
This commit is contained in:
@@ -10,7 +10,6 @@ public class GetPurchaseProductDto
|
||||
public int ProductApprovalNumber { get; set; }
|
||||
public decimal ProductWeight { get; set; }
|
||||
public decimal ProductNec { get; set; }
|
||||
public decimal ProductSellingPrice { get; set; }
|
||||
public string? ProductImage { get; set; }
|
||||
public string? ProductLink { get; set; }
|
||||
public int ProductMinimalQuantity { get; set; }
|
||||
|
@@ -5,7 +5,8 @@ using PyroFetes.DTO.PurchaseProduct.Response;
|
||||
|
||||
namespace PyroFetes.Endpoints.PurchaseProduct;
|
||||
|
||||
public class CreatePurchaseProductEndpoint(PyroFetesDbContext database) : Endpoint<CreatePurchaseProductDto, GetPurchaseProductDto>
|
||||
public class CreatePurchaseProductEndpoint(PyroFetesDbContext database)
|
||||
: Endpoint<CreatePurchaseProductDto, GetPurchaseProductDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
@@ -15,38 +16,59 @@ public class CreatePurchaseProductEndpoint(PyroFetesDbContext database) : Endpoi
|
||||
|
||||
public override async Task HandleAsync(CreatePurchaseProductDto req, CancellationToken ct)
|
||||
{
|
||||
var purchaseOrderExists = await database.PurchaseOrders.FirstOrDefaultAsync(a => a.Id == req.PurchaseOrderId, ct);
|
||||
|
||||
if (purchaseOrderExists == null)
|
||||
// Vérifie que le produit existe
|
||||
var product = await database.Products.FirstOrDefaultAsync(p => p.Id == req.ProductId, ct);
|
||||
if (product == null)
|
||||
{
|
||||
await Send.NoContentAsync(ct);
|
||||
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()
|
||||
{
|
||||
PurchaseConditions = req.PurchaseOrderPurchaseConditions ?? "Conditions non précisées"
|
||||
};
|
||||
database.PurchaseOrders.Add(purchaseOrder);
|
||||
await database.SaveChangesAsync(ct); // important pour avoir l'Id
|
||||
}
|
||||
|
||||
// Création du lien produit-commande
|
||||
var purchaseProduct = new Models.PurchaseProduct()
|
||||
{
|
||||
ProductId = req.ProductId,
|
||||
PurchaseOrderId = req.PurchaseOrderId,
|
||||
ProductId = product.Id,
|
||||
PurchaseOrderId = purchaseOrder.Id,
|
||||
Quantity = req.Quantity
|
||||
};
|
||||
|
||||
database.PurchaseProducts.Add(purchaseProduct);
|
||||
|
||||
var purchaseOrder = new Models.PurchaseOrder()
|
||||
{
|
||||
PurchaseConditions = req.PurchaseOrderPurchaseConditions,
|
||||
};
|
||||
database.PurchaseOrders.Add(purchaseOrder);
|
||||
|
||||
await database.SaveChangesAsync(ct);
|
||||
|
||||
GetPurchaseProductDto responseDto = new()
|
||||
|
||||
// Prépare la réponse
|
||||
var responseDto = new GetPurchaseProductDto()
|
||||
{
|
||||
ProductId = purchaseProduct.ProductId,
|
||||
PurchaseOrderId = purchaseProduct.PurchaseOrderId,
|
||||
Quantity = purchaseProduct.Quantity,
|
||||
ProductId = product.Id,
|
||||
ProductReferences = int.TryParse(product.Reference, out var refInt) ? refInt : 0,
|
||||
ProductName = product.Name,
|
||||
ProductDuration = product.Duration,
|
||||
ProductCaliber = product.Caliber,
|
||||
ProductApprovalNumber = product.ApprovalNumber,
|
||||
ProductWeight = product.Weight,
|
||||
ProductNec = product.Nec,
|
||||
ProductImage = product.Image,
|
||||
ProductLink = product.Link,
|
||||
ProductMinimalQuantity = product.MinimalQuantity,
|
||||
|
||||
PurchaseOrderId = purchaseOrder.Id,
|
||||
PurchaseOrderPurchaseConditions = purchaseOrder.PurchaseConditions,
|
||||
Quantity = purchaseProduct.Quantity
|
||||
};
|
||||
|
||||
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user