MAJ Mathilde

This commit is contained in:
2025-10-09 15:16:08 +02:00
parent f155d03559
commit d1fa3aca68
24 changed files with 628 additions and 211 deletions

View File

@@ -1,6 +1,4 @@
using PyroFetes.Models; namespace API.DTO.Product.Request;
namespace PyroFetes.DTO.Product.Request;
public class CreateProductDto public class CreateProductDto
{ {
@@ -11,11 +9,28 @@ public class CreateProductDto
public int ApprovalNumber { get; set; } public int ApprovalNumber { get; set; }
public decimal Weight { get; set; } public decimal Weight { get; set; }
public decimal Nec { get; set; } public decimal Nec { get; set; }
public decimal SellingPrice {get; set;} public decimal SellingPrice { get; set; }
public string? Image { get; set; } public string? Image { get; set; }
public string? Link { get; set; } public string? Link { get; set; }
public int ClassificationId { get; set;} public int ClassificationId { get; set;}
public int ProductCategoryId { get; set; } public int ProductCategoryId { get; set; }
// Liste des fournisseurs liés au produit
public List<ProductSupplierPriceDto>? Suppliers { get; set; }
// Liste des entrepôts liés au produit (ajoutée ici)
public List<CreateProductWarehouseDto>? Warehouses { get; set; }
}
public class ProductSupplierPriceDto
{
public int SupplierId { get; set; }
public decimal SellingPrice { get; set; }
}
public class CreateProductWarehouseDto
{
public int WarehouseId { get; set; }
public int Quantity { get; set; }
} }

View File

@@ -1,20 +1,32 @@
using PyroFetes.Models; namespace API.DTO.Product.Request;
namespace PyroFetes.DTO.Product.Request;
public class UpdateProductDto public class UpdateProductDto
{ {
public int Id { get; set; } public int Id { get; set; }
public int Reference { get; set; } public int References { get; set; }
public string? Name { get; set; } public string? Name { get; set; }
public decimal Duration { get; set; } public decimal Duration { get; set; }
public decimal Caliber { get; set; } public decimal Caliber { get; set; }
public int ApprovalNumber { get; set; } public int ApprovalNumber { get; set; }
public decimal Weight { get; set; } public decimal Weight { get; set; }
public decimal Nec { get; set; } public decimal Nec { get; set; }
public decimal SellingPrice {get; set;} public decimal SellingPrice { get; set; }
public string? Image { get; set; } public string? Image { get; set; }
public string? Link { get; set; } public string? Link { get; set; }
public int ClassificationId { get; set;} public int ClassificationId { get; set; }
public int ProductCategoryId { get; set; } public int ProductCategoryId { get; set; }
}
// Liste des fournisseurs associés
public List<ProductSupplierPriceDto> Suppliers { get; set; }
// Liste des entrepôts associés (corrigé ici)
public List<UpdateProductWarehouseDto> Warehouses { get; set; }
}
// DTO pour la mise à jour des entrepôts liés
public class UpdateProductWarehouseDto
{
public int WarehouseId { get; set; }
public int Quantity { get; set; }
}

View File

@@ -1,6 +1,6 @@
using PyroFetes.Models; using API.DTO.Product.Request;
namespace PyroFetes.DTO.Product.Response; namespace API.DTO.Product.Response;
public class GetProductDto public class GetProductDto
{ {
@@ -12,12 +12,30 @@ public class GetProductDto
public int ApprovalNumber { get; set; } public int ApprovalNumber { get; set; }
public decimal Weight { get; set; } public decimal Weight { get; set; }
public decimal Nec { get; set; } public decimal Nec { get; set; }
public decimal SellingPrice {get; set;} public decimal SellingPrice { get; set; }
public string? Image { get; set; } public string? Image { get; set; }
public string? Link { get; set; } public string? Link { get; set; }
public int ClassificationId { get; set;} public int ClassificationId { get; set; }
public string? ClassificationLabel { get; set; }
public int ProductCategoryId { get; set; } public int ProductCategoryId { get; set; }
public string? ProductCategoryLabel { get; set; }
// Liste des fournisseurs liés avec leur prix de vente
public List<ProductSupplierPriceDto> Suppliers { get; set; }
public List<GetProductWarehouseDto> Warehouses { get; set; } = new();
}
public class GetProductSupplierDto
{
public int SupplierId { get; set; }
public string SupplierName { get; set; } = string.Empty;
public decimal SellingPrice { get; set; }
}
public class GetProductWarehouseDto
{
public int WarehouseId { get; set; }
public string WarehouseName { get; set; } = string.Empty;
public int Quantity { get; set; }
} }

View File

@@ -8,4 +8,13 @@ public class CreateSupplierDto
public string Adress { get; set; } public string Adress { get; set; }
public int ZipCode { get; set; } public int ZipCode { get; set; }
public string City { get; set; } public string City { get; set; }
// Produits que ce fournisseur fournit
public List<SupplierProductPriceDto>? Products { get; set; }
}
public class SupplierProductPriceDto
{
public int ProductId { get; set; }
public decimal SellingPrice { get; set; }
} }

View File

@@ -9,4 +9,6 @@ public class UpdateSupplierDto
public string Adress { get; set; } public string Adress { get; set; }
public int ZipCode { get; set; } public int ZipCode { get; set; }
public string City { get; set; } public string City { get; set; }
public List<SupplierProductPriceDto>? Products { get; set; }
} }

View File

@@ -1,4 +1,6 @@
namespace API.DTO.Supplier.Response; using API.DTO.Supplier.Request;
namespace API.DTO.Supplier.Response;
public class GetSupplierDto public class GetSupplierDto
{ {
@@ -9,4 +11,14 @@ public class GetSupplierDto
public string Adress { get; set; } public string Adress { get; set; }
public int ZipCode { get; set; } public int ZipCode { get; set; }
public string City { get; set; } public string City { get; set; }
// Liste des produits liés avec leur prix fournisseur
public List<SupplierProductPriceDto> Products { get; set; }
}
public class GetSupplierProductDto
{
public int ProductId { get; set; }
public string ProductName { get; set; } = string.Empty;
public decimal SellingPrice { get; set; }
} }

