forked from sanchezvem/PyroFetes
MAJ Mathilde
This commit is contained in:
@@ -1,47 +1,88 @@
|
||||
using FastEndpoints;
|
||||
using API.DTO.Product.Request;
|
||||
using API.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 UpdateProductEndpoint(PyroFetesDbContext pyrofetesdbcontext) :Endpoint<UpdateProductDto, GetProductDto>
|
||||
// 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)
|
||||
{
|
||||
Models.Product? productToEdit = await pyrofetesdbcontext
|
||||
.Products
|
||||
.SingleOrDefaultAsync(p => p.Id == req.Id, 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);
|
||||
|
||||
if (productToEdit == null)
|
||||
// Si le produit n'existe pas, on retourne une réponse 404
|
||||
if (product is null)
|
||||
{
|
||||
Console.WriteLine($"Aucun produit avec l'ID {req.Id} trouvé.");
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
productToEdit.References = req.Reference;
|
||||
productToEdit.Name = req.Name;
|
||||
productToEdit.Duration = req.Duration;
|
||||
productToEdit.Caliber = req.Caliber;
|
||||
productToEdit.ApprovalNumber = req.ApprovalNumber;
|
||||
productToEdit.Weight = req.Weight;
|
||||
productToEdit.Nec = req.Nec;
|
||||
productToEdit.SellingPrice = req.SellingPrice;
|
||||
productToEdit.Image = req.Image;
|
||||
productToEdit.Link = req.Link;
|
||||
await pyrofetesdbcontext.SaveChangesAsync(ct);
|
||||
// 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;
|
||||
|
||||
GetProductDto responseDto = new()
|
||||
// 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)
|
||||
{
|
||||
Id = req.Id,
|
||||
Reference = req.Reference,
|
||||
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,
|
||||
@@ -50,9 +91,27 @@ public class UpdateProductEndpoint(PyroFetesDbContext pyrofetesdbcontext) :Endpo
|
||||
Nec = req.Nec,
|
||||
SellingPrice = req.SellingPrice,
|
||||
Image = req.Image,
|
||||
Link = req.Link
|
||||
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()
|
||||
};
|
||||
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
// Envoi de la réponse HTTP 200 avec les données du produit mis à jour
|
||||
await Send.OkAsync(response, ct);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user