diff --git a/PyroFetes/DTO/Brand/Request/CreateBrandDto.cs b/PyroFetes/DTO/Brand/Request/CreateBrandDto.cs index 4d5e3f0..bf455c5 100644 --- a/PyroFetes/DTO/Brand/Request/CreateBrandDto.cs +++ b/PyroFetes/DTO/Brand/Request/CreateBrandDto.cs @@ -1,4 +1,4 @@ -namespace PyroFetes.DTO.Brand.Request; +namespace API.DTO.Brand.Request; public class CreateBrandDto { diff --git a/PyroFetes/DTO/Brand/Request/UpdateBrandDto.cs b/PyroFetes/DTO/Brand/Request/UpdateBrandDto.cs index c111b09..4773281 100644 --- a/PyroFetes/DTO/Brand/Request/UpdateBrandDto.cs +++ b/PyroFetes/DTO/Brand/Request/UpdateBrandDto.cs @@ -1,4 +1,4 @@ -namespace PyroFetes.DTO.Brand.Request; +namespace API.DTO.Brand.Request; public class UpdateBrandDto { diff --git a/PyroFetes/DTO/Brand/Response/GetBrandDto.cs b/PyroFetes/DTO/Brand/Response/GetBrandDto.cs index e57228f..4e12c35 100644 --- a/PyroFetes/DTO/Brand/Response/GetBrandDto.cs +++ b/PyroFetes/DTO/Brand/Response/GetBrandDto.cs @@ -1,4 +1,4 @@ -namespace PyroFetes.DTO.Brand.Response; +namespace API.DTO.Brand.Response; public class GetBrandDto { diff --git a/PyroFetes/DTO/Classification/Request/CreateClassificationDto.cs b/PyroFetes/DTO/Classification/Request/CreateClassificationDto.cs index dfa6238..a351241 100644 --- a/PyroFetes/DTO/Classification/Request/CreateClassificationDto.cs +++ b/PyroFetes/DTO/Classification/Request/CreateClassificationDto.cs @@ -1,4 +1,4 @@ -namespace PyroFetes.DTO.Classification.Request; +namespace API.DTO.Classification.Request; public class CreateClassificationDto { diff --git a/PyroFetes/DTO/Classification/Request/UpdateClassificationDto.cs b/PyroFetes/DTO/Classification/Request/UpdateClassificationDto.cs index 2cc2b8d..2ecc0a6 100644 --- a/PyroFetes/DTO/Classification/Request/UpdateClassificationDto.cs +++ b/PyroFetes/DTO/Classification/Request/UpdateClassificationDto.cs @@ -1,4 +1,4 @@ -namespace PyroFetes.DTO.Classification.Request; +namespace API.DTO.Classification.Request; public class UpdateClassificationDto { diff --git a/PyroFetes/DTO/Classification/Response/GetClassificationDto.cs b/PyroFetes/DTO/Classification/Response/GetClassificationDto.cs index e7a1cae..f86f615 100644 --- a/PyroFetes/DTO/Classification/Response/GetClassificationDto.cs +++ b/PyroFetes/DTO/Classification/Response/GetClassificationDto.cs @@ -1,4 +1,4 @@ -namespace PyroFetes.DTO.Classification.Response; +namespace API.DTO.Classification.Response; public class GetClassificationDto { diff --git a/PyroFetes/DTO/Color/Request/CreateColorDto.cs b/PyroFetes/DTO/Color/Request/CreateColorDto.cs index 43b1851..943d708 100644 --- a/PyroFetes/DTO/Color/Request/CreateColorDto.cs +++ b/PyroFetes/DTO/Color/Request/CreateColorDto.cs @@ -1,4 +1,4 @@ -namespace PyroFetes.DTO.Color.Request; +namespace API.DTO.Color.Request; public class CreateColorDto { diff --git a/PyroFetes/DTO/Color/Request/UpdateColorDto.cs b/PyroFetes/DTO/Color/Request/UpdateColorDto.cs index 3838a49..3a3b3c5 100644 --- a/PyroFetes/DTO/Color/Request/UpdateColorDto.cs +++ b/PyroFetes/DTO/Color/Request/UpdateColorDto.cs @@ -1,4 +1,4 @@ -namespace PyroFetes.DTO.Color.Request; +namespace API.DTO.Color.Request; public class UpdateColorDto { diff --git a/PyroFetes/DTO/Color/Response/GetColorDto.cs b/PyroFetes/DTO/Color/Response/GetColorDto.cs index d53c0fc..61b49ac 100644 --- a/PyroFetes/DTO/Color/Response/GetColorDto.cs +++ b/PyroFetes/DTO/Color/Response/GetColorDto.cs @@ -1,4 +1,4 @@ -namespace PyroFetes.DTO.Color.Response; +namespace API.DTO.Color.Response; public class GetColorDto { diff --git a/PyroFetes/DTO/Effect/Request/CreateEffectDto.cs b/PyroFetes/DTO/Effect/Request/CreateEffectDto.cs index cb6f630..43621e6 100644 --- a/PyroFetes/DTO/Effect/Request/CreateEffectDto.cs +++ b/PyroFetes/DTO/Effect/Request/CreateEffectDto.cs @@ -1,4 +1,4 @@ -namespace PyroFetes.DTO.Effect.Request; +namespace API.DTO.Effect.Request; public class CreateEffectDto { diff --git a/PyroFetes/DTO/Effect/Request/UpdateEffectDto.cs b/PyroFetes/DTO/Effect/Request/UpdateEffectDto.cs index 004a0c5..40a2957 100644 --- a/PyroFetes/DTO/Effect/Request/UpdateEffectDto.cs +++ b/PyroFetes/DTO/Effect/Request/UpdateEffectDto.cs @@ -1,4 +1,4 @@ -namespace PyroFetes.DTO.Effect.Request; +namespace API.DTO.Effect.Request; public class UpdateEffectDto { diff --git a/PyroFetes/DTO/Effect/Response/GetEffectDto.cs b/PyroFetes/DTO/Effect/Response/GetEffectDto.cs index 1708fc6..d37051d 100644 --- a/PyroFetes/DTO/Effect/Response/GetEffectDto.cs +++ b/PyroFetes/DTO/Effect/Response/GetEffectDto.cs @@ -1,4 +1,4 @@ -namespace PyroFetes.DTO.Effect.Response; +namespace API.DTO.Effect.Response; public class GetEffectDto { diff --git a/PyroFetes/DTO/Material/Request/CreateMaterialDto.cs b/PyroFetes/DTO/Material/Request/CreateMaterialDto.cs index 54fecd5..4806b90 100644 --- a/PyroFetes/DTO/Material/Request/CreateMaterialDto.cs +++ b/PyroFetes/DTO/Material/Request/CreateMaterialDto.cs @@ -1,4 +1,4 @@ -namespace PyroFetes.DTO.Material.Request; +namespace API.DTO.Material.Request; public class CreateMaterialDto { diff --git a/PyroFetes/DTO/Material/Request/UpdateMaterialDto.cs b/PyroFetes/DTO/Material/Request/UpdateMaterialDto.cs index e462751..73b9ffd 100644 --- a/PyroFetes/DTO/Material/Request/UpdateMaterialDto.cs +++ b/PyroFetes/DTO/Material/Request/UpdateMaterialDto.cs @@ -1,4 +1,4 @@ -namespace PyroFetes.DTO.Material.Request; +namespace API.DTO.Material.Request; public class UpdateMaterialDto { diff --git a/PyroFetes/DTO/Material/Response/GetMaterialDto.cs b/PyroFetes/DTO/Material/Response/GetMaterialDto.cs index bad4c77..a74e2b6 100644 --- a/PyroFetes/DTO/Material/Response/GetMaterialDto.cs +++ b/PyroFetes/DTO/Material/Response/GetMaterialDto.cs @@ -1,4 +1,4 @@ -namespace PyroFetes.DTO.Material.Response; +namespace API.DTO.Material.Response; public class GetMaterialDto { diff --git a/PyroFetes/DTO/Product/Request/CreateProductDto.cs b/PyroFetes/DTO/Product/Request/CreateProductDto.cs index a704c38..542ac42 100644 --- a/PyroFetes/DTO/Product/Request/CreateProductDto.cs +++ b/PyroFetes/DTO/Product/Request/CreateProductDto.cs @@ -1,21 +1,26 @@ -using PyroFetes.Models; +using PyroFetes.DTO.Product.Request; -namespace PyroFetes.DTO.Product.Request; - -public class CreateProductDto +namespace PyroFetes.DTO.Product.Request { - public int References { get; set; } - public string? Name { get; set; } - public decimal Duration { get; set; } - public decimal Caliber { get; set; } - public int ApprovalNumber { get; set; } - public decimal Weight { get; set; } - public decimal Nec { get; set; } - public decimal SellingPrice {get; set;} - public string? Image { get; set; } - public string? Link { get; set; } - - public int ClassificationId { get; set;} - public int ProductCategoryId { get; set; } - + public class CreateProductDto + { + public int References { get; set; } + public string? Name { get; set; } + public decimal Duration { get; set; } + public decimal Caliber { get; set; } + public int ApprovalNumber { get; set; } + public decimal Weight { get; set; } + public decimal Nec { get; set; } + public decimal SellingPrice { get; set; } + public string? Image { get; set; } + public string? Link { get; set; } + public int ClassificationId { get; set;} + public int ProductCategoryId { get; set; } + + // Liste des fournisseurs liés au produit + public List? Suppliers { get; set; } + + // Liste des entrepôts liés au produit + public List? Warehouses { get; set; } + } } \ No newline at end of file diff --git a/PyroFetes/DTO/Product/Request/UpdateProductDto.cs b/PyroFetes/DTO/Product/Request/UpdateProductDto.cs index 1bc96a4..59ab8e5 100644 --- a/PyroFetes/DTO/Product/Request/UpdateProductDto.cs +++ b/PyroFetes/DTO/Product/Request/UpdateProductDto.cs @@ -1,20 +1,27 @@ -using PyroFetes.Models; +using PyroFetes.DTO.Product.Request; -namespace PyroFetes.DTO.Product.Request; - -public class UpdateProductDto +namespace PyroFetes.DTO.Product.Request { - public int Id { get; set; } - public int Reference { get; set; } - public string? Name { get; set; } - public decimal Duration { get; set; } - public decimal Caliber { get; set; } - public int ApprovalNumber { get; set; } - public decimal Weight { get; set; } - public decimal Nec { get; set; } - public decimal SellingPrice {get; set;} - public string? Image { get; set; } - public string? Link { get; set; } - public int ClassificationId { get; set;} - public int ProductCategoryId { get; set; } + public class UpdateProductDto + { + public int Id { get; set; } + public int References { get; set; } + public string? Name { get; set; } + public decimal Duration { get; set; } + public decimal Caliber { get; set; } + public int ApprovalNumber { get; set; } + public decimal Weight { get; set; } + public decimal Nec { get; set; } + public decimal SellingPrice { get; set; } + public string? Image { get; set; } + public string? Link { get; set; } + public int ClassificationId { get; set; } + public int ProductCategoryId { get; set; } + + // Liste des fournisseurs associés + public List Suppliers { get; set; } = new(); + + // Liste des entrepôts associés + public List Warehouses { get; set; } = new(); + } } \ No newline at end of file diff --git a/PyroFetes/DTO/Product/Response/GetProductDto.cs b/PyroFetes/DTO/Product/Response/GetProductDto.cs index dbfed6a..48763bf 100644 --- a/PyroFetes/DTO/Product/Response/GetProductDto.cs +++ b/PyroFetes/DTO/Product/Response/GetProductDto.cs @@ -1,23 +1,29 @@ -using PyroFetes.Models; +using PyroFetes.DTO.Product.Request; +using PyroFetes.DTO.Product.Request; +using PyroFetes.DTO.Product.Response; -namespace PyroFetes.DTO.Product.Response; - -public class GetProductDto +namespace PyroFetes.DTO.Product.Response { - public int Id { get; set; } - public int Reference { get; set; } - public string? Name { get; set; } - public decimal Duration { get; set; } - public decimal Caliber { get; set; } - public int ApprovalNumber { get; set; } - public decimal Weight { get; set; } - public decimal Nec { get; set; } - public decimal SellingPrice {get; set;} - public string? Image { get; set; } - public string? Link { get; set; } - public int ClassificationId { get; set;} - public string? ClassificationLabel { get; set; } - public int ProductCategoryId { get; set; } - public string? ProductCategoryLabel { get; set; } - + public class GetProductDto + { + public int Id { get; set; } + public int Reference { get; set; } + public string? Name { get; set; } + public decimal Duration { get; set; } + public decimal Caliber { get; set; } + public int ApprovalNumber { get; set; } + public decimal Weight { get; set; } + public decimal Nec { get; set; } + public decimal SellingPrice { get; set; } + public string? Image { get; set; } + public string? Link { get; set; } + public int ClassificationId { get; set; } + public int ProductCategoryId { get; set; } + + // Fournisseurs liés + public List Suppliers { get; set; } = new(); + + // Entrepôts liés + public List Warehouses { get; set; } = new(); + } } \ No newline at end of file diff --git a/PyroFetes/DTO/ProductSupplierPrice/Request/ProductSupplierPriceDto.cs b/PyroFetes/DTO/ProductSupplierPrice/Request/ProductSupplierPriceDto.cs new file mode 100644 index 0000000..f48ba0a --- /dev/null +++ b/PyroFetes/DTO/ProductSupplierPrice/Request/ProductSupplierPriceDto.cs @@ -0,0 +1,10 @@ +namespace PyroFetes.DTO.Product.Request +{ + // DTO utilisé pour créer ou mettre à jour la relation Product <-> Supplier + public class ProductSupplierPriceDto + { + public int ProductId { get; set; } // Id du produit (pour update) + public int SupplierId { get; set; } // Id du fournisseur + public decimal SellingPrice { get; set; } // Prix de vente + } +} diff --git a/PyroFetes/DTO/ProductSupplierPrice/Response/GetProductSupplierDto.cs b/PyroFetes/DTO/ProductSupplierPrice/Response/GetProductSupplierDto.cs new file mode 100644 index 0000000..a423279 --- /dev/null +++ b/PyroFetes/DTO/ProductSupplierPrice/Response/GetProductSupplierDto.cs @@ -0,0 +1,11 @@ +namespace PyroFetes.DTO.Product.Response +{ +// DTO pour la lecture des fournisseurs liés à un produit +public class GetProductSupplierDto +{ + public int ProductId { get; set; } + public int SupplierId { get; set; } + public string SupplierName { get; set; } = string.Empty; + public decimal SellingPrice { get; set; } +} +} diff --git a/PyroFetes/DTO/ProductWarehouse/Request/ProductWarehouseDto.cs b/PyroFetes/DTO/ProductWarehouse/Request/ProductWarehouseDto.cs new file mode 100644 index 0000000..a74cab2 --- /dev/null +++ b/PyroFetes/DTO/ProductWarehouse/Request/ProductWarehouseDto.cs @@ -0,0 +1,17 @@ +namespace PyroFetes.DTO.Product.Request +{ + // DTO utilisé pour créer ou mettre à jour la relation Product <-> Warehouse + public class CreateProductWarehouseDto + { + public int WarehouseId { get; set; } + public int ProductId { get; set; } + public int Quantity { get; set; } + } + + public class UpdateProductWarehouseDto + { + public int WarehouseId { get; set; } + public int ProductId { get; set; } + public int Quantity { get; set; } + } +} \ No newline at end of file diff --git a/PyroFetes/DTO/ProductWarehouse/Response/GetProductWarehouseDto.cs b/PyroFetes/DTO/ProductWarehouse/Response/GetProductWarehouseDto.cs new file mode 100644 index 0000000..40a28fb --- /dev/null +++ b/PyroFetes/DTO/ProductWarehouse/Response/GetProductWarehouseDto.cs @@ -0,0 +1,11 @@ +namespace PyroFetes.DTO.Product.Response +{ + // DTO pour la lecture des entrepôts liés à un produit + public class GetProductWarehouseDto + { + public int WarehouseId { get; set; } + public int ProductId { get; set; } + public string WarehouseName { get; set; } = string.Empty; + public int Quantity { get; set; } + } +} \ No newline at end of file diff --git a/PyroFetes/DTO/Supplier/Request/CreateSupplierDto.cs b/PyroFetes/DTO/Supplier/Request/CreateSupplierDto.cs index 25a94a4..9395f20 100644 --- a/PyroFetes/DTO/Supplier/Request/CreateSupplierDto.cs +++ b/PyroFetes/DTO/Supplier/Request/CreateSupplierDto.cs @@ -1,4 +1,4 @@ -namespace API.DTO.Supplier.Request; +namespace PyroFetes.DTO.Supplier.Request; public class CreateSupplierDto { @@ -8,4 +8,13 @@ public class CreateSupplierDto public string Adress { get; set; } public int ZipCode { get; set; } public string City { get; set; } + + // Produits que ce fournisseur fournit + public List? Products { get; set; } +} + +public class SupplierProductPriceDto +{ + public int ProductId { get; set; } + public decimal SellingPrice { get; set; } } \ No newline at end of file diff --git a/PyroFetes/DTO/Supplier/Request/UpdateSupplierDto.cs b/PyroFetes/DTO/Supplier/Request/UpdateSupplierDto.cs index 20ae5f0..f71d406 100644 --- a/PyroFetes/DTO/Supplier/Request/UpdateSupplierDto.cs +++ b/PyroFetes/DTO/Supplier/Request/UpdateSupplierDto.cs @@ -1,4 +1,4 @@ -namespace API.DTO.Supplier.Request; +namespace PyroFetes.DTO.Supplier.Request; public class UpdateSupplierDto { @@ -9,4 +9,6 @@ public class UpdateSupplierDto public string Adress { get; set; } public int ZipCode { get; set; } public string City { get; set; } + + public List? Products { get; set; } } \ No newline at end of file diff --git a/PyroFetes/DTO/Supplier/Response/GetSupplierDto.cs b/PyroFetes/DTO/Supplier/Response/GetSupplierDto.cs index ddc2365..ba2237c 100644 --- a/PyroFetes/DTO/Supplier/Response/GetSupplierDto.cs +++ b/PyroFetes/DTO/Supplier/Response/GetSupplierDto.cs @@ -1,4 +1,6 @@ -namespace API.DTO.Supplier.Response; +using PyroFetes.DTO.Supplier.Request; + +namespace PyroFetes.DTO.Supplier.Response; public class GetSupplierDto { @@ -9,4 +11,14 @@ public class GetSupplierDto public string Adress { get; set; } public int ZipCode { get; set; } public string City { get; set; } + + // Liste des produits liés avec leur prix fournisseur + public List Products { get; set; } +} + +public class GetSupplierProductDto +{ + public int ProductId { get; set; } + public string ProductName { get; set; } = string.Empty; + public decimal SellingPrice { get; set; } } \ No newline at end of file diff --git a/PyroFetes/DTO/Warehouse/Request/CreateWarehouseDto.cs b/PyroFetes/DTO/Warehouse/Request/CreateWarehouseDto.cs index 9f2e7cb..36563c2 100644 --- a/PyroFetes/DTO/Warehouse/Request/CreateWarehouseDto.cs +++ b/PyroFetes/DTO/Warehouse/Request/CreateWarehouseDto.cs @@ -2,11 +2,20 @@ public class CreateWarehouseDto { - public string Name {get; set;} - public int MaxWeight {get; set;} - public int Current {get; set;} - public int MinWeight {get; set;} + public string Name { get; set; } + public int MaxWeight { get; set; } + public int Current { get; set; } + public int MinWeight { get; set; } public string Adress { get; set; } public int ZipCode { get; set; } public string City { get; set; } + + // Liste des produits à stocker dans cet entrepôt + public List? Products { get; set; } } + +public class CreateWarehouseProductDto +{ + public int ProductId { get; set; } + public int Quantity { get; set; } +} \ No newline at end of file diff --git a/PyroFetes/DTO/Warehouse/Request/UpdateWarehouseDto.cs b/PyroFetes/DTO/Warehouse/Request/UpdateWarehouseDto.cs index a154737..3e843d5 100644 --- a/PyroFetes/DTO/Warehouse/Request/UpdateWarehouseDto.cs +++ b/PyroFetes/DTO/Warehouse/Request/UpdateWarehouseDto.cs @@ -1,13 +1,21 @@ -namespace API.DTO.Warehouse.Request; +namespace API.DTO.Warehouse.Request; public class UpdateWarehouseDto { - public int Id {get; set;} - public string Name {get; set;} - public int MaxWeight {get; set;} - public int Current {get; set;} - public int MinWeight {get; set;} + public int Id { get; set; } + public string Name { get; set; } + public int MaxWeight { get; set; } + public int Current { get; set; } + public int MinWeight { get; set; } public string Adress { get; set; } public int ZipCode { get; set; } public string City { get; set; } + + public List? Products { get; set; } +} + +public class UpdateWarehouseProductDto +{ + public int ProductId { get; set; } + public int Quantity { get; set; } } \ No newline at end of file diff --git a/PyroFetes/DTO/Warehouse/Response/GetWarehouseDto.cs b/PyroFetes/DTO/Warehouse/Response/GetWarehouseDto.cs index 77df53c..3af0328 100644 --- a/PyroFetes/DTO/Warehouse/Response/GetWarehouseDto.cs +++ b/PyroFetes/DTO/Warehouse/Response/GetWarehouseDto.cs @@ -1,13 +1,22 @@ -namespace API.DTO.Warehouse.Response; +namespace API.DTO.Warehouse.Response; public class GetWarehouseDto { - public int Id {get; set;} - public string Name {get; set;} - public int MaxWeight {get; set;} - public int Current {get; set;} - public int MinWeight {get; set;} + public int Id { get; set; } + public string Name { get; set; } + public int MaxWeight { get; set; } + public int Current { get; set; } + public int MinWeight { get; set; } public string Adress { get; set; } public int ZipCode { get; set; } public string City { get; set; } + + public List? Products { get; set; } +} + +public class WarehouseProductDto +{ + public int ProductId { get; set; } + public string? ProductName { get; set; } + public int Quantity { get; set; } } \ No newline at end of file diff --git a/PyroFetes/Endpoints/Brand/CreateBrandEndpoint.cs b/PyroFetes/Endpoints/Brand/CreateBrandEndpoint.cs index 265abc6..19756c9 100644 --- a/PyroFetes/Endpoints/Brand/CreateBrandEndpoint.cs +++ b/PyroFetes/Endpoints/Brand/CreateBrandEndpoint.cs @@ -1,5 +1,5 @@ -using PyroFetes.DTO.Brand.Request; -using PyroFetes.DTO.Brand.Response; +using API.DTO.Brand.Request; +using API.DTO.Brand.Response; using FastEndpoints; namespace PyroFetes.Endpoints.Brand; diff --git a/PyroFetes/Endpoints/Brand/GetAllBrandsEndpoint.cs b/PyroFetes/Endpoints/Brand/GetAllBrandsEndpoint.cs index c4995ae..2f7be29 100644 --- a/PyroFetes/Endpoints/Brand/GetAllBrandsEndpoint.cs +++ b/PyroFetes/Endpoints/Brand/GetAllBrandsEndpoint.cs @@ -1,4 +1,4 @@ -using PyroFetes.DTO.Brand.Response; +using API.DTO.Brand.Response; using FastEndpoints; using Microsoft.EntityFrameworkCore; diff --git a/PyroFetes/Endpoints/Brand/GetBrandEndpoint.cs b/PyroFetes/Endpoints/Brand/GetBrandEndpoint.cs index 86d7da9..0e8dc22 100644 --- a/PyroFetes/Endpoints/Brand/GetBrandEndpoint.cs +++ b/PyroFetes/Endpoints/Brand/GetBrandEndpoint.cs @@ -1,4 +1,4 @@ -using PyroFetes.DTO.Brand.Response; +using API.DTO.Brand.Response; using FastEndpoints; using Microsoft.EntityFrameworkCore; diff --git a/PyroFetes/Endpoints/Brand/UpdateBrandEndpoint.cs b/PyroFetes/Endpoints/Brand/UpdateBrandEndpoint.cs index 652291c..2db66ce 100644 --- a/PyroFetes/Endpoints/Brand/UpdateBrandEndpoint.cs +++ b/PyroFetes/Endpoints/Brand/UpdateBrandEndpoint.cs @@ -1,5 +1,5 @@ -using PyroFetes.DTO.Brand.Request; -using PyroFetes.DTO.Brand.Response; +using API.DTO.Brand.Request; +using API.DTO.Brand.Response; using FastEndpoints; namespace PyroFetes.Endpoints.Brand; @@ -8,7 +8,7 @@ public class UpdateBrandEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoi { public override void Configure() { - Post("/api/brands"); + Put("/api/brands"); AllowAnonymous(); } diff --git a/PyroFetes/Endpoints/Classification/CreateClassificationEndpoint.cs b/PyroFetes/Endpoints/Classification/CreateClassificationEndpoint.cs index bc7ed9e..34184dc 100644 --- a/PyroFetes/Endpoints/Classification/CreateClassificationEndpoint.cs +++ b/PyroFetes/Endpoints/Classification/CreateClassificationEndpoint.cs @@ -1,5 +1,5 @@ -using PyroFetes.DTO.Classification.Request; -using PyroFetes.DTO.Classification.Response; +using API.DTO.Classification.Request; +using API.DTO.Classification.Response; using FastEndpoints; namespace PyroFetes.Endpoints.Classification; diff --git a/PyroFetes/Endpoints/Classification/GetAllClassificationsEndpoint.cs b/PyroFetes/Endpoints/Classification/GetAllClassificationsEndpoint.cs index 7513e7e..b96ab3a 100644 --- a/PyroFetes/Endpoints/Classification/GetAllClassificationsEndpoint.cs +++ b/PyroFetes/Endpoints/Classification/GetAllClassificationsEndpoint.cs @@ -1,4 +1,4 @@ -using PyroFetes.DTO.Classification.Response; +using API.DTO.Classification.Response; using FastEndpoints; using Microsoft.EntityFrameworkCore; diff --git a/PyroFetes/Endpoints/Classification/GetClassificationEndpoint.cs b/PyroFetes/Endpoints/Classification/GetClassificationEndpoint.cs index a972863..fd7dafd 100644 --- a/PyroFetes/Endpoints/Classification/GetClassificationEndpoint.cs +++ b/PyroFetes/Endpoints/Classification/GetClassificationEndpoint.cs @@ -1,4 +1,4 @@ -using PyroFetes.DTO.Classification.Response; +using API.DTO.Classification.Response; using FastEndpoints; using Microsoft.EntityFrameworkCore; diff --git a/PyroFetes/Endpoints/Classification/UpdateClassificationEndpoint.cs b/PyroFetes/Endpoints/Classification/UpdateClassificationEndpoint.cs index cee10d3..8569cc7 100644 --- a/PyroFetes/Endpoints/Classification/UpdateClassificationEndpoint.cs +++ b/PyroFetes/Endpoints/Classification/UpdateClassificationEndpoint.cs @@ -1,5 +1,5 @@ -using PyroFetes.DTO.Classification.Request; -using PyroFetes.DTO.Classification.Response; +using API.DTO.Classification.Request; +using API.DTO.Classification.Response; using FastEndpoints; namespace PyroFetes.Endpoints.Classification; @@ -8,7 +8,7 @@ public class UpdateClassificationEndpoint(PyroFetesDbContext pyrofetesdbcontext) { public override void Configure() { - Post("/api/classifications"); + Put("/api/classifications"); AllowAnonymous(); } diff --git a/PyroFetes/Endpoints/Color/CreateColorEndpoint.cs b/PyroFetes/Endpoints/Color/CreateColorEndpoint.cs index e926e52..4573468 100644 --- a/PyroFetes/Endpoints/Color/CreateColorEndpoint.cs +++ b/PyroFetes/Endpoints/Color/CreateColorEndpoint.cs @@ -1,14 +1,14 @@ -using PyroFetes.DTO.Color.Request; -using PyroFetes.DTO.Color.Response; +using API.DTO.Color.Request; +using API.DTO.Color.Response; using FastEndpoints; namespace PyroFetes.Endpoints.Color; -public class CreateColorEndpoint(PyroFetesDbContext appDbContext) : Endpoint +public class CreateColorEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint { public override void Configure() { - Post("/api/color/create"); + Post("/color/create"); AllowAnonymous(); } @@ -19,8 +19,8 @@ public class CreateColorEndpoint(PyroFetesDbContext appDbContext) : Endpoint +public class DeleteColorEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint { public override void Configure() { - Delete("/api/colors/{@id}", x => new { x.Id }); + Delete("/colors/{@id}", x => new { x.Id }); AllowAnonymous(); } public override async Task HandleAsync(DeleteColorRequest req, CancellationToken ct) { - Models.Color? colorToDelete = await appDbContext + Models.Color? colorToDelete = await pyrofetesdbcontext .Colors .SingleOrDefaultAsync(a => a.Id == req.Id, cancellationToken: ct); @@ -29,8 +29,8 @@ public class DeleteColorEndpoint(PyroFetesDbContext appDbContext) : Endpoint> +public class GetAllColorsEndpoint(PyroFetesDbContext pyrofetesdbcontext) : EndpointWithoutRequest> { public override void Configure() { - Get("/api/colors"); + Get("/colors"); AllowAnonymous(); } public override async Task HandleAsync(CancellationToken ct) { - List responseDto = await appDbContext.Colors + List responseDto = await pyrofetesdbcontext.Colors .Select(a => new GetColorDto { Id = a.Id, diff --git a/PyroFetes/Endpoints/Color/GetColorEndpoint.cs b/PyroFetes/Endpoints/Color/GetColorEndpoint.cs index ad29958..dcacb8a 100644 --- a/PyroFetes/Endpoints/Color/GetColorEndpoint.cs +++ b/PyroFetes/Endpoints/Color/GetColorEndpoint.cs @@ -1,6 +1,5 @@ -using PyroFetes.DTO.Color.Response; +using API.DTO.Color.Response; using FastEndpoints; -using Microsoft.AspNetCore.Authentication; using Microsoft.EntityFrameworkCore; namespace PyroFetes.Endpoints.Color; @@ -10,23 +9,23 @@ public class GetColorRequest public int Id { get; set; } } -public class GetColorEndpoint(PyroFetesDbContext appDbContext) : Endpoint +public class GetColorEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint { public override void Configure() { - Get("/api/colors/{@id}", x => new { x.Id}); + Get("/colors/{@id}", x => new { x.Id}); AllowAnonymous(); } public override async Task HandleAsync(GetColorRequest req, CancellationToken ct) { - Models.Color? color = await appDbContext + Models.Color? color = await pyrofetesdbcontext .Colors .SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct); if (color == null) { - Console.WriteLine($"Aucune couleur avec l'ID {req.Id} trouvé."); + Console.WriteLine("Aucune couleur avec l'ID {req.Id} trouvé."); await Send.NotFoundAsync(ct); return; } diff --git a/PyroFetes/Endpoints/Color/UpdateColorEndpoint.cs b/PyroFetes/Endpoints/Color/UpdateColorEndpoint.cs index 43c9ee9..b05c896 100644 --- a/PyroFetes/Endpoints/Color/UpdateColorEndpoint.cs +++ b/PyroFetes/Endpoints/Color/UpdateColorEndpoint.cs @@ -1,34 +1,33 @@ -using PyroFetes.DTO.Color.Request; -using PyroFetes.DTO.Color.Response; +using API.DTO.Color.Request; +using API.DTO.Color.Response; using FastEndpoints; -using Microsoft.AspNetCore.Server.Kestrel; using Microsoft.EntityFrameworkCore; namespace PyroFetes.Endpoints.Color; -public class UpdateColorEndpoint(PyroFetesDbContext appDbContext) : Endpoint +public class UpdateColorEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint { public override void Configure() { - Put("/api/colors/{@id}", x => new { x.Id }); + Put("/colors/{@id}", x => new { x.Id }); AllowAnonymous(); } public override async Task HandleAsync(UpdateColorDto req, CancellationToken ct) { - Models.Color? colorToEdit = await appDbContext + Models.Color? colorToEdit = await pyrofetesdbcontext .Colors .SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct); if (colorToEdit == null) { - Console.WriteLine($"Aucune couleur avec l'id {req.Id} trouvé."); + Console.WriteLine("Aucune couleur avec l'id {req.Id} trouvé."); await Send.NotFoundAsync(ct); return; } colorToEdit.Label = req.Label; - await appDbContext.SaveChangesAsync(ct); + await pyrofetesdbcontext.SaveChangesAsync(ct); GetColorDto responseDto = new() { diff --git a/PyroFetes/Endpoints/Effect/CreateEffectEndpoint.cs b/PyroFetes/Endpoints/Effect/CreateEffectEndpoint.cs index 1e6aafa..1663ef3 100644 --- a/PyroFetes/Endpoints/Effect/CreateEffectEndpoint.cs +++ b/PyroFetes/Endpoints/Effect/CreateEffectEndpoint.cs @@ -1,14 +1,14 @@ -using PyroFetes.DTO.Effect.Request; -using PyroFetes.DTO.Effect.Response; - +using API.DTO.Effect.Request; +using API.DTO.Effect.Response; using FastEndpoints; + namespace PyroFetes.Endpoints.Effect; -public class CreateEffectEndpoint(PyroFetesDbContext appDbContext) : Endpoint +public class CreateEffectEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint { public override void Configure() { - Post("/api/effect/create"); + Post("/effect/create"); AllowAnonymous(); } @@ -19,8 +19,8 @@ public class CreateEffectEndpoint(PyroFetesDbContext appDbContext) : Endpoint +public class DeleteEffectEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint { public override void Configure() { - Delete("/api/effects/{@id}", x => new { x.Id }); + Delete("/effects/{@id}", x => new { x.Id }); AllowAnonymous(); } public override async Task HandleAsync(DeleteEffectRequest req, CancellationToken ct) { - Models.Effect? effectToDelete = await appDbContext + Models.Effect? effectToDelete = await pyrofetesdbcontext .Effects .SingleOrDefaultAsync(a => a.Id == req.Id, cancellationToken: ct); @@ -28,8 +28,8 @@ public class DeleteEffectEndpoint(PyroFetesDbContext appDbContext) : Endpoint> +public class GetAllEffectsEndpoint(PyroFetesDbContext pyrofetesdbcontext) : EndpointWithoutRequest> { public override void Configure() { - Get("/api/effects"); + Get("/effects"); AllowAnonymous(); } public override async Task HandleAsync(CancellationToken ct) { - List responseDto = await appDbContext.Effects + List responseDto = await pyrofetesdbcontext.Effects .Select(a => new GetEffectDto { Id = a.Id, diff --git a/PyroFetes/Endpoints/Effect/GetEffectEndpoint.cs b/PyroFetes/Endpoints/Effect/GetEffectEndpoint.cs index 2465755..cd2a91d 100644 --- a/PyroFetes/Endpoints/Effect/GetEffectEndpoint.cs +++ b/PyroFetes/Endpoints/Effect/GetEffectEndpoint.cs @@ -1,6 +1,5 @@ -using PyroFetes.DTO.Effect.Response; +using API.DTO.Effect.Response; using FastEndpoints; -using Microsoft.AspNetCore.Authentication; using Microsoft.EntityFrameworkCore; namespace PyroFetes.Endpoints.Effect; @@ -10,23 +9,23 @@ public class GetEffectRequest public int Id { get; set; } } -public class GetEffectEndpoint(PyroFetesDbContext appDbContext) : Endpoint +public class GetEffectEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint { public override void Configure() { - Get("/api/effect/{@id}", x => new { x.Id }); + Get("/effect/{@id}", x => new { x.Id }); AllowAnonymous(); } public override async Task HandleAsync(GetEffectRequest req, CancellationToken ct) { - Models.Effect? effect = await appDbContext + Models.Effect? effect = await pyrofetesdbcontext .Effects .SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct); if (effect == null) { - Console.WriteLine($"Aucun effet avec l'ID {req.Id} trouvé."); + Console.WriteLine("Aucun effet avec l'ID {req.Id} trouvé."); await Send.NotFoundAsync(ct); return; } diff --git a/PyroFetes/Endpoints/Effect/UpdateEffectEndpoint.cs b/PyroFetes/Endpoints/Effect/UpdateEffectEndpoint.cs index 1ff9417..ac5bffe 100644 --- a/PyroFetes/Endpoints/Effect/UpdateEffectEndpoint.cs +++ b/PyroFetes/Endpoints/Effect/UpdateEffectEndpoint.cs @@ -1,34 +1,33 @@ -using PyroFetes.DTO.Effect.Request; -using PyroFetes.DTO.Effect.Response; +using API.DTO.Effect.Request; +using API.DTO.Effect.Response; using FastEndpoints; -using Microsoft.AspNetCore.Server.Kestrel; using Microsoft.EntityFrameworkCore; namespace PyroFetes.Endpoints.Effect; -public class UpdateEffectEndpoint(PyroFetesDbContext appDbContext) : Endpoint +public class UpdateEffectEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint { public override void Configure() { - Put("/api/effect/{@id}", x => new { x.Id }); + Put("/effect/{@id}", x => new { x.Id }); AllowAnonymous(); } public override async Task HandleAsync(UpdateEffectDto req, CancellationToken ct) { - Models.Effect? effectToEdit = await appDbContext + Models.Effect? effectToEdit = await pyrofetesdbcontext .Effects .SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct); if (effectToEdit == null) { - Console.WriteLine($"Aucun effet avec l'id {req.Id} trouvé."); + Console.WriteLine("Aucun effet avec l'id {req.Id} trouvé."); await Send.NotFoundAsync(ct); return; } effectToEdit.Label = req.Label; - await appDbContext.SaveChangesAsync(ct); + await pyrofetesdbcontext.SaveChangesAsync(ct); GetEffectDto responseDto = new() { diff --git a/PyroFetes/Endpoints/Material/CreateMaterialEndpoint.cs b/PyroFetes/Endpoints/Material/CreateMaterialEndpoint.cs index cdb8ab2..545b96d 100644 --- a/PyroFetes/Endpoints/Material/CreateMaterialEndpoint.cs +++ b/PyroFetes/Endpoints/Material/CreateMaterialEndpoint.cs @@ -1,33 +1,34 @@ -using PyroFetes.DTO.Material.Request; -using PyroFetes.DTO.Material.Response; +using API.DTO.Material.Request; +using API.DTO.Material.Response; using FastEndpoints; + namespace PyroFetes.Endpoints.Material; -public class CreateMaterialEndpoint(PyroFetesDbContext appDbContext) : Endpoint +public class CreateMaterialEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint { public override void Configure() { - Post("/api/material/create"); + Post("/material/create"); AllowAnonymous(); } public override async Task HandleAsync(CreateMaterialDto req, CancellationToken ct) { - Models.Material material = new() + Models.Material quantity = new() { - Label = req.Label, + Name = req.Label, Quantity = req.Quantity, WarehouseId = req.WarehouseId, }; - appDbContext.Materials.Add(material); - await appDbContext.SaveChangesAsync(ct); + pyrofetesdbcontext.Materials.Add(quantity); + await pyrofetesdbcontext.SaveChangesAsync(ct); Console.WriteLine("Material added"); GetMaterialDto responseDto = new() { - Id = material.Id, - WarehouseId = material.WarehouseId, + Id = quantity.Id, + WarehouseId = quantity.WarehouseId, Label = req.Label, }; diff --git a/PyroFetes/Endpoints/Material/DeleteMaterialEndpoint.cs b/PyroFetes/Endpoints/Material/DeleteMaterialEndpoint.cs index 6912a26..6fbdd46 100644 --- a/PyroFetes/Endpoints/Material/DeleteMaterialEndpoint.cs +++ b/PyroFetes/Endpoints/Material/DeleteMaterialEndpoint.cs @@ -6,17 +6,17 @@ public class DeleteMaterialRequest { public int Id { get; set; } } -public class DeleteMaterialEndpoint(PyroFetesDbContext appDbContext) : Endpoint +public class DeleteMaterialEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint { public override void Configure() { - Delete("/api/materials/{@id}", x => new { x.Id }); + Delete("/materials/{@id}", x => new { x.Id }); AllowAnonymous(); } public override async Task HandleAsync(DeleteMaterialRequest req, CancellationToken ct) { - Models.Material? materialToDelete = await appDbContext + Models.Material? materialToDelete = await pyrofetesdbcontext .Materials .SingleOrDefaultAsync(a => a.Id == req.Id, cancellationToken: ct); @@ -27,8 +27,8 @@ public class DeleteMaterialEndpoint(PyroFetesDbContext appDbContext) : Endpoint< return; } - appDbContext.Materials.Remove(materialToDelete); - await appDbContext.SaveChangesAsync(ct); + pyrofetesdbcontext.Materials.Remove(materialToDelete); + await pyrofetesdbcontext.SaveChangesAsync(ct); await Send.NoContentAsync(ct); } diff --git a/PyroFetes/Endpoints/Material/GetAllMaterialsEndpoint.cs b/PyroFetes/Endpoints/Material/GetAllMaterialsEndpoint.cs index a809711..9f6416b 100644 --- a/PyroFetes/Endpoints/Material/GetAllMaterialsEndpoint.cs +++ b/PyroFetes/Endpoints/Material/GetAllMaterialsEndpoint.cs @@ -1,24 +1,24 @@ -using PyroFetes.DTO.Material.Response; +using API.DTO.Material.Response; using FastEndpoints; using Microsoft.EntityFrameworkCore; namespace PyroFetes.Endpoints.Material; -public class GetAllMaterialsEndpoint(PyroFetesDbContext appDbContext) : EndpointWithoutRequest> +public class GetAllMaterialsEndpoint(PyroFetesDbContext pyrofetesdbcontext) : EndpointWithoutRequest> { public override void Configure() { - Get("/api/material"); + Get("/material"); AllowAnonymous(); } public override async Task HandleAsync(CancellationToken ct) { - List responseDto = await appDbContext.Materials + List responseDto = await pyrofetesdbcontext.Materials .Select(a => new GetMaterialDto { Id = a.Id, - Label = a.Label, + Label = a.Name, Quantity = a.Quantity, WarehouseId = a.WarehouseId, } diff --git a/PyroFetes/Endpoints/Material/GetMaterialEndpoint.cs b/PyroFetes/Endpoints/Material/GetMaterialEndpoint.cs index 7860ab8..2f60a18 100644 --- a/PyroFetes/Endpoints/Material/GetMaterialEndpoint.cs +++ b/PyroFetes/Endpoints/Material/GetMaterialEndpoint.cs @@ -1,6 +1,5 @@ -using PyroFetes.DTO.Material.Response; +using API.DTO.Material.Response; using FastEndpoints; -using Microsoft.AspNetCore.Authentication; using Microsoft.EntityFrameworkCore; namespace PyroFetes.Endpoints.Material; @@ -10,17 +9,17 @@ public class GetMaterialRequest public int Id { get; set; } } -public class GetMaterialEndpoint(PyroFetesDbContext appDbContext) : Endpoint +public class GetMaterialEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint { public override void Configure() { - Get("/api/material/{@id}", x => new { x.Id }); + Get("/material/{@id}", x => new { x.Id }); AllowAnonymous(); } public override async Task HandleAsync(GetMaterialRequest req, CancellationToken ct) { - Models.Material? material = await appDbContext + Models.Material? material = await pyrofetesdbcontext .Materials .SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct); @@ -34,7 +33,7 @@ public class GetMaterialEndpoint(PyroFetesDbContext appDbContext) : Endpoint +public class UpdateMaterialEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint { public override void Configure() { - Put("/api/material/{@id}", x => new { x.Id }); + Put("/material/{@id}", x => new { x.Id }); AllowAnonymous(); } public override async Task HandleAsync(UpdateMaterialDto req, CancellationToken ct) { - Models.Material? materialToEdit = await appDbContext + Models.Material? materialToEdit = await pyrofetesdbcontext .Materials .SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct); if (materialToEdit == null) { - Console.WriteLine($"Aucun matériel avec l'id {req.Id} trouvé."); + Console.WriteLine("Aucun matériel avec l'id {req.Id} trouvé."); await Send.NotFoundAsync(ct); return; } - materialToEdit.Label = req.Label; + materialToEdit.Name = req.Label; materialToEdit.WarehouseId = req.WarehouseId; - await appDbContext.SaveChangesAsync(ct); + await pyrofetesdbcontext.SaveChangesAsync(ct); GetMaterialDto responseDto = new() { diff --git a/PyroFetes/Endpoints/Movement/UpdateMovementEndpoint.cs b/PyroFetes/Endpoints/Movement/UpdateMovementEndpoint.cs index 151ffc9..9e69545 100644 --- a/PyroFetes/Endpoints/Movement/UpdateMovementEndpoint.cs +++ b/PyroFetes/Endpoints/Movement/UpdateMovementEndpoint.cs @@ -8,7 +8,7 @@ public class UpdateMovementEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End { public override void Configure() { - Post("/api/movements"); + Put("/api/movements"); AllowAnonymous(); } diff --git a/PyroFetes/Endpoints/Product/CreateProductEndpoint.cs b/PyroFetes/Endpoints/Product/CreateProductEndpoint.cs index 5a30b83..5deb4fe 100644 --- a/PyroFetes/Endpoints/Product/CreateProductEndpoint.cs +++ b/PyroFetes/Endpoints/Product/CreateProductEndpoint.cs @@ -1,10 +1,15 @@ -using FastEndpoints; +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; -public class CreateProductEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint +public class CreateProductEndpoint(PyroFetesDbContext db) + : Endpoint { public override void Configure() { @@ -14,7 +19,7 @@ public class CreateProductEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endp public override async Task HandleAsync(CreateProductDto req, CancellationToken ct) { - Models.Product product = new () + var product = new Models.Product { References = req.References, Name = req.Name!, @@ -29,13 +34,48 @@ public class CreateProductEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endp ProductCategoryId = req.ProductCategoryId, 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, Reference = req.References, @@ -49,9 +89,20 @@ public class CreateProductEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endp Image = req.Image, Link = req.Link, 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); } -} \ No newline at end of file +} diff --git a/PyroFetes/Endpoints/Product/DeleteProductEndpoint.cs b/PyroFetes/Endpoints/Product/DeleteProductEndpoint.cs index cf425ea..f400abe 100644 --- a/PyroFetes/Endpoints/Product/DeleteProductEndpoint.cs +++ b/PyroFetes/Endpoints/Product/DeleteProductEndpoint.cs @@ -1,5 +1,6 @@ using FastEndpoints; using Microsoft.EntityFrameworkCore; +using PyroFetes.Models; namespace PyroFetes.Endpoints.Product; @@ -8,29 +9,50 @@ public class DeleteProductRequest public int Id { get; set; } } -public class DeleteProductEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint +public class DeleteProductEndpoint(PyroFetesDbContext db) : Endpoint { public override void Configure() { Delete("/api/products/{@id}", x => new { x.Id }); AllowAnonymous(); } - + public override async Task HandleAsync(DeleteProductRequest req, CancellationToken ct) { - Models.Product? productToDelete = await pyrofetesdbcontext - .Products - .SingleOrDefaultAsync(p => p.Id == req.Id, cancellationToken: ct); + // Récupérer le produit + var productToDelete = await db.Products + .SingleOrDefaultAsync(p => p.Id == req.Id, ct); - if (productToDelete == null) + if (productToDelete is null) { Console.WriteLine($"Aucun produit avec l'ID {req.Id} trouvé."); await Send.NotFoundAsync(ct); return; } - pyrofetesdbcontext.Products.Remove(productToDelete); - await pyrofetesdbcontext.SaveChangesAsync(ct); + // Supprimer les liaisons Price + 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); } diff --git a/PyroFetes/Endpoints/Product/GetAllProductsEndpoint.cs b/PyroFetes/Endpoints/Product/GetAllProductsEndpoint.cs index ee81ef9..ee5bb08 100644 --- a/PyroFetes/Endpoints/Product/GetAllProductsEndpoint.cs +++ b/PyroFetes/Endpoints/Product/GetAllProductsEndpoint.cs @@ -1,10 +1,15 @@ -using FastEndpoints; -using Microsoft.EntityFrameworkCore; +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; -public class GetAllProductsEndpoint(PyroFetesDbContext pyrofetesdbcontext) : EndpointWithoutRequest> +public class GetAllProductsEndpoint(PyroFetesDbContext db) + : EndpointWithoutRequest> { public override void Configure() { @@ -14,27 +19,45 @@ public class GetAllProductsEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End public override async Task HandleAsync(CancellationToken ct) { - List responseDto = await pyrofetesdbcontext.Products - .Select(p => new GetProductDto() - { - Id = p.Id, - Reference = p.References, - Name = p.Name, - Duration = p.Duration, - Caliber = p.Caliber, - ApprovalNumber = p.ApprovalNumber, - Weight = p.Weight, - Nec = p.Nec, - SellingPrice = p.SellingPrice, - Image = p.Image, - Link = p.Link, - ClassificationId = p.ClassificationId, - ClassificationLabel = p.Classification!.Label, - ProductCategoryId = p.ProductCategoryId, - ProductCategoryLabel = p.ProductCategory!.Label, - } - ).ToListAsync(ct); + // Inclure toutes les relations nécessaires : Prices + WarehouseProducts + Warehouse + var products = await db.Products + .Include(p => p.Prices) + .Include(p => p.WarehouseProducts) + .ThenInclude(wp => wp.Warehouse) + .ToListAsync(ct); + + var responseDto = products.Select(p => new GetProductDto + { + Id = p.Id, + Reference = p.References, + Name = p.Name, + Duration = p.Duration, + Caliber = p.Caliber, + ApprovalNumber = p.ApprovalNumber, + Weight = p.Weight, + Nec = p.Nec, + SellingPrice = p.SellingPrice, + Image = p.Image, + 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); } -} +} \ No newline at end of file diff --git a/PyroFetes/Endpoints/Product/GetProductEndpoint.cs b/PyroFetes/Endpoints/Product/GetProductEndpoint.cs index df4b64f..695ac8e 100644 --- a/PyroFetes/Endpoints/Product/GetProductEndpoint.cs +++ b/PyroFetes/Endpoints/Product/GetProductEndpoint.cs @@ -1,6 +1,10 @@ -using FastEndpoints; -using Microsoft.EntityFrameworkCore; +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; @@ -9,20 +13,23 @@ public class GetProductRequest public int Id { get; set; } } - -public class GetProductEndpoint(PyroFetesDbContext pyrofetesdbcontext) :Endpoint +public class GetProductEndpoint(PyroFetesDbContext db) + : Endpoint { public override void Configure() { - Get("/api/product/{@id}", x => new { x.Id }); + Get("/api/products/{@id}", x => new { x.Id }); AllowAnonymous(); } public override async Task HandleAsync(GetProductRequest req, CancellationToken ct) { - Models.Product? product = await pyrofetesdbcontext - .Products.Include(product => product.Classification).Include(product => product.ProductCategory) - .SingleOrDefaultAsync(p => p.Id == req.Id, cancellationToken: ct); + // Inclure toutes les relations : Prices + WarehouseProducts + Warehouse + var product = await db.Products + .Include(p => p.Prices) + .Include(p => p.WarehouseProducts) + .ThenInclude(wp => wp.Warehouse) + .SingleOrDefaultAsync(p => p.Id == req.Id, ct); if (product == null) { @@ -31,7 +38,7 @@ public class GetProductEndpoint(PyroFetesDbContext pyrofetesdbcontext) :Endpoint return; } - GetProductDto responseDto = new() + var responseDto = new GetProductDto { Id = product.Id, Reference = product.References, @@ -45,11 +52,24 @@ public class GetProductEndpoint(PyroFetesDbContext pyrofetesdbcontext) :Endpoint Image = product.Image, Link = product.Link, ClassificationId = product.ClassificationId, - ClassificationLabel = product.Classification!.Label, - ProductCategoryId = product.ProductCategoryId, - ProductCategoryLabel = product.ProductCategory!.Label, + ProductCategoryId = product.ProductCategoryId, + + // 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); } -} +} \ No newline at end of file diff --git a/PyroFetes/Endpoints/Product/UpdateProductEndpoint.cs b/PyroFetes/Endpoints/Product/UpdateProductEndpoint.cs index 2512e9c..2640ec9 100644 --- a/PyroFetes/Endpoints/Product/UpdateProductEndpoint.cs +++ b/PyroFetes/Endpoints/Product/UpdateProductEndpoint.cs @@ -1,47 +1,90 @@ -using FastEndpoints; +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; -public class UpdateProductEndpoint(PyroFetesDbContext pyrofetesdbcontext) :Endpoint +// Endpoint permettant de mettre à jour un produit existant +public class UpdateProductEndpoint(PyroFetesDbContext db) + : Endpoint { 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 +93,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); } -} \ No newline at end of file +} diff --git a/PyroFetes/Endpoints/Supplier/CreateSupplierEndpoint.cs b/PyroFetes/Endpoints/Supplier/CreateSupplierEndpoint.cs index 761eb4a..530e1af 100644 --- a/PyroFetes/Endpoints/Supplier/CreateSupplierEndpoint.cs +++ b/PyroFetes/Endpoints/Supplier/CreateSupplierEndpoint.cs @@ -1,10 +1,13 @@ -using API.DTO.Supplier.Request; -using API.DTO.Supplier.Response; +using PyroFetes.DTO.Supplier.Request; +using PyroFetes.DTO.Supplier.Response; using FastEndpoints; +using Microsoft.EntityFrameworkCore; +using PyroFetes.Models; namespace PyroFetes.Endpoints.Supplier; -public class CreateSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint +public class CreateSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) + : Endpoint { public override void Configure() { @@ -14,8 +17,7 @@ public class CreateSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End public override async Task HandleAsync(CreateSupplierDto req, CancellationToken ct) { - // Création d'un nouvel objet Supplier - Models.Supplier supplier = new() + var supplier = new Models.Supplier { Name = req.Name, Email = req.Email, @@ -24,15 +26,27 @@ public class CreateSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End ZipCode = req.ZipCode, City = req.City }; - - // Ajout à la base et sauvegarde + pyrofetesdbcontext.Suppliers.Add(supplier); await pyrofetesdbcontext.SaveChangesAsync(ct); - - Console.WriteLine("Fournisseur créé avec succès !"); - // Préparation de la réponse - GetSupplierDto responseDto = new() + // Ajout des liaisons Price si produits renseignés + 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, Name = supplier.Name, @@ -43,6 +57,6 @@ public class CreateSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End City = supplier.City }; - await Send.OkAsync(responseDto, ct); + await Send.OkAsync(response, ct); } } \ No newline at end of file diff --git a/PyroFetes/Endpoints/Supplier/DeleteSupplierEndpoint.cs b/PyroFetes/Endpoints/Supplier/DeleteSupplierEndpoint.cs index dbeb8db..e9735dc 100644 --- a/PyroFetes/Endpoints/Supplier/DeleteSupplierEndpoint.cs +++ b/PyroFetes/Endpoints/Supplier/DeleteSupplierEndpoint.cs @@ -1,5 +1,6 @@ using FastEndpoints; using Microsoft.EntityFrameworkCore; +using PyroFetes.Models; namespace PyroFetes.Endpoints.Supplier; @@ -18,19 +19,32 @@ public class DeleteSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End public override async Task HandleAsync(DeleteSupplierRequest req, CancellationToken ct) { - Models.Supplier? supplierToDelete = await pyrofetesdbcontext + var supplierToDelete = await pyrofetesdbcontext .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é."); await Send.NotFoundAsync(ct); 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); + await pyrofetesdbcontext.SaveChangesAsync(ct); + Console.WriteLine($"Fournisseur {req.Id} et ses prix liés supprimés avec succès."); await Send.NoContentAsync(ct); } diff --git a/PyroFetes/Endpoints/Supplier/GetAllSupplierEndpoint.cs b/PyroFetes/Endpoints/Supplier/GetAllSupplierEndpoint.cs index 51df593..28517a3 100644 --- a/PyroFetes/Endpoints/Supplier/GetAllSupplierEndpoint.cs +++ b/PyroFetes/Endpoints/Supplier/GetAllSupplierEndpoint.cs @@ -1,10 +1,13 @@ -using API.DTO.Supplier.Response; +using PyroFetes.DTO.Supplier.Response; +using PyroFetes.DTO.Supplier.Request; using FastEndpoints; using Microsoft.EntityFrameworkCore; +using PyroFetes.Models; namespace PyroFetes.Endpoints.Supplier; -public class GetAllSuppliersEndpoint(PyroFetesDbContext pyrofetesdbcontext) : EndpointWithoutRequest> +public class GetAllSuppliersEndpoint(PyroFetesDbContext pyrofetesdbcontext) + : EndpointWithoutRequest> { public override void Configure() { @@ -14,19 +17,28 @@ public class GetAllSuppliersEndpoint(PyroFetesDbContext pyrofetesdbcontext) : En public override async Task HandleAsync(CancellationToken ct) { - List responseDto = await pyrofetesdbcontext.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! - }) + var suppliers = await pyrofetesdbcontext.Suppliers + .Include(s => s.Prices) .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); } } \ No newline at end of file diff --git a/PyroFetes/Endpoints/Supplier/GetSupplierEndpoint.cs b/PyroFetes/Endpoints/Supplier/GetSupplierEndpoint.cs index 878e8ba..a66bfad 100644 --- a/PyroFetes/Endpoints/Supplier/GetSupplierEndpoint.cs +++ b/PyroFetes/Endpoints/Supplier/GetSupplierEndpoint.cs @@ -1,4 +1,5 @@ -using API.DTO.Supplier.Response; +using PyroFetes.DTO.Supplier.Request; +using PyroFetes.DTO.Supplier.Response; using FastEndpoints; using Microsoft.EntityFrameworkCore; @@ -9,7 +10,8 @@ public class GetSupplierRequest public int Id { get; set; } } -public class GetSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint +public class GetSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) + : Endpoint { public override void Configure() { @@ -19,9 +21,9 @@ public class GetSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoi public override async Task HandleAsync(GetSupplierRequest req, CancellationToken ct) { - Models.Supplier? supplier = await pyrofetesdbcontext - .Suppliers - .SingleOrDefaultAsync(s => s.Id == req.Id, cancellationToken: ct); + var supplier = await pyrofetesdbcontext.Suppliers + .Include(s => s.Prices) + .SingleOrDefaultAsync(s => s.Id == req.Id, ct); if (supplier == null) { @@ -30,7 +32,7 @@ public class GetSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoi return; } - GetSupplierDto responseDto = new() + var responseDto = new GetSupplierDto { Id = supplier.Id, Name = supplier.Name!, @@ -38,7 +40,14 @@ public class GetSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoi PhoneNumber = supplier.Phone!, Adress = supplier.Address!, 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); diff --git a/PyroFetes/Endpoints/Supplier/UpdateSupplierEndpoint.cs b/PyroFetes/Endpoints/Supplier/UpdateSupplierEndpoint.cs index e835fce..fde3c2e 100644 --- a/PyroFetes/Endpoints/Supplier/UpdateSupplierEndpoint.cs +++ b/PyroFetes/Endpoints/Supplier/UpdateSupplierEndpoint.cs @@ -1,7 +1,8 @@ -using API.DTO.Supplier.Request; -using API.DTO.Supplier.Response; +using PyroFetes.DTO.Supplier.Request; +using PyroFetes.DTO.Supplier.Response; using FastEndpoints; using Microsoft.EntityFrameworkCore; +using PyroFetes.Models; namespace PyroFetes.Endpoints.Supplier; @@ -15,18 +16,16 @@ public class UpdateSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End public override async Task HandleAsync(UpdateSupplierDto req, CancellationToken ct) { - Models.Supplier? supplierToEdit = await pyrofetesdbcontext + var supplierToEdit = await pyrofetesdbcontext .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); return; } - - // Mise à jour des propriétés supplierToEdit.Name = req.Name; supplierToEdit.Email = req.Email; supplierToEdit.Phone = req.PhoneNumber; @@ -34,9 +33,28 @@ public class UpdateSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End supplierToEdit.ZipCode = req.ZipCode; 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); - GetSupplierDto responseDto = new() + var response = new GetSupplierDto { Id = supplierToEdit.Id, Name = supplierToEdit.Name, @@ -47,6 +65,6 @@ public class UpdateSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End City = supplierToEdit.City }; - await Send.OkAsync(responseDto, ct); + await Send.OkAsync(response, ct); } -} \ No newline at end of file +} diff --git a/PyroFetes/Endpoints/Warehouse/CreateWarehouseEndpoint.cs b/PyroFetes/Endpoints/Warehouse/CreateWarehouseEndpoint.cs index 2c46f85..e6ff638 100644 --- a/PyroFetes/Endpoints/Warehouse/CreateWarehouseEndpoint.cs +++ b/PyroFetes/Endpoints/Warehouse/CreateWarehouseEndpoint.cs @@ -1,10 +1,12 @@ using API.DTO.Warehouse.Request; using API.DTO.Warehouse.Response; using FastEndpoints; +using PyroFetes.Models; namespace PyroFetes.Endpoints.Warehouse; -public class CreateWarehouseEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint +public class CreateWarehouseEndpoint(PyroFetesDbContext db) + : Endpoint { public override void Configure() { @@ -14,7 +16,7 @@ public class CreateWarehouseEndpoint(PyroFetesDbContext pyrofetesdbcontext) : En public override async Task HandleAsync(CreateWarehouseDto req, CancellationToken ct) { - Models.Warehouse warehouse = new() + var warehouse = new Models.Warehouse { Name = req.Name, MaxWeight = req.MaxWeight, @@ -24,13 +26,27 @@ public class CreateWarehouseEndpoint(PyroFetesDbContext pyrofetesdbcontext) : En ZipCode = req.ZipCode, 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, Name = warehouse.Name, @@ -42,6 +58,6 @@ public class CreateWarehouseEndpoint(PyroFetesDbContext pyrofetesdbcontext) : En City = warehouse.City }; - await Send.OkAsync(responseDto, ct); + await Send.OkAsync(response, ct); } } \ No newline at end of file diff --git a/PyroFetes/Endpoints/Warehouse/DeleteWarehouseEndpoint.cs b/PyroFetes/Endpoints/Warehouse/DeleteWarehouseEndpoint.cs index 758096b..2e533bc 100644 --- a/PyroFetes/Endpoints/Warehouse/DeleteWarehouseEndpoint.cs +++ b/PyroFetes/Endpoints/Warehouse/DeleteWarehouseEndpoint.cs @@ -1,5 +1,6 @@ using FastEndpoints; using Microsoft.EntityFrameworkCore; +using PyroFetes.Models; namespace PyroFetes.Endpoints.Warehouse; @@ -8,18 +9,20 @@ public class DeleteWarehouseRequest public int Id { get; set; } } -public class DeleteWarehouseEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint +public class DeleteWarehouseEndpoint(PyroFetesDbContext db) : Endpoint { public override void Configure() { + // L’annotation correcte du paramètre est {id}, pas {@id} Delete("/api/warehouse/{@id}", x => new { x.Id }); AllowAnonymous(); } - + public override async Task HandleAsync(DeleteWarehouseRequest req, CancellationToken ct) { - Models.Warehouse? warehouseToDelete = await pyrofetesdbcontext - .Warehouses + // On charge aussi les WarehouseProducts liés pour les supprimer proprement + var warehouseToDelete = await db.Warehouses + .Include(w => w.WarehouseProducts) .SingleOrDefaultAsync(w => w.Id == req.Id, cancellationToken: ct); if (warehouseToDelete == null) @@ -29,9 +32,21 @@ public class DeleteWarehouseEndpoint(PyroFetesDbContext pyrofetesdbcontext) : En return; } - pyrofetesdbcontext.Warehouses.Remove(warehouseToDelete); - await pyrofetesdbcontext.SaveChangesAsync(ct); + // 🔹 Suppression des relations WarehouseProduct avant l'entrepôt + var relatedWarehouseProducts = await db.WarehouseProducts + .Where(wp => wp.WarehouseId == req.Id) + .ToListAsync(ct); + if (relatedWarehouseProducts.Any()) + { + db.WarehouseProducts.RemoveRange(relatedWarehouseProducts); + } + + // 🔹 Suppression de l’entrepô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); } } \ No newline at end of file diff --git a/PyroFetes/Endpoints/Warehouse/GetAllWarehouseEndpoint.cs b/PyroFetes/Endpoints/Warehouse/GetAllWarehouseEndpoint.cs index 0d98e6f..2c7cdb8 100644 --- a/PyroFetes/Endpoints/Warehouse/GetAllWarehouseEndpoint.cs +++ b/PyroFetes/Endpoints/Warehouse/GetAllWarehouseEndpoint.cs @@ -1,10 +1,13 @@ using API.DTO.Warehouse.Response; +using API.DTO.Warehouse.Request; using FastEndpoints; using Microsoft.EntityFrameworkCore; +using PyroFetes.Models; namespace PyroFetes.Endpoints.Warehouse; -public class GetAllWarehouseEndpoint(PyroFetesDbContext pyrofetesdbcontext) : EndpointWithoutRequest> +public class GetAllWarehouseEndpoint(PyroFetesDbContext db) + : EndpointWithoutRequest> { public override void Configure() { @@ -14,20 +17,30 @@ public class GetAllWarehouseEndpoint(PyroFetesDbContext pyrofetesdbcontext) : En public override async Task HandleAsync(CancellationToken ct) { - List responseDto = await pyrofetesdbcontext.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 - }) + // 🔹 On inclut les relations avec WarehouseProducts et Product + var warehouses = await db.Warehouses + .Include(w => w.WarehouseProducts) + .ThenInclude(wp => wp.Product) .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); } } \ No newline at end of file diff --git a/PyroFetes/Endpoints/Warehouse/GetWarehouseEndpoint.cs b/PyroFetes/Endpoints/Warehouse/GetWarehouseEndpoint.cs index b24e007..386048d 100644 --- a/PyroFetes/Endpoints/Warehouse/GetWarehouseEndpoint.cs +++ b/PyroFetes/Endpoints/Warehouse/GetWarehouseEndpoint.cs @@ -1,6 +1,7 @@ using API.DTO.Warehouse.Response; using FastEndpoints; using Microsoft.EntityFrameworkCore; +using PyroFetes.Models; namespace PyroFetes.Endpoints.Warehouse; @@ -9,28 +10,32 @@ public class GetWarehouseRequest public int Id { get; set; } } -public class GetWarehouseEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint +public class GetWarehouseEndpoint(PyroFetesDbContext db) + : Endpoint { public override void Configure() { + // Pas de "@id" ici, juste {id} Get("/api/warehouses/{@id}", x => new { x.Id }); AllowAnonymous(); } public override async Task HandleAsync(GetWarehouseRequest req, CancellationToken ct) { - Models.Warehouse? warehouse = await pyrofetesdbcontext - .Warehouses + // 🔹 Inclut les produits associés à cet entrepôt + var warehouse = await db.Warehouses + .Include(w => w.WarehouseProducts) + .ThenInclude(wp => wp.Product) .SingleOrDefaultAsync(w => w.Id == req.Id, cancellationToken: ct); 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); return; } - GetWarehouseDto responseDto = new() + var response = new GetWarehouseDto { Id = warehouse.Id, Name = warehouse.Name!, @@ -39,9 +44,15 @@ public class GetWarehouseEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpo MinWeight = warehouse.MinWeight, Adress = warehouse.Address!, 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); } } \ No newline at end of file diff --git a/PyroFetes/Endpoints/Warehouse/UpdateWarehouseEndpoint.cs b/PyroFetes/Endpoints/Warehouse/UpdateWarehouseEndpoint.cs index 0864bd6..0a31c70 100644 --- a/PyroFetes/Endpoints/Warehouse/UpdateWarehouseEndpoint.cs +++ b/PyroFetes/Endpoints/Warehouse/UpdateWarehouseEndpoint.cs @@ -2,21 +2,25 @@ using API.DTO.Warehouse.Response; using FastEndpoints; using Microsoft.EntityFrameworkCore; +using PyroFetes.Models; namespace PyroFetes.Endpoints.Warehouse; -public class UpdateWarehouseEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint +public class UpdateWarehouseEndpoint(PyroFetesDbContext db) + : Endpoint { public override void Configure() { + // Utilise {id} plutôt que {@id} Put("/api/warehouses/{@id}", x => new { x.Id }); AllowAnonymous(); } public override async Task HandleAsync(UpdateWarehouseDto req, CancellationToken ct) { - Models.Warehouse? warehouseToEdit = await pyrofetesdbcontext - .Warehouses + // 🔹 On inclut les produits existants pour pouvoir les modifier + var warehouseToEdit = await db.Warehouses + .Include(w => w.WarehouseProducts) .SingleOrDefaultAsync(w => w.Id == req.Id, cancellationToken: ct); if (warehouseToEdit == null) @@ -26,7 +30,7 @@ public class UpdateWarehouseEndpoint(PyroFetesDbContext pyrofetesdbcontext) : En return; } - // Mise à jour des champs + // 🔹 Mise à jour des champs de base warehouseToEdit.Name = req.Name; warehouseToEdit.MaxWeight = req.MaxWeight; warehouseToEdit.Current = req.Current; @@ -35,9 +39,30 @@ public class UpdateWarehouseEndpoint(PyroFetesDbContext pyrofetesdbcontext) : En warehouseToEdit.ZipCode = req.ZipCode; 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, Name = warehouseToEdit.Name, @@ -46,9 +71,16 @@ public class UpdateWarehouseEndpoint(PyroFetesDbContext pyrofetesdbcontext) : En MinWeight = warehouseToEdit.MinWeight, Adress = warehouseToEdit.Address, 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); } -} \ No newline at end of file +} diff --git a/PyroFetes/Models/Availability.cs b/PyroFetes/Models/Availability.cs index fdb5f68..47b0f1c 100644 --- a/PyroFetes/Models/Availability.cs +++ b/PyroFetes/Models/Availability.cs @@ -4,13 +4,11 @@ namespace PyroFetes.Models; public class Availability { - //Champs [Key] public int Id { get; set; } [Required] public DateOnly AvailabilityDate { get; set; } [Required] public DateOnly DeliveryDate { get; set; } [Required] public DateOnly ExpirationDate { get; set; } [Required] public DateOnly RenewallDate { get; set; } - //Relations public List? StaffAvailabilities { get; set; } } \ No newline at end of file diff --git a/PyroFetes/Models/Brand.cs b/PyroFetes/Models/Brand.cs index f1b486f..49016ca 100644 --- a/PyroFetes/Models/Brand.cs +++ b/PyroFetes/Models/Brand.cs @@ -4,11 +4,9 @@ namespace PyroFetes.Models; public class Brand { - //Champs [Key] public int Id { get; set; } [Required, MaxLength(100)] public string? Name { get; set; } - [Required] public int ProductId { get; set; } - //Relations + [Required] public int ProductId { get; set; } [Required] public Product? Product { get; set; } } \ No newline at end of file diff --git a/PyroFetes/Models/City.cs b/PyroFetes/Models/City.cs index 60cd49a..04dc24e 100644 --- a/PyroFetes/Models/City.cs +++ b/PyroFetes/Models/City.cs @@ -4,11 +4,9 @@ namespace PyroFetes.Models; public class City { - //Champs [Key] public int Id { get; set; } [Required, MaxLength(100)] public string? Name { get; set; } [Required] public int ZipCode { get; set; } - //Relations public List? Shows { get; set; } } \ No newline at end of file diff --git a/PyroFetes/Models/Classification.cs b/PyroFetes/Models/Classification.cs index 1ac77db..bd3685b 100644 --- a/PyroFetes/Models/Classification.cs +++ b/PyroFetes/Models/Classification.cs @@ -4,10 +4,8 @@ namespace PyroFetes.Models; public class Classification { - //Champs [Key] public int Id { get; set; } [Required, MaxLength(100)] public string? Label { get; set; } - //Relations public List? Products { get; set; } } \ No newline at end of file diff --git a/PyroFetes/Models/Color.cs b/PyroFetes/Models/Color.cs index 95cce3d..acfe830 100644 --- a/PyroFetes/Models/Color.cs +++ b/PyroFetes/Models/Color.cs @@ -4,10 +4,8 @@ namespace PyroFetes.Models; public class Color { - //Champs [Key] public int Id { get; set; } [Required, MaxLength(100)] public string? Label { get; set; } - //Relations public List? ProductColors { get; set; } } \ No newline at end of file diff --git a/PyroFetes/Models/Communication.cs b/PyroFetes/Models/Communication.cs index 1265822..a3dd0d2 100644 --- a/PyroFetes/Models/Communication.cs +++ b/PyroFetes/Models/Communication.cs @@ -4,13 +4,11 @@ namespace PyroFetes.Models; public class Communication { - //Champs [Key] public int Id { get; set; } [Required, MaxLength(100)] public string? Calling { get; set; } [Required, MaxLength(100)] public string? Email { get; set; } [Required, MaxLength(300)] public string? Meeting { get; set; } - //Relations [Required] public int ContactId { get; set; } public Contact? Contact { get; set; } } \ No newline at end of file diff --git a/PyroFetes/Models/Contact.cs b/PyroFetes/Models/Contact.cs index 5473328..e1573ad 100644 --- a/PyroFetes/Models/Contact.cs +++ b/PyroFetes/Models/Contact.cs @@ -4,7 +4,6 @@ namespace PyroFetes.Models; public class Contact { - //Champs [Key] public int Id { get; set; } [Required, MaxLength(100)] public string? LastName { get; set; } [Required, MaxLength(100)] public string? FirstName { get; set; } @@ -15,8 +14,6 @@ public class Contact [Required, MaxLength(100)] public string? City { get; set; } [Required, MaxLength(100)] public string? Role { get; set; } - - //Relations public Customer? Customer { get; set; } [Required] public int CustomerId { get; set; } diff --git a/PyroFetes/Models/Material.cs b/PyroFetes/Models/Material.cs index 61f74c9..224e5d5 100644 --- a/PyroFetes/Models/Material.cs +++ b/PyroFetes/Models/Material.cs @@ -5,7 +5,7 @@ namespace PyroFetes.Models; public class Material { [Key] public int Id {get; set;} - [Required, MaxLength(100)] public string? Label {get; set;} + [Required, MaxLength(100)] public string? Name {get; set;} [Required] public int Quantity {get; set;} [Required] public int WarehouseId {get; set;} diff --git a/PyroFetes/PyroFetes.csproj b/PyroFetes/PyroFetes.csproj index c662ac6..b301a92 100644 --- a/PyroFetes/PyroFetes.csproj +++ b/PyroFetes/PyroFetes.csproj @@ -9,7 +9,7 @@ - + all