View File

@@ -2,11 +2,20 @@
public class CreateWarehouseDto public class CreateWarehouseDto
{ {
public string Name {get; set;} public string Name { get; set; }
public int MaxWeight {get; set;} public int MaxWeight { get; set; }
public int Current {get; set;} public int Current { get; set; }
public int MinWeight {get; set;} public int MinWeight { get; set; }
public string Adress { get; set; } public string Adress { get; set; }
public int ZipCode { get; set; } public int ZipCode { get; set; }
public string City { get; set; } public string City { get; set; }
// Liste des produits à stocker dans cet entrepôt
public List<CreateWarehouseProductDto>? Products { get; set; }
} }
public class CreateWarehouseProductDto
{
public int ProductId { get; set; }
public int Quantity { get; set; }
}

View File

@@ -1,13 +1,21 @@
namespace API.DTO.Warehouse.Request; namespace API.DTO.Warehouse.Request;
public class UpdateWarehouseDto public class UpdateWarehouseDto
{ {
public int Id {get; set;} public int Id { get; set; }
public string Name {get; set;} public string Name { get; set; }
public int MaxWeight {get; set;} public int MaxWeight { get; set; }
public int Current {get; set;} public int Current { get; set; }
public int MinWeight {get; set;} public int MinWeight { get; set; }
public string Adress { get; set; } public string Adress { get; set; }
public int ZipCode { get; set; } public int ZipCode { get; set; }
public string City { get; set; } public string City { get; set; }
public List<UpdateWarehouseProductDto>? Products { get; set; }
}
public class UpdateWarehouseProductDto
{
public int ProductId { get; set; }
public int Quantity { get; set; }
} }

View File

@@ -1,13 +1,22 @@
namespace API.DTO.Warehouse.Response; namespace API.DTO.Warehouse.Response;
public class GetWarehouseDto public class GetWarehouseDto
{ {
public int Id {get; set;} public int Id { get; set; }
public string Name {get; set;} public string Name { get; set; }
public int MaxWeight {get; set;} public int MaxWeight { get; set; }
public int Current {get; set;} public int Current { get; set; }
public int MinWeight {get; set;} public int MinWeight { get; set; }
public string Adress { get; set; } public string Adress { get; set; }
public int ZipCode { get; set; } public int ZipCode { get; set; }
public string City { get; set; } public string City { get; set; }
public List<WarehouseProductDto>? Products { get; set; }
}
public class WarehouseProductDto
{
public int ProductId { get; set; }
public string? ProductName { get; set; }
public int Quantity { get; set; }
} }

View File

