Ajout de Login et du model réuni avec tout les autres Sujet

This commit is contained in:
2025-10-16 16:30:17 +02:00
parent b3347fe163
commit 2112605cf3
20 changed files with 340 additions and 36 deletions

View File

@@ -2,8 +2,6 @@
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;
@@ -21,14 +19,13 @@ public class CreateProductEndpoint(PyroFetesDbContext db)
{
var product = new Models.Product
{
References = req.References,
Reference = req.References.ToString(),
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,

View File

@@ -1,9 +1,7 @@
using PyroFetes.DTO.Product.Request;
using PyroFetes.DTO.Product.Response;
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;
@@ -23,20 +21,22 @@ public class GetAllProductsEndpoint(PyroFetesDbContext db)
var products = await db.Products
.Include(p => p.Prices)
.Include(p => p.WarehouseProducts)
.ThenInclude(wp => wp.Warehouse)
.ThenInclude(wp => wp.Warehouse)
.ToListAsync(ct);
var responseDto = products.Select(p => new GetProductDto
{
Id = p.Id,
Reference = p.References,
// Le modèle Product contient "Reference" (string) — pas "References" (int)
Reference = int.TryParse(p.Reference, out var refInt) ? refInt : 0,
Name = p.Name,
Duration = p.Duration,
Caliber = p.Caliber,
ApprovalNumber = p.ApprovalNumber,
Weight = p.Weight,
Nec = p.Nec,
SellingPrice = p.SellingPrice,
// Le prix de vente nest pas dans Product, on le récupère via Prices
SellingPrice = p.Prices.FirstOrDefault()?.SellingPrice ?? 0,
Image = p.Image,
Link = p.Link,
ClassificationId = p.ClassificationId,
@@ -49,7 +49,7 @@ public class GetAllProductsEndpoint(PyroFetesDbContext db)
SellingPrice = pr.SellingPrice
}).ToList(),
// Liste des entrepôts via WarehouseProduct
// Liste des entrepôts liés via WarehouseProduct
Warehouses = p.WarehouseProducts.Select(wp => new GetProductWarehouseDto
{
WarehouseId = wp.WarehouseId,
@@ -60,4 +60,4 @@ public class GetAllProductsEndpoint(PyroFetesDbContext db)
await Send.OkAsync(responseDto, ct);
}
}
}

View File

@@ -1,9 +1,7 @@
using PyroFetes.DTO.Product.Request;
using PyroFetes.DTO.Product.Response;
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;
@@ -41,14 +39,20 @@ public class GetProductEndpoint(PyroFetesDbContext db)
var responseDto = new GetProductDto
{
Id = product.Id,
Reference = product.References,
// Le modèle Product contient "Reference" (string), pas "References" (int)
Reference = int.TryParse(product.Reference, out var refInt) ? refInt : 0,
Name = product.Name,
Duration = product.Duration,
Caliber = product.Caliber,
ApprovalNumber = product.ApprovalNumber,
Weight = product.Weight,
Nec = product.Nec,
SellingPrice = product.SellingPrice,
// Le prix de vente nest pas dans Product → récupéré via Price
SellingPrice = product.Prices.FirstOrDefault()?.SellingPrice ?? 0,
Image = product.Image,
Link = product.Link,
ClassificationId = product.ClassificationId,
@@ -72,4 +76,4 @@ public class GetProductEndpoint(PyroFetesDbContext db)
await Send.OkAsync(responseDto, ct);
}
}
}

View File

@@ -2,8 +2,6 @@
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;
@@ -37,21 +35,19 @@ public class UpdateProductEndpoint(PyroFetesDbContext db)
}
// Mise à jour des propriétés principales du produit
product.References = req.References;
product.Reference = req.References.ToString(); // Converti int → string
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)
{
@@ -64,7 +60,6 @@ public class UpdateProductEndpoint(PyroFetesDbContext db)
}
// 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)
{
@@ -76,44 +71,41 @@ public class UpdateProductEndpoint(PyroFetesDbContext db)
});
}
// 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,
Reference = req.References, // DTO garde int pour cohérence
Name = req.Name,
Duration = req.Duration,
Caliber = req.Caliber,
ApprovalNumber = req.ApprovalNumber,
Weight = req.Weight,
Nec = req.Nec,
SellingPrice = req.SellingPrice,
// Le prix de vente est pris depuis Prices
SellingPrice = req.Suppliers.FirstOrDefault()?.SellingPrice ?? 0,
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
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);
}
}