forked from sanchezvem/PyroFetes
109 lines
3.5 KiB
C#
109 lines
3.5 KiB
C#
using PyroFetes.DTO.Product.Request;
|
|
using PyroFetes.DTO.Product.Response;
|
|
using FastEndpoints;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using PyroFetes.DTO.Product.Request;
|
|
using PyroFetes.DTO.Product.Response;
|
|
using PyroFetes.Models;
|
|
|
|
namespace PyroFetes.Endpoints.Product;
|
|
|
|
public class CreateProductEndpoint(PyroFetesDbContext db)
|
|
: Endpoint<CreateProductDto, GetProductDto>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Post("/api/products");
|
|
AllowAnonymous();
|
|
}
|
|
|
|
public override async Task HandleAsync(CreateProductDto req, CancellationToken ct)
|
|
{
|
|
var product = new Models.Product
|
|
{
|
|
References = req.References,
|
|
Name = req.Name!,
|
|
Duration = req.Duration,
|
|
Caliber = req.Caliber,
|
|
ApprovalNumber = req.ApprovalNumber,
|
|
Weight = req.Weight,
|
|
Nec = req.Nec,
|
|
SellingPrice = req.SellingPrice,
|
|
Image = req.Image!,
|
|
Link = req.Link!,
|
|
ProductCategoryId = req.ProductCategoryId,
|
|
ClassificationId = req.ClassificationId
|
|
};
|
|
|
|
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,
|
|
Name = req.Name,
|
|
Duration = req.Duration,
|
|
Caliber = req.Caliber,
|
|
ApprovalNumber = req.ApprovalNumber,
|
|
Weight = req.Weight,
|
|
Nec = req.Nec,
|
|
SellingPrice = req.SellingPrice,
|
|
Image = req.Image,
|
|
Link = req.Link,
|
|
ProductCategoryId = req.ProductCategoryId,
|
|
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(response, ct);
|
|
}
|
|
}
|