@@ -1,10 +1,13 @@
using FastEndpoints; using API.DTO.Product.Request;
using PyroFetes.DTO.Product.Request; using API.DTO.Product.Response;
using PyroFetes.DTO.Product.Response; using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.Models;
namespace PyroFetes.Endpoints.Product; namespace PyroFetes.Endpoints.Product;
public class CreateProductEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<CreateProductDto, GetProductDto> public class CreateProductEndpoint(PyroFetesDbContext db)
: Endpoint<CreateProductDto, GetProductDto>
{ {
public override void Configure() public override void Configure()
{ {
@@ -14,7 +17,7 @@ public class CreateProductEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endp
public override async Task HandleAsync(CreateProductDto req, CancellationToken ct) public override async Task HandleAsync(CreateProductDto req, CancellationToken ct)
{ {
Models.Product product = new () var product = new Models.Product
{ {
References = req.References, References = req.References,
Name = req.Name!, Name = req.Name!,
@@ -29,13 +32,48 @@ public class CreateProductEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endp
ProductCategoryId = req.ProductCategoryId, ProductCategoryId = req.ProductCategoryId,
ClassificationId = req.ClassificationId ClassificationId = req.ClassificationId
}; };
pyrofetesdbcontext.Products.Add(product);
await pyrofetesdbcontext.SaveChangesAsync(ct);
Console.WriteLine("Product créé avec succès !");
GetProductDto responseDto = new () 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, Id = product.Id,
Reference = req.References, Reference = req.References,
@@ -49,9 +87,20 @@ public class CreateProductEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endp
Image = req.Image, Image = req.Image,
Link = req.Link, Link = req.Link,
ProductCategoryId = req.ProductCategoryId, ProductCategoryId = req.ProductCategoryId,
ClassificationId = req.ClassificationId 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(responseDto, ct); await Send.OkAsync(response, ct);
} }
} }

View File

@@ -1,5 +1,6 @@
using FastEndpoints; using FastEndpoints;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using PyroFetes.Models;
namespace PyroFetes.Endpoints.Product; namespace PyroFetes.Endpoints.Product;
@@ -8,29 +9,50 @@ public class DeleteProductRequest
public int Id { get; set; } public int Id { get; set; }
} }
public class DeleteProductEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<DeleteProductRequest> public class DeleteProductEndpoint(PyroFetesDbContext db) : Endpoint<DeleteProductRequest>
{ {
public override void Configure() public override void Configure()
{ {
Delete("/api/products/{@id}", x => new { x.Id }); Delete("/api/products/{@id}", x => new { x.Id });
AllowAnonymous(); AllowAnonymous();
} }
public override async Task HandleAsync(DeleteProductRequest req, CancellationToken ct) public override async Task HandleAsync(DeleteProductRequest req, CancellationToken ct)
{ {
Models.Product? productToDelete = await pyrofetesdbcontext // Récupérer le produit
.Products var productToDelete = await db.Products
.SingleOrDefaultAsync(p => p.Id == req.Id, cancellationToken: ct); .SingleOrDefaultAsync(p => p.Id == req.Id, ct);
if (productToDelete == null) if (productToDelete is null)
{ {
Console.WriteLine($"Aucun produit avec l'ID {req.Id} trouvé."); Console.WriteLine($"Aucun produit avec l'ID {req.Id} trouvé.");
await Send.NotFoundAsync(ct); await Send.NotFoundAsync(ct);
return; return;
} }
pyrofetesdbcontext.Products.Remove(productToDelete); // Supprimer les liaisons Price
await pyrofetesdbcontext.SaveChangesAsync(ct); var relatedPrices = await db.Prices
.Where(p => p.ProductId == req.Id)
.ToListAsync(ct);
if (relatedPrices.Any())
{
db.Prices.RemoveRange(relatedPrices);
}
// Supprimer les liaisons WarehouseProduct
var relatedWarehouseProducts = await db.WarehouseProducts
.Where(wp => wp.ProductId == req.Id)
.ToListAsync(ct);
if (relatedWarehouseProducts.Any())
{
db.WarehouseProducts.RemoveRange(relatedWarehouseProducts);
}
// Supprimer le produit
db.Products.Remove(productToDelete);
await db.SaveChangesAsync(ct);
Console.WriteLine($"Produit {req.Id}, ses prix et ses entrepôts liés ont été supprimés avec succès.");
await Send.NoContentAsync(ct); await Send.NoContentAsync(ct);
} }

View File

@@ -1,10 +1,13 @@
using FastEndpoints; using API.DTO.Product.Request;
using API.DTO.Product.Response;
using FastEndpoints;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Product.Response; using PyroFetes.Models;
namespace PyroFetes.Endpoints.Product; namespace PyroFetes.Endpoints.Product;
public class GetAllProductsEndpoint(PyroFetesDbContext pyrofetesdbcontext) : EndpointWithoutRequest<List<GetProductDto>> public class GetAllProductsEndpoint(PyroFetesDbContext db)
: EndpointWithoutRequest<List<GetProductDto>>
{ {
public override void Configure() public override void Configure()
{ {
@@ -14,27 +17,45 @@ public class GetAllProductsEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End
public override async Task HandleAsync(CancellationToken ct) public override async Task HandleAsync(CancellationToken ct)
{ {
List<GetProductDto> responseDto = await pyrofetesdbcontext.Products // Inclure toutes les relations nécessaires : Prices + WarehouseProducts + Warehouse
.Select(p => new GetProductDto() var products = await db.Products
{ .Include(p => p.Prices)
Id = p.Id, .Include(p => p.WarehouseProducts)
Reference = p.References, .ThenInclude(wp => wp.Warehouse)
Name = p.Name, .ToListAsync(ct);
Duration = p.Duration,
Caliber = p.Caliber, var responseDto = products.Select(p => new GetProductDto
ApprovalNumber = p.ApprovalNumber, {
Weight = p.Weight, Id = p.Id,
Nec = p.Nec, Reference = p.References,
SellingPrice = p.SellingPrice, Name = p.Name,
Image = p.Image, Duration = p.Duration,
Link = p.Link, Caliber = p.Caliber,
ClassificationId = p.ClassificationId, ApprovalNumber = p.ApprovalNumber,
ClassificationLabel = p.Classification!.Label, Weight = p.Weight,
ProductCategoryId = p.ProductCategoryId, Nec = p.Nec,
ProductCategoryLabel = p.ProductCategory!.Label, SellingPrice = p.SellingPrice,
} Image = p.Image,
).ToListAsync(ct); Link = p.Link,
ClassificationId = p.ClassificationId,
ProductCategoryId = p.ProductCategoryId,
// Liste des fournisseurs liés via Price
Suppliers = p.Prices.Select(pr => new ProductSupplierPriceDto
{
SupplierId = pr.SupplierId,
SellingPrice = pr.SellingPrice
}).ToList(),
// Liste des entrepôts via WarehouseProduct
Warehouses = p.WarehouseProducts.Select(wp => new GetProductWarehouseDto
{
WarehouseId = wp.WarehouseId,
WarehouseName = wp.Warehouse?.Name ?? string.Empty,
Quantity = wp.Quantity
}).ToList()
}).ToList();
await Send.OkAsync(responseDto, ct); await Send.OkAsync(responseDto, ct);
} }
} }

View File

@@ -1,6 +1,8 @@
using FastEndpoints; using API.DTO.Product.Request;
using API.DTO.Product.Response;
using FastEndpoints;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Product.Response; using PyroFetes.Models;
namespace PyroFetes.Endpoints.Product; namespace PyroFetes.Endpoints.Product;
@@ -9,20 +11,23 @@ public class GetProductRequest
public int Id { get; set; } public int Id { get; set; }
} }
public class GetProductEndpoint(PyroFetesDbContext db)
public class GetProductEndpoint(PyroFetesDbContext pyrofetesdbcontext) :Endpoint<GetProductRequest, GetProductDto> : Endpoint<GetProductRequest, GetProductDto>
{ {
public override void Configure() public override void Configure()
{ {
Get("/api/product/{@id}", x => new { x.Id }); Get("/api/products/{@id}", x => new { x.Id });
AllowAnonymous(); AllowAnonymous();
} }
public override async Task HandleAsync(GetProductRequest req, CancellationToken ct) public override async Task HandleAsync(GetProductRequest req, CancellationToken ct)
{ {
Models.Product? product = await pyrofetesdbcontext // Inclure toutes les relations : Prices + WarehouseProducts + Warehouse
.Products.Include(product => product.Classification).Include(product => product.ProductCategory) var product = await db.Products
.SingleOrDefaultAsync(p => p.Id == req.Id, cancellationToken: ct); .Include(p => p.Prices)
.Include(p => p.WarehouseProducts)
.ThenInclude(wp => wp.Warehouse)
.SingleOrDefaultAsync(p => p.Id == req.Id, ct);
if (product == null) if (product == null)
{ {
@@ -31,7 +36,7 @@ public class GetProductEndpoint(PyroFetesDbContext pyrofetesdbcontext) :Endpoint
return; return;
} }
GetProductDto responseDto = new() var responseDto = new GetProductDto
{ {
Id = product.Id, Id = product.Id,
Reference = product.References, Reference = product.References,
@@ -45,11 +50,24 @@ public class GetProductEndpoint(PyroFetesDbContext pyrofetesdbcontext) :Endpoint
Image = product.Image, Image = product.Image,
Link = product.Link, Link = product.Link,
ClassificationId = product.ClassificationId, ClassificationId = product.ClassificationId,
ClassificationLabel = product.Classification!.Label, ProductCategoryId = product.ProductCategoryId,
ProductCategoryId = product.ProductCategoryId,
ProductCategoryLabel = product.ProductCategory!.Label, // Fournisseurs liés via Price
Suppliers = product.Prices.Select(pr => new ProductSupplierPriceDto
{
SupplierId = pr.SupplierId,
SellingPrice = pr.SellingPrice
}).ToList(),
// Entrepôts liés via WarehouseProduct
Warehouses = product.WarehouseProducts.Select(wp => new GetProductWarehouseDto
{
WarehouseId = wp.WarehouseId,
WarehouseName = wp.Warehouse?.Name ?? string.Empty,
Quantity = wp.Quantity
}).ToList()
}; };
await Send.OkAsync(responseDto, ct); await Send.OkAsync(responseDto, ct);
} }
} }

View File

@@ -1,47 +1,88 @@
using FastEndpoints; using API.DTO.Product.Request;
using API.DTO.Product.Response;
using FastEndpoints;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Product.Request; using PyroFetes.Models;
using PyroFetes.DTO.Product.Response;
namespace PyroFetes.Endpoints.Product; 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() public override void Configure()
{ {
// Route HTTP PUT avec un paramètre d'identifiant dans l'URL
Put("/api/products/{@id}", x => new { x.Id }); Put("/api/products/{@id}", x => new { x.Id });
// Autorise les requêtes anonymes (sans authentification)
AllowAnonymous(); AllowAnonymous();
} }
public override async Task HandleAsync(UpdateProductDto req, CancellationToken ct) public override async Task HandleAsync(UpdateProductDto req, CancellationToken ct)
{ {
Models.Product? productToEdit = await pyrofetesdbcontext // Recherche du produit à mettre à jour, en incluant les relations Prices et WarehouseProducts
.Products var product = await db.Products
.SingleOrDefaultAsync(p => p.Id == req.Id, cancellationToken: ct); .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); await Send.NotFoundAsync(ct);
return; return;
} }
productToEdit.References = req.Reference; // Mise à jour des propriétés principales du produit
productToEdit.Name = req.Name; product.References = req.References;
productToEdit.Duration = req.Duration; product.Name = req.Name;
productToEdit.Caliber = req.Caliber; product.Duration = req.Duration;
productToEdit.ApprovalNumber = req.ApprovalNumber; product.Caliber = req.Caliber;
productToEdit.Weight = req.Weight; product.ApprovalNumber = req.ApprovalNumber;
productToEdit.Nec = req.Nec; product.Weight = req.Weight;
productToEdit.SellingPrice = req.SellingPrice; product.Nec = req.Nec;
productToEdit.Image = req.Image; product.SellingPrice = req.SellingPrice;
productToEdit.Link = req.Link; product.Image = req.Image;
await pyrofetesdbcontext.SaveChangesAsync(ct); 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, db.Prices.Add(new Price
Reference = req.Reference, {
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, Name = req.Name,
Duration = req.Duration, Duration = req.Duration,
Caliber = req.Caliber, Caliber = req.Caliber,
@@ -50,9 +91,27 @@ public class UpdateProductEndpoint(PyroFetesDbContext pyrofetesdbcontext) :Endpo
Nec = req.Nec, Nec = req.Nec,
SellingPrice = req.SellingPrice, SellingPrice = req.SellingPrice,
Image = req.Image, 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);
} }
} }

