forked from sanchezvem/PyroFetes
120 lines
4.2 KiB
C#
120 lines
4.2 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;
|
|
|
|
// Endpoint permettant de mettre à jour un produit existant
|
|
public class UpdateProductEndpoint(PyroFetesDbContext db)
|
|
: Endpoint<UpdateProductDto, GetProductDto>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
// Route HTTP PUT avec un paramètre d'identifiant dans l'URL
|
|
Put("/api/products/{@id}", x => new { x.Id });
|
|
|
|
// Autorise les requêtes anonymes (sans authentification)
|
|
AllowAnonymous();
|
|
}
|
|
|
|
public override async Task HandleAsync(UpdateProductDto req, CancellationToken ct)
|
|
{
|
|
// Recherche du produit à mettre à jour, en incluant les relations Prices et WarehouseProducts
|
|
var product = await db.Products
|
|
.Include(p => p.Prices)
|
|
.Include(p => p.WarehouseProducts)
|
|
.SingleOrDefaultAsync(p => p.Id == req.Id, ct);
|
|
|
|
// Si le produit n'existe pas, on retourne une réponse 404
|
|
if (product is null)
|
|
{
|
|
await Send.NotFoundAsync(ct);
|
|
return;
|
|
}
|
|
|
|
// Mise à jour des propriétés principales du produit
|
|
product.References = req.References;
|
|
product.Name = req.Name;
|
|
product.Duration = req.Duration;
|
|
product.Caliber = req.Caliber;
|
|
product.ApprovalNumber = req.ApprovalNumber;
|
|
product.Weight = req.Weight;
|
|
product.Nec = req.Nec;
|
|
product.SellingPrice = req.SellingPrice;
|
|
product.Image = req.Image;
|
|
product.Link = req.Link;
|
|
product.ClassificationId = req.ClassificationId;
|
|
product.ProductCategoryId = req.ProductCategoryId;
|
|
|
|
// Mise à jour des prix fournisseurs associés
|
|
// On supprime les anciens enregistrements pour les remplacer
|
|
db.Prices.RemoveRange(product.Prices);
|
|
foreach (var s in req.Suppliers)
|
|
{
|
|
db.Prices.Add(new Price
|
|
{
|
|
ProductId = product.Id,
|
|
SupplierId = s.SupplierId,
|
|
SellingPrice = s.SellingPrice
|
|
});
|
|
}
|
|
|
|
// Mise à jour des entrepôts associés
|
|
// On supprime les anciens liens avant d'ajouter les nouveaux
|
|
db.WarehouseProducts.RemoveRange(product.WarehouseProducts);
|
|
foreach (var w in req.Warehouses)
|
|
{
|
|
db.WarehouseProducts.Add(new WarehouseProduct
|
|
{
|
|
ProductId = product.Id,
|
|
WarehouseId = w.WarehouseId,
|
|
Quantity = w.Quantity
|
|
});
|
|
}
|
|
|
|
// Sauvegarde des modifications dans la base de données
|
|
await db.SaveChangesAsync(ct);
|
|
|
|
// Construction de la réponse renvoyée au client
|
|
// On reconstruit les listes Suppliers et Warehouses au bon format de DTO
|
|
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,
|
|
ClassificationId = req.ClassificationId,
|
|
ProductCategoryId = req.ProductCategoryId,
|
|
|
|
// Mapping des fournisseurs pour la réponse
|
|
Suppliers = req.Suppliers.Select(s => new ProductSupplierPriceDto
|
|
{
|
|
SupplierId = s.SupplierId,
|
|
SellingPrice = s.SellingPrice
|
|
}).ToList(),
|
|
|
|
// Mapping des entrepôts pour la réponse
|
|
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()
|
|
};
|
|
|
|
// Envoi de la réponse HTTP 200 avec les données du produit mis à jour
|
|
await Send.OkAsync(response, ct);
|
|
}
|
|
}
|