Creating purchaseproduct's endpoint
This commit is contained in:
@@ -10,7 +10,6 @@ public class GetPurchaseProductDto
|
|||||||
public int ProductApprovalNumber { get; set; }
|
public int ProductApprovalNumber { get; set; }
|
||||||
public decimal ProductWeight { get; set; }
|
public decimal ProductWeight { get; set; }
|
||||||
public decimal ProductNec { get; set; }
|
public decimal ProductNec { get; set; }
|
||||||
public decimal ProductSellingPrice { get; set; }
|
|
||||||
public string? ProductImage { get; set; }
|
public string? ProductImage { get; set; }
|
||||||
public string? ProductLink { get; set; }
|
public string? ProductLink { get; set; }
|
||||||
public int ProductMinimalQuantity { get; set; }
|
public int ProductMinimalQuantity { get; set; }
|
||||||
|
@@ -5,7 +5,8 @@ using PyroFetes.DTO.PurchaseProduct.Response;
|
|||||||
|
|
||||||
namespace PyroFetes.Endpoints.PurchaseProduct;
|
namespace PyroFetes.Endpoints.PurchaseProduct;
|
||||||
|
|
||||||
public class CreatePurchaseProductEndpoint(PyroFetesDbContext database) : Endpoint<CreatePurchaseProductDto, GetPurchaseProductDto>
|
public class CreatePurchaseProductEndpoint(PyroFetesDbContext database)
|
||||||
|
: Endpoint<CreatePurchaseProductDto, GetPurchaseProductDto>
|
||||||
{
|
{
|
||||||
public override void Configure()
|
public override void Configure()
|
||||||
{
|
{
|
||||||
@@ -15,36 +16,57 @@ public class CreatePurchaseProductEndpoint(PyroFetesDbContext database) : Endpoi
|
|||||||
|
|
||||||
public override async Task HandleAsync(CreatePurchaseProductDto req, CancellationToken ct)
|
public override async Task HandleAsync(CreatePurchaseProductDto req, CancellationToken ct)
|
||||||
{
|
{
|
||||||
var purchaseOrderExists = await database.PurchaseOrders.FirstOrDefaultAsync(a => a.Id == req.PurchaseOrderId, ct);
|
// Vérifie que le produit existe
|
||||||
|
var product = await database.Products.FirstOrDefaultAsync(p => p.Id == req.ProductId, ct);
|
||||||
if (purchaseOrderExists == null)
|
if (product == null)
|
||||||
{
|
{
|
||||||
await Send.NoContentAsync(ct);
|
await Send.NotFoundAsync(ct);
|
||||||
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);
|
||||||
|
|
||||||
|
// 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()
|
var purchaseProduct = new Models.PurchaseProduct()
|
||||||
{
|
{
|
||||||
ProductId = req.ProductId,
|
ProductId = product.Id,
|
||||||
PurchaseOrderId = req.PurchaseOrderId,
|
PurchaseOrderId = purchaseOrder.Id,
|
||||||
Quantity = req.Quantity
|
Quantity = req.Quantity
|
||||||
};
|
};
|
||||||
|
|
||||||
database.PurchaseProducts.Add(purchaseProduct);
|
database.PurchaseProducts.Add(purchaseProduct);
|
||||||
|
|
||||||
var purchaseOrder = new Models.PurchaseOrder()
|
|
||||||
{
|
|
||||||
PurchaseConditions = req.PurchaseOrderPurchaseConditions,
|
|
||||||
};
|
|
||||||
database.PurchaseOrders.Add(purchaseOrder);
|
|
||||||
|
|
||||||
await database.SaveChangesAsync(ct);
|
await database.SaveChangesAsync(ct);
|
||||||
|
|
||||||
GetPurchaseProductDto responseDto = new()
|
// Prépare la réponse
|
||||||
|
var responseDto = new GetPurchaseProductDto()
|
||||||
{
|
{
|
||||||
ProductId = purchaseProduct.ProductId,
|
ProductId = product.Id,
|
||||||
PurchaseOrderId = purchaseProduct.PurchaseOrderId,
|
ProductReferences = int.TryParse(product.Reference, out var refInt) ? refInt : 0,
|
||||||
Quantity = purchaseProduct.Quantity,
|
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,
|
PurchaseOrderPurchaseConditions = purchaseOrder.PurchaseConditions,
|
||||||
|
Quantity = purchaseProduct.Quantity
|
||||||
};
|
};
|
||||||
|
|
||||||
await Send.OkAsync(responseDto, ct);
|
await Send.OkAsync(responseDto, ct);
|
||||||
|
Reference in New Issue
Block a user