forked from sanchezvem/PyroFetes
MAJ Mathilde
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
using FastEndpoints;
|
||||
using PyroFetes.DTO.Product.Request;
|
||||
using PyroFetes.DTO.Product.Response;
|
||||
using API.DTO.Product.Request;
|
||||
using API.DTO.Product.Response;
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes.Models;
|
||||
|
||||
namespace PyroFetes.Endpoints.Product;
|
||||
|
||||
public class CreateProductEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<CreateProductDto, GetProductDto>
|
||||
public class CreateProductEndpoint(PyroFetesDbContext db)
|
||||
: Endpoint<CreateProductDto, GetProductDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
@@ -14,7 +17,7 @@ public class CreateProductEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endp
|
||||
|
||||
public override async Task HandleAsync(CreateProductDto req, CancellationToken ct)
|
||||
{
|
||||
Models.Product product = new ()
|
||||
var product = new Models.Product
|
||||
{
|
||||
References = req.References,
|
||||
Name = req.Name!,
|
||||
@@ -29,13 +32,48 @@ public class CreateProductEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endp
|
||||
ProductCategoryId = req.ProductCategoryId,
|
||||
ClassificationId = req.ClassificationId
|
||||
};
|
||||
|
||||
pyrofetesdbcontext.Products.Add(product);
|
||||
await pyrofetesdbcontext.SaveChangesAsync(ct);
|
||||
|
||||
Console.WriteLine("Product créé avec succès !");
|
||||
|
||||
GetProductDto responseDto = new ()
|
||||
db.Products.Add(product);
|
||||
await db.SaveChangesAsync(ct);
|
||||
|
||||
// Ajout des fournisseurs liés
|
||||
if (req.Suppliers is not null && req.Suppliers.Any())
|
||||
{
|
||||
foreach (var s in req.Suppliers)
|
||||
{
|
||||
var price = new Price
|
||||
{
|
||||
ProductId = product.Id,
|
||||
SupplierId = s.SupplierId,
|
||||
SellingPrice = s.SellingPrice
|
||||
};
|
||||
db.Prices.Add(price);
|
||||
}
|
||||
await db.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
// Ajout des entrepôts liés
|
||||
if (req.Warehouses is not null && req.Warehouses.Any())
|
||||
{
|
||||
foreach (var w in req.Warehouses)
|
||||
{
|
||||
var exists = await db.Warehouses.AnyAsync(x => x.Id == w.WarehouseId, ct);
|
||||
if (!exists)
|
||||
continue; // sécurité : on ignore les warehouses inexistants
|
||||
|
||||
var warehouseProduct = new WarehouseProduct
|
||||
{
|
||||
ProductId = product.Id,
|
||||
WarehouseId = w.WarehouseId,
|
||||
Quantity = w.Quantity
|
||||
};
|
||||
db.WarehouseProducts.Add(warehouseProduct);
|
||||
}
|
||||
await db.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
// Construction de la réponse
|
||||
var response = new GetProductDto
|
||||
{
|
||||
Id = product.Id,
|
||||
Reference = req.References,
|
||||
@@ -49,9 +87,20 @@ public class CreateProductEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endp
|
||||
Image = req.Image,
|
||||
Link = req.Link,
|
||||
ProductCategoryId = req.ProductCategoryId,
|
||||
ClassificationId = req.ClassificationId
|
||||
ClassificationId = req.ClassificationId,
|
||||
Suppliers = req.Suppliers?.Select(s => new ProductSupplierPriceDto
|
||||
{
|
||||
SupplierId = s.SupplierId,
|
||||
SellingPrice = s.SellingPrice
|
||||
}).ToList() ?? new(),
|
||||
Warehouses = req.Warehouses?.Select(w => new GetProductWarehouseDto
|
||||
{
|
||||
WarehouseId = w.WarehouseId,
|
||||
Quantity = w.Quantity,
|
||||
WarehouseName = db.Warehouses.FirstOrDefault(x => x.Id == w.WarehouseId)?.Name ?? string.Empty
|
||||
}).ToList() ?? new()
|
||||
};
|
||||
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
await Send.OkAsync(response, ct);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user