View File

@@ -1,10 +1,13 @@
using API.DTO.Supplier.Request; using API.DTO.Supplier.Request;
using API.DTO.Supplier.Response; using API.DTO.Supplier.Response;
using FastEndpoints; using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.Models;
namespace PyroFetes.Endpoints.Supplier; namespace PyroFetes.Endpoints.Supplier;
public class CreateSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<CreateSupplierDto, GetSupplierDto> public class CreateSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext)
: Endpoint<CreateSupplierDto, GetSupplierDto>
{ {
public override void Configure() public override void Configure()
{ {
@@ -14,8 +17,7 @@ public class CreateSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End
public override async Task HandleAsync(CreateSupplierDto req, CancellationToken ct) public override async Task HandleAsync(CreateSupplierDto req, CancellationToken ct)
{ {
// Création d'un nouvel objet Supplier var supplier = new Models.Supplier
Models.Supplier supplier = new()
{ {
Name = req.Name, Name = req.Name,
Email = req.Email, Email = req.Email,
@@ -24,15 +26,27 @@ public class CreateSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End
ZipCode = req.ZipCode, ZipCode = req.ZipCode,
City = req.City City = req.City
}; };
// Ajout à la base et sauvegarde
pyrofetesdbcontext.Suppliers.Add(supplier); pyrofetesdbcontext.Suppliers.Add(supplier);
await pyrofetesdbcontext.SaveChangesAsync(ct); await pyrofetesdbcontext.SaveChangesAsync(ct);
Console.WriteLine("Fournisseur créé avec succès !");
// Préparation de la réponse // Ajout des liaisons Price si produits renseignés
GetSupplierDto responseDto = new() if (req.Products is not null && req.Products.Any())
{
foreach (var p in req.Products)
{
var price = new Price
{
SupplierId = supplier.Id,
ProductId = p.ProductId,
SellingPrice = p.SellingPrice
};
pyrofetesdbcontext.Prices.Add(price);
}
await pyrofetesdbcontext.SaveChangesAsync(ct);
}
var response = new GetSupplierDto
{ {
Id = supplier.Id, Id = supplier.Id,
Name = supplier.Name, Name = supplier.Name,
@@ -43,6 +57,6 @@ public class CreateSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End
City = supplier.City City = supplier.City
}; };
await Send.OkAsync(responseDto, ct); await Send.OkAsync(response, ct);
} }
} }

View File

@@ -1,5 +1,6 @@
using FastEndpoints; using FastEndpoints;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using PyroFetes.Models;
namespace PyroFetes.Endpoints.Supplier; namespace PyroFetes.Endpoints.Supplier;
@@ -18,19 +19,32 @@ public class DeleteSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End
public override async Task HandleAsync(DeleteSupplierRequest req, CancellationToken ct) public override async Task HandleAsync(DeleteSupplierRequest req, CancellationToken ct)
{ {
Models.Supplier? supplierToDelete = await pyrofetesdbcontext var supplierToDelete = await pyrofetesdbcontext
.Suppliers .Suppliers
.SingleOrDefaultAsync(s => s.Id == req.Id, cancellationToken: ct); .SingleOrDefaultAsync(s => s.Id == req.Id, ct);
if (supplierToDelete == null) if (supplierToDelete is null)
{ {
Console.WriteLine($"Aucun fournisseur avec l'ID {req.Id} trouvé."); Console.WriteLine($"Aucun fournisseur avec l'ID {req.Id} trouvé.");
await Send.NotFoundAsync(ct); await Send.NotFoundAsync(ct);
return; return;
} }
// Supprimer les liaisons Price avant le fournisseur
var relatedPrices = await pyrofetesdbcontext.Prices
.Where(p => p.SupplierId == req.Id)
.ToListAsync(ct);
if (relatedPrices.Any())
{
pyrofetesdbcontext.Prices.RemoveRange(relatedPrices);
}
// Supprimer le fournisseur
pyrofetesdbcontext.Suppliers.Remove(supplierToDelete); pyrofetesdbcontext.Suppliers.Remove(supplierToDelete);
await pyrofetesdbcontext.SaveChangesAsync(ct); await pyrofetesdbcontext.SaveChangesAsync(ct);
Console.WriteLine($"Fournisseur {req.Id} et ses prix liés supprimés avec succès.");
await Send.NoContentAsync(ct); await Send.NoContentAsync(ct);
} }

View File

@@ -1,10 +1,13 @@
using API.DTO.Supplier.Response; using API.DTO.Supplier.Response;
using API.DTO.Supplier.Request;
using FastEndpoints; using FastEndpoints;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using PyroFetes.Models;
namespace PyroFetes.Endpoints.Supplier; namespace PyroFetes.Endpoints.Supplier;
public class GetAllSuppliersEndpoint(PyroFetesDbContext pyrofetesdbcontext) : EndpointWithoutRequest<List<GetSupplierDto>> public class GetAllSuppliersEndpoint(PyroFetesDbContext pyrofetesdbcontext)
: EndpointWithoutRequest<List<GetSupplierDto>>
{ {
public override void Configure() public override void Configure()
{ {
@@ -14,19 +17,28 @@ public class GetAllSuppliersEndpoint(PyroFetesDbContext pyrofetesdbcontext) : En
public override async Task HandleAsync(CancellationToken ct) public override async Task HandleAsync(CancellationToken ct)
{ {
List<GetSupplierDto> responseDto = await pyrofetesdbcontext.Suppliers var suppliers = await pyrofetesdbcontext.Suppliers
.Select(s => new GetSupplierDto .Include(s => s.Prices)
{
Id = s.Id,
Name = s.Name!,
Email = s.Email!,
PhoneNumber = s.Phone!,
Adress = s.Address!,
ZipCode = s.ZipCode,
City = s.City!
})
.ToListAsync(ct); .ToListAsync(ct);
var responseDto = suppliers.Select(s => new GetSupplierDto
{
Id = s.Id,
Name = s.Name!,
Email = s.Email!,
PhoneNumber = s.Phone!,
Adress = s.Address!,
ZipCode = s.ZipCode,
City = s.City!,
// 🔹 Liste des produits liés via Price
Products = s.Prices.Select(pr => new SupplierProductPriceDto
{
ProductId = pr.ProductId,
SellingPrice = pr.SellingPrice
}).ToList()
}).ToList();
await Send.OkAsync(responseDto, ct); await Send.OkAsync(responseDto, ct);
} }
} }

View File

@@ -1,4 +1,5 @@
using API.DTO.Supplier.Response; using API.DTO.Supplier.Request;
using API.DTO.Supplier.Response;
using FastEndpoints; using FastEndpoints;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@@ -9,7 +10,8 @@ public class GetSupplierRequest
public int Id { get; set; } public int Id { get; set; }
} }
public class GetSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<GetSupplierRequest, GetSupplierDto> public class GetSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext)
: Endpoint<GetSupplierRequest, GetSupplierDto>
{ {
public override void Configure() public override void Configure()
{ {
@@ -19,9 +21,9 @@ public class GetSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoi
public override async Task HandleAsync(GetSupplierRequest req, CancellationToken ct) public override async Task HandleAsync(GetSupplierRequest req, CancellationToken ct)
{ {
Models.Supplier? supplier = await pyrofetesdbcontext var supplier = await pyrofetesdbcontext.Suppliers
.Suppliers .Include(s => s.Prices)
.SingleOrDefaultAsync(s => s.Id == req.Id, cancellationToken: ct); .SingleOrDefaultAsync(s => s.Id == req.Id, ct);
if (supplier == null) if (supplier == null)
{ {
@@ -30,7 +32,7 @@ public class GetSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoi
return; return;
} }
GetSupplierDto responseDto = new() var responseDto = new GetSupplierDto
{ {
Id = supplier.Id, Id = supplier.Id,
Name = supplier.Name!, Name = supplier.Name!,
@@ -38,7 +40,14 @@ public class GetSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoi
PhoneNumber = supplier.Phone!, PhoneNumber = supplier.Phone!,
Adress = supplier.Address!, Adress = supplier.Address!,
ZipCode = supplier.ZipCode, ZipCode = supplier.ZipCode,
City = supplier.City! City = supplier.City!,
// Produits liés
Products = supplier.Prices.Select(p => new SupplierProductPriceDto
{
ProductId = p.ProductId,
SellingPrice = p.SellingPrice
}).ToList()
}; };
await Send.OkAsync(responseDto, ct); await Send.OkAsync(responseDto, ct);

View File

@@ -2,6 +2,7 @@
using API.DTO.Supplier.Response; using API.DTO.Supplier.Response;
using FastEndpoints; using FastEndpoints;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using PyroFetes.Models;
namespace PyroFetes.Endpoints.Supplier; namespace PyroFetes.Endpoints.Supplier;
@@ -15,18 +16,16 @@ public class UpdateSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End
public override async Task HandleAsync(UpdateSupplierDto req, CancellationToken ct) public override async Task HandleAsync(UpdateSupplierDto req, CancellationToken ct)
{ {
Models.Supplier? supplierToEdit = await pyrofetesdbcontext var supplierToEdit = await pyrofetesdbcontext
.Suppliers .Suppliers
.SingleOrDefaultAsync(s => s.Id == req.Id, cancellationToken: ct); .Include(s => s.Prices)
.SingleOrDefaultAsync(s => s.Id == req.Id, ct);
if (supplierToEdit == null) if (supplierToEdit is null)
{ {
Console.WriteLine($"Aucun fournisseur avec l'ID {req.Id} trouvé.");
await Send.NotFoundAsync(ct); await Send.NotFoundAsync(ct);
return; return;
} }
// Mise à jour des propriétés
supplierToEdit.Name = req.Name; supplierToEdit.Name = req.Name;
supplierToEdit.Email = req.Email; supplierToEdit.Email = req.Email;
supplierToEdit.Phone = req.PhoneNumber; supplierToEdit.Phone = req.PhoneNumber;
@@ -34,9 +33,28 @@ public class UpdateSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End
supplierToEdit.ZipCode = req.ZipCode; supplierToEdit.ZipCode = req.ZipCode;
supplierToEdit.City = req.City; supplierToEdit.City = req.City;
if (req.Products is not null)
{
// Supprimer les anciennes liaisons
var existingPrices = pyrofetesdbcontext.Prices
.Where(p => p.SupplierId == supplierToEdit.Id);
pyrofetesdbcontext.Prices.RemoveRange(existingPrices);
// Ajouter les nouvelles liaisons
foreach (var p in req.Products)
{
pyrofetesdbcontext.Prices.Add(new Price
{
SupplierId = supplierToEdit.Id,
ProductId = p.ProductId,
SellingPrice = p.SellingPrice
});
}
}
await pyrofetesdbcontext.SaveChangesAsync(ct); await pyrofetesdbcontext.SaveChangesAsync(ct);
GetSupplierDto responseDto = new() var response = new GetSupplierDto
{ {
Id = supplierToEdit.Id, Id = supplierToEdit.Id,
Name = supplierToEdit.Name, Name = supplierToEdit.Name,
@@ -47,6 +65,6 @@ public class UpdateSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End
City = supplierToEdit.City City = supplierToEdit.City
}; };
await Send.OkAsync(responseDto, ct); await Send.OkAsync(response, ct);
} }
} }

View File

@@ -1,10 +1,12 @@
using API.DTO.Warehouse.Request; using API.DTO.Warehouse.Request;
using API.DTO.Warehouse.Response; using API.DTO.Warehouse.Response;
using FastEndpoints; using FastEndpoints;
using PyroFetes.Models;
namespace PyroFetes.Endpoints.Warehouse; namespace PyroFetes.Endpoints.Warehouse;
public class CreateWarehouseEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<CreateWarehouseDto, GetWarehouseDto> public class CreateWarehouseEndpoint(PyroFetesDbContext db)
: Endpoint<CreateWarehouseDto, GetWarehouseDto>
{ {
public override void Configure() public override void Configure()
{ {
@@ -14,7 +16,7 @@ public class CreateWarehouseEndpoint(PyroFetesDbContext pyrofetesdbcontext) : En
public override async Task HandleAsync(CreateWarehouseDto req, CancellationToken ct) public override async Task HandleAsync(CreateWarehouseDto req, CancellationToken ct)
{ {
Models.Warehouse warehouse = new() var warehouse = new Models.Warehouse
{ {
Name = req.Name, Name = req.Name,
MaxWeight = req.MaxWeight, MaxWeight = req.MaxWeight,
@@ -24,13 +26,27 @@ public class CreateWarehouseEndpoint(PyroFetesDbContext pyrofetesdbcontext) : En
ZipCode = req.ZipCode, ZipCode = req.ZipCode,
City = req.City City = req.City
}; };
pyrofetesdbcontext.Warehouses.Add(warehouse);
await pyrofetesdbcontext.SaveChangesAsync(ct);
Console.WriteLine("Entrepôt créé avec succès !");
GetWarehouseDto responseDto = new() db.Warehouses.Add(warehouse);
await db.SaveChangesAsync(ct);
// 🔹 Ajout des produits liés à cet entrepôt
if (req.Products is not null && req.Products.Any())
{
foreach (var p in req.Products)
{
var warehouseProduct = new WarehouseProduct
{
WarehouseId = warehouse.Id,
ProductId = p.ProductId,
Quantity = p.Quantity
};
db.WarehouseProducts.Add(warehouseProduct);
}
await db.SaveChangesAsync(ct);
}
var response = new GetWarehouseDto
{ {
Id = warehouse.Id, Id = warehouse.Id,
Name = warehouse.Name, Name = warehouse.Name,
@@ -42,6 +58,6 @@ public class CreateWarehouseEndpoint(PyroFetesDbContext pyrofetesdbcontext) : En
City = warehouse.City City = warehouse.City
}; };
await Send.OkAsync(responseDto, ct); await Send.OkAsync(response, ct);
} }
} }

View File

@@ -1,5 +1,6 @@
using FastEndpoints; using FastEndpoints;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using PyroFetes.Models;
namespace PyroFetes.Endpoints.Warehouse; namespace PyroFetes.Endpoints.Warehouse;
@@ -8,18 +9,20 @@ public class DeleteWarehouseRequest
public int Id { get; set; } public int Id { get; set; }
} }
public class DeleteWarehouseEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<DeleteWarehouseRequest> public class DeleteWarehouseEndpoint(PyroFetesDbContext db) : Endpoint<DeleteWarehouseRequest>
{ {
public override void Configure() public override void Configure()
{ {
Delete("/api/warehouse/{@id}", x => new { x.Id }); // Lannotation correcte du paramètre est {id}, pas {@id}
Delete("/api/warehouse/{id}", x => new { x.Id });
AllowAnonymous(); AllowAnonymous();
} }
public override async Task HandleAsync(DeleteWarehouseRequest req, CancellationToken ct) public override async Task HandleAsync(DeleteWarehouseRequest req, CancellationToken ct)
{ {
Models.Warehouse? warehouseToDelete = await pyrofetesdbcontext // On charge aussi les WarehouseProducts liés pour les supprimer proprement
.Warehouses var warehouseToDelete = await db.Warehouses
.Include(w => w.WarehouseProducts)
.SingleOrDefaultAsync(w => w.Id == req.Id, cancellationToken: ct); .SingleOrDefaultAsync(w => w.Id == req.Id, cancellationToken: ct);
if (warehouseToDelete == null) if (warehouseToDelete == null)
@@ -29,9 +32,21 @@ public class DeleteWarehouseEndpoint(PyroFetesDbContext pyrofetesdbcontext) : En
return; return;
} }
pyrofetesdbcontext.Warehouses.Remove(warehouseToDelete); // 🔹 Suppression des relations WarehouseProduct avant l'entrepôt
await pyrofetesdbcontext.SaveChangesAsync(ct); var relatedWarehouseProducts = await db.WarehouseProducts
.Where(wp => wp.WarehouseId == req.Id)
.ToListAsync(ct);
if (relatedWarehouseProducts.Any())
{
db.WarehouseProducts.RemoveRange(relatedWarehouseProducts);
}
// 🔹 Suppression de lentrepôt
db.Warehouses.Remove(warehouseToDelete);
await db.SaveChangesAsync(ct);
Console.WriteLine($"Entrepôt {warehouseToDelete.Name} (ID {req.Id}) supprimé avec succès.");
await Send.NoContentAsync(ct); await Send.NoContentAsync(ct);
} }
} }

View File

@@ -1,10 +1,13 @@
using API.DTO.Warehouse.Response; using API.DTO.Warehouse.Response;
using API.DTO.Warehouse.Request;
using FastEndpoints; using FastEndpoints;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using PyroFetes.Models;
namespace PyroFetes.Endpoints.Warehouse; namespace PyroFetes.Endpoints.Warehouse;
public class GetAllWarehouseEndpoint(PyroFetesDbContext pyrofetesdbcontext) : EndpointWithoutRequest<List<GetWarehouseDto>> public class GetAllWarehouseEndpoint(PyroFetesDbContext db)
: EndpointWithoutRequest<List<GetWarehouseDto>>
{ {
public override void Configure() public override void Configure()
{ {
@@ -14,20 +17,30 @@ public class GetAllWarehouseEndpoint(PyroFetesDbContext pyrofetesdbcontext) : En
public override async Task HandleAsync(CancellationToken ct) public override async Task HandleAsync(CancellationToken ct)
{ {
List<GetWarehouseDto> responseDto = await pyrofetesdbcontext.Warehouses // 🔹 On inclut les relations avec WarehouseProducts et Product
.Select(w => new GetWarehouseDto var warehouses = await db.Warehouses
{ .Include(w => w.WarehouseProducts)
Id = w.Id, .ThenInclude(wp => wp.Product)
Name = w.Name,
MaxWeight = w.MaxWeight,
Current = w.Current,
MinWeight = w.MinWeight,
Adress = w.Address,
ZipCode = w.ZipCode,
City = w.City
})
.ToListAsync(ct); .ToListAsync(ct);
await Send.OkAsync(responseDto, ct); var response = warehouses.Select(w => new GetWarehouseDto
{
Id = w.Id,
Name = w.Name,
MaxWeight = w.MaxWeight,
Current = w.Current,
MinWeight = w.MinWeight,
Adress = w.Address,
ZipCode = w.ZipCode,
City = w.City,
Products = w.WarehouseProducts.Select(wp => new WarehouseProductDto
{
ProductId = wp.ProductId,
ProductName = wp.Product?.Name,
Quantity = wp.Quantity
}).ToList()
}).ToList();
await Send.OkAsync(response, ct);
} }
} }

View File

@@ -1,6 +1,7 @@
using API.DTO.Warehouse.Response; using API.DTO.Warehouse.Response;
using FastEndpoints; using FastEndpoints;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using PyroFetes.Models;
namespace PyroFetes.Endpoints.Warehouse; namespace PyroFetes.Endpoints.Warehouse;
@@ -9,28 +10,32 @@ public class GetWarehouseRequest
public int Id { get; set; } public int Id { get; set; }
} }
public class GetWarehouseEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<GetWarehouseRequest, GetWarehouseDto> public class GetWarehouseEndpoint(PyroFetesDbContext db)
: Endpoint<GetWarehouseRequest, GetWarehouseDto>
{ {
public override void Configure() public override void Configure()
{ {
Get("/api/warehouses/{@id}", x => new { x.Id }); // Pas de "@id" ici, juste {id}
Get("/api/warehouses/{id}", x => new { x.Id });
AllowAnonymous(); AllowAnonymous();
} }
public override async Task HandleAsync(GetWarehouseRequest req, CancellationToken ct) public override async Task HandleAsync(GetWarehouseRequest req, CancellationToken ct)
{ {
Models.Warehouse? warehouse = await pyrofetesdbcontext // 🔹 Inclut les produits associés à cet entrepôt
.Warehouses var warehouse = await db.Warehouses
.Include(w => w.WarehouseProducts)
.ThenInclude(wp => wp.Product)
.SingleOrDefaultAsync(w => w.Id == req.Id, cancellationToken: ct); .SingleOrDefaultAsync(w => w.Id == req.Id, cancellationToken: ct);
if (warehouse == null) if (warehouse == null)
{ {
Console.WriteLine($"Aucun entrepôt avec l'ID {req.Id} trouvé."); Console.WriteLine($" Aucun entrepôt avec l'ID {req.Id} trouvé.");
await Send.NotFoundAsync(ct); await Send.NotFoundAsync(ct);
return; return;
} }
GetWarehouseDto responseDto = new() var response = new GetWarehouseDto
{ {
Id = warehouse.Id, Id = warehouse.Id,
Name = warehouse.Name!, Name = warehouse.Name!,
@@ -39,9 +44,15 @@ public class GetWarehouseEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpo
MinWeight = warehouse.MinWeight, MinWeight = warehouse.MinWeight,
Adress = warehouse.Address!, Adress = warehouse.Address!,
ZipCode = warehouse.ZipCode, ZipCode = warehouse.ZipCode,
City = warehouse.City! City = warehouse.City!,
Products = warehouse.WarehouseProducts.Select(wp => new WarehouseProductDto
{
ProductId = wp.ProductId,
ProductName = wp.Product?.Name,
Quantity = wp.Quantity
}).ToList()
}; };
await Send.OkAsync(responseDto, ct); await Send.OkAsync(response, ct);
} }
} }

View File

@@ -2,21 +2,25 @@
using API.DTO.Warehouse.Response; using API.DTO.Warehouse.Response;
using FastEndpoints; using FastEndpoints;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using PyroFetes.Models;
namespace PyroFetes.Endpoints.Warehouse; namespace PyroFetes.Endpoints.Warehouse;
public class UpdateWarehouseEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<UpdateWarehouseDto, GetWarehouseDto> public class UpdateWarehouseEndpoint(PyroFetesDbContext db)
: Endpoint<UpdateWarehouseDto, GetWarehouseDto>
{ {
public override void Configure() public override void Configure()
{ {
Put("/api/warehouses/{@id}", x => new { x.Id }); // Utilise {id} plutôt que {@id}
Put("/api/warehouses/{id}", x => new { x.Id });
AllowAnonymous(); AllowAnonymous();
} }
public override async Task HandleAsync(UpdateWarehouseDto req, CancellationToken ct) public override async Task HandleAsync(UpdateWarehouseDto req, CancellationToken ct)
{ {
Models.Warehouse? warehouseToEdit = await pyrofetesdbcontext // 🔹 On inclut les produits existants pour pouvoir les modifier
.Warehouses var warehouseToEdit = await db.Warehouses
.Include(w => w.WarehouseProducts)
.SingleOrDefaultAsync(w => w.Id == req.Id, cancellationToken: ct); .SingleOrDefaultAsync(w => w.Id == req.Id, cancellationToken: ct);
if (warehouseToEdit == null) if (warehouseToEdit == null)
@@ -26,7 +30,7 @@ public class UpdateWarehouseEndpoint(PyroFetesDbContext pyrofetesdbcontext) : En
return; return;
} }
// Mise à jour des champs // 🔹 Mise à jour des champs de base
warehouseToEdit.Name = req.Name; warehouseToEdit.Name = req.Name;
warehouseToEdit.MaxWeight = req.MaxWeight; warehouseToEdit.MaxWeight = req.MaxWeight;
warehouseToEdit.Current = req.Current; warehouseToEdit.Current = req.Current;
@@ -35,9 +39,30 @@ public class UpdateWarehouseEndpoint(PyroFetesDbContext pyrofetesdbcontext) : En
warehouseToEdit.ZipCode = req.ZipCode; warehouseToEdit.ZipCode = req.ZipCode;
warehouseToEdit.City = req.City; warehouseToEdit.City = req.City;
await pyrofetesdbcontext.SaveChangesAsync(ct); // 🔹 Gestion des produits associés
if (req.Products is not null)
{
// On supprime les anciens liens pour recréer proprement
var existingLinks = warehouseToEdit.WarehouseProducts.ToList();
if (existingLinks.Any())
db.WarehouseProducts.RemoveRange(existingLinks);
GetWarehouseDto responseDto = new() foreach (var p in req.Products)
{
var newLink = new WarehouseProduct
{
WarehouseId = warehouseToEdit.Id,
ProductId = p.ProductId,
Quantity = p.Quantity
};
db.WarehouseProducts.Add(newLink);
}
}
await db.SaveChangesAsync(ct);
// 🔹 On renvoie la version mise à jour
var response = new GetWarehouseDto
{ {
Id = warehouseToEdit.Id, Id = warehouseToEdit.Id,
Name = warehouseToEdit.Name, Name = warehouseToEdit.Name,
@@ -46,9 +71,16 @@ public class UpdateWarehouseEndpoint(PyroFetesDbContext pyrofetesdbcontext) : En
MinWeight = warehouseToEdit.MinWeight, MinWeight = warehouseToEdit.MinWeight,
Adress = warehouseToEdit.Address, Adress = warehouseToEdit.Address,
ZipCode = warehouseToEdit.ZipCode, ZipCode = warehouseToEdit.ZipCode,
City = warehouseToEdit.City City = warehouseToEdit.City,
Products = warehouseToEdit.WarehouseProducts.Select(wp => new WarehouseProductDto
{
ProductId = wp.ProductId,
ProductName = wp.Product?.Name,
Quantity = wp.Quantity
}).ToList()
}; };
await Send.OkAsync(responseDto, ct); Console.WriteLine($"Entrepôt {warehouseToEdit.Name} mis à jour avec succès.");
await Send.OkAsync(response, ct);
} }
} }