Files
PyroFetes-Sujet1/PyroFetes/Endpoints/Product/CreateProductEndpoint.cs
2025-11-05 22:38:55 +01:00

106 lines
4.1 KiB
C#

using PyroFetes.DTO.Product.Request;
using PyroFetes.DTO.Product.Response;
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.Models;
namespace PyroFetes.Endpoints.Product;
public class CreateProductEndpoint(PyroFetesDbContext db)
: Endpoint<CreateProductDto, GetProductDto> //Instanciation d'une connexion à la bdd dans un endpoint, utilise l'élément de requête CreateProductDto et l'élement de réponse GetProductDto
{
public override void Configure() //Configuration de l'endpoint
{
Post("/api/products"); //Créer un produit
AllowAnonymous(); //Autorise l'accès sans authentification
}
public override async Task HandleAsync(CreateProductDto req, CancellationToken ct)
{
var product = new Models.Product //Création d'un nom, référence, durée, calibre, numéro d'aprovisionnement, poids, nec, image, lien, liaison de la catégorie du produit et de la classification rentré par l'utilisateur
{
Reference = req.References.ToString(),
Name = req.Name!,
Duration = req.Duration,
Caliber = req.Caliber,
ApprovalNumber = req.ApprovalNumber,
Weight = req.Weight,
Nec = req.Nec,
Image = req.Image!,
Link = req.Link!,
ProductCategoryId = req.ProductCategoryId,
ClassificationId = req.ClassificationId
};
db.Products.Add(product); //Ajout du produit à la bdd
await db.SaveChangesAsync(ct); //Sauvegarde du produit dans la bdd
// 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); //Ajout du prix à la bdd
}
await db.SaveChangesAsync(ct); //Sauvegarde du produit dans la bdd
}
// 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); //Ajout du produit de l'entrepot à la bdd
}
await db.SaveChangesAsync(ct); //Sauvegarde du produit de l'entrepot dans la bdd
}
// 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); //Réponse au client
}
}