diff --git a/PyroFetes/Endpoints/Deliverers/CreateDelivererEndpoint.cs b/PyroFetes/Endpoints/Deliverers/CreateDelivererEndpoint.cs index d7f67ff..8661c64 100644 --- a/PyroFetes/Endpoints/Deliverers/CreateDelivererEndpoint.cs +++ b/PyroFetes/Endpoints/Deliverers/CreateDelivererEndpoint.cs @@ -7,7 +7,7 @@ using PyroFetes.Repositories; namespace PyroFetes.Endpoints.Deliverers; public class CreateDelivererEndpoint( - DeliverersRepository deliverersRepository, + DeliverersRepository deliverersRepository, AutoMapper.IMapper mapper) : Endpoint { public override void Configure() diff --git a/PyroFetes/Endpoints/Price/CreatePriceEndpoint.cs b/PyroFetes/Endpoints/Price/CreatePriceEndpoint.cs deleted file mode 100644 index d65ba2d..0000000 --- a/PyroFetes/Endpoints/Price/CreatePriceEndpoint.cs +++ /dev/null @@ -1,104 +0,0 @@ -using FastEndpoints; -using Microsoft.EntityFrameworkCore; -using PyroFetes.DTO.Price.Request; -using PyroFetes.DTO.Price.Response; - -namespace PyroFetes.Endpoints.Price; - -public class CreatePriceEndpoint(PyroFetesDbContext database) : Endpoint -{ - public override void Configure() - { - Post("/api/prices"); - AllowAnonymous(); - } - - public override async Task HandleAsync(CreatePriceDto req, CancellationToken ct) - { - // Gestion du fournisseur - var supplier = await database.Suppliers.FirstOrDefaultAsync(s => s.Id == req.SupplierId, ct); - if (supplier == null) - { - supplier = new Models.Supplier() - { - Name = req.SupplierName, - Email = req.SupplierEmail, - Phone = req.SupplierPhone, - Address = req.SupplierAddress, - City = req.SupplierCity, - ZipCode = req.SupplierZipCode, - DeliveryDelay = req.SupplierDeliveryDelay - }; - database.Suppliers.Add(supplier); - await database.SaveChangesAsync(ct); - } - - // Gestion du produit - var product = await database.Products.SingleOrDefaultAsync(p => p.Id == req.ProductId, ct); - if (product == null) - { - product = new Models.Product() - { - Reference = req.ProductReferences, - Name = req.ProductName, - Duration = req.ProductDuration, - Caliber = req.ProductCaliber, - ApprovalNumber = req.ProductApprovalNumber, - Weight = req.ProductWeight, - Nec = req.ProductNec, - Image = req.ProductImage, - Link = req.ProductLink, - MinimalQuantity = req.ProductMinimalQuantity - }; - database.Products.Add(product); - await database.SaveChangesAsync(ct); - } - - // Vérifie si le prix existe déjà pour ce fournisseur et produit - var existingPrice = await database.Prices - .SingleOrDefaultAsync(p => p.ProductId == product.Id && p.SupplierId == supplier.Id, ct); - - if (existingPrice != null) - { - await Send.ConflictAsync("Le fournisseur a déjà un prix pour ce produit.", ct); - return; - } - - // Création du prix - var priceAdded = new Models.Price() - { - SellingPrice = req.SellingPrice, - SupplierId = supplier.Id, - ProductId = product.Id - }; - database.Prices.Add(priceAdded); - await database.SaveChangesAsync(ct); - - // Création du DTO de réponse - var responseDto = new GetPriceDto() - { - SellingPrice = priceAdded.SellingPrice, - SupplierId = supplier.Id, - ProductId = product.Id, - SupplierName = supplier.Name, - SupplierEmail = supplier.Email, - SupplierPhone = supplier.Phone, - SupplierAddress = supplier.Address, - SupplierCity = supplier.City, - SupplierZipCode = supplier.ZipCode, - SupplierDeliveryDelay = supplier.DeliveryDelay, - ProductReferences = product.Reference, - ProductName = product.Name, - ProductDuration = product.Duration, - ProductCaliber = product.Caliber, - ProductApprovalNumber = product.ApprovalNumber, - ProductWeight = product.Weight, - ProductNec = product.Nec, - ProductImage = product.Image, - ProductLink = product.Link, - ProductMinimalQuantity = product.MinimalQuantity - }; - - await Send.OkAsync(responseDto, ct); - } -} diff --git a/PyroFetes/Endpoints/Prices/CreatePriceEndpoint.cs b/PyroFetes/Endpoints/Prices/CreatePriceEndpoint.cs new file mode 100644 index 0000000..fcdbde3 --- /dev/null +++ b/PyroFetes/Endpoints/Prices/CreatePriceEndpoint.cs @@ -0,0 +1,83 @@ +using FastEndpoints; +using PyroFetes.DTO.Price.Request; +using PyroFetes.DTO.Price.Response; +using PyroFetes.Repositories; +using PyroFetes.Specifications.Prices; +using PyroFetes.Specifications.Products; +using PyroFetes.Specifications.Suppliers; + +namespace PyroFetes.Endpoints.Prices; + +public class CreatePriceEndpoint( + SuppliersRepository suppliersRepository, + ProductsRepository productsRepository, + PricesRepository pricesRepository, + AutoMapper.IMapper mapper) : Endpoint +{ + public override void Configure() + { + Post("/api/prices"); + AllowAnonymous(); + } + + public override async Task HandleAsync(CreatePriceDto req, CancellationToken ct) + { + // Gestion du fournisseur + var supplier = await suppliersRepository.FirstOrDefaultAsync(new GetSupplierByIdSpec(req.SupplierId), ct); + if (supplier == null) + { + supplier = new Models.Supplier() + { + Name = req.SupplierName, + Email = req.SupplierEmail, + Phone = req.SupplierPhone, + Address = req.SupplierAddress, + City = req.SupplierCity, + ZipCode = req.SupplierZipCode, + DeliveryDelay = req.SupplierDeliveryDelay + }; + await suppliersRepository.AddAsync(supplier, ct); + } + + // Gestion du produit + var product = await productsRepository.FirstOrDefaultAsync(new GetProductByIdSpec(req.ProductId), ct); + if (product == null) + { + product = new Models.Product() + { + Reference = req.ProductReferences, + Name = req.ProductName, + Duration = req.ProductDuration, + Caliber = req.ProductCaliber, + ApprovalNumber = req.ProductApprovalNumber, + Weight = req.ProductWeight, + Nec = req.ProductNec, + Image = req.ProductImage, + Link = req.ProductLink, + MinimalQuantity = req.ProductMinimalQuantity + }; + await productsRepository.AddAsync(product, ct); + } + + // Vérifie si le prix existe déjà pour ce fournisseur et produit + var existingPrice = await pricesRepository.FirstOrDefaultAsync(new GetPriceByProductIdAndSupplierIdSpec(req.ProductId, req.SupplierId), ct); + + if (existingPrice != null) + { + await Send.StringAsync("Le fournisseur a déjà un prix pour ce produit.", 400, cancellation: ct); + return; + } + + // Création du prix + var priceAdded = new Models.Price() + { + SellingPrice = req.SellingPrice, + SupplierId = supplier.Id, + ProductId = product.Id + }; + await pricesRepository.AddAsync(priceAdded, ct); + + + await Send.OkAsync(mapper.Map(priceAdded), ct); + } +} diff --git a/PyroFetes/Endpoints/Price/DeletePriceEndpoint.cs b/PyroFetes/Endpoints/Prices/DeletePriceEndpoint.cs similarity index 100% rename from PyroFetes/Endpoints/Price/DeletePriceEndpoint.cs rename to PyroFetes/Endpoints/Prices/DeletePriceEndpoint.cs diff --git a/PyroFetes/Endpoints/Price/PatchPriceEndpoint.cs b/PyroFetes/Endpoints/Prices/PatchPriceEndpoint.cs similarity index 100% rename from PyroFetes/Endpoints/Price/PatchPriceEndpoint.cs rename to PyroFetes/Endpoints/Prices/PatchPriceEndpoint.cs diff --git a/PyroFetes/Repositories/DeliverersRepository.cs b/PyroFetes/Repositories/DeliverersRepository.cs index 5f2af84..0bf8c2e 100644 --- a/PyroFetes/Repositories/DeliverersRepository.cs +++ b/PyroFetes/Repositories/DeliverersRepository.cs @@ -2,4 +2,4 @@ using PyroFetes.Models; namespace PyroFetes.Repositories; -public class DeliverersRepository(PyroFetesDbContext lmdContext, AutoMapper.IMapper mapper) : PyrofetesRepository(lmdContext, mapper); +public class DeliverersRepository(PyroFetesDbContext pyrofetesContext, AutoMapper.IMapper mapper) : PyrofetesRepository(pyrofetesContext, mapper); diff --git a/PyroFetes/Repositories/PricesRepository.cs b/PyroFetes/Repositories/PricesRepository.cs new file mode 100644 index 0000000..ec33111 --- /dev/null +++ b/PyroFetes/Repositories/PricesRepository.cs @@ -0,0 +1,5 @@ +using PyroFetes.Models; + +namespace PyroFetes.Repositories; + +public class PricesRepository(PyroFetesDbContext pyrofetesContext, AutoMapper.IMapper mapper) : PyrofetesRepository(pyrofetesContext, mapper); \ No newline at end of file diff --git a/PyroFetes/Repositories/ProductsRepository.cs b/PyroFetes/Repositories/ProductsRepository.cs new file mode 100644 index 0000000..40719c6 --- /dev/null +++ b/PyroFetes/Repositories/ProductsRepository.cs @@ -0,0 +1,5 @@ +using PyroFetes.Models; + +namespace PyroFetes.Repositories; + +public class ProductsRepository(PyroFetesDbContext pyrofetesContext, AutoMapper.IMapper mapper) : PyrofetesRepository(pyrofetesContext, mapper); \ No newline at end of file diff --git a/PyroFetes/Repositories/SuppliersRepository.cs b/PyroFetes/Repositories/SuppliersRepository.cs new file mode 100644 index 0000000..1f8a8c0 --- /dev/null +++ b/PyroFetes/Repositories/SuppliersRepository.cs @@ -0,0 +1,5 @@ +using PyroFetes.Models; + +namespace PyroFetes.Repositories; + +public class SuppliersRepository(PyroFetesDbContext pyrofetesContext, AutoMapper.IMapper mapper) : PyrofetesRepository(pyrofetesContext, mapper); \ No newline at end of file diff --git a/PyroFetes/Specifications/Prices/GetPriceByProductIdAndSupplierIdSpec.cs b/PyroFetes/Specifications/Prices/GetPriceByProductIdAndSupplierIdSpec.cs new file mode 100644 index 0000000..5f74c13 --- /dev/null +++ b/PyroFetes/Specifications/Prices/GetPriceByProductIdAndSupplierIdSpec.cs @@ -0,0 +1,13 @@ +using Ardalis.Specification; +using PyroFetes.Models; + +namespace PyroFetes.Specifications.Prices; + +public sealed class GetPriceByProductIdAndSupplierIdSpec : Specification +{ + public GetPriceByProductIdAndSupplierIdSpec(int? productId, int? supplierId) + { + Query + .Where(p => p.ProductId == productId && p.SupplierId == supplierId); + } +} \ No newline at end of file diff --git a/PyroFetes/Specifications/Products/GetProductByIdSpec.cs b/PyroFetes/Specifications/Products/GetProductByIdSpec.cs new file mode 100644 index 0000000..fb7322c --- /dev/null +++ b/PyroFetes/Specifications/Products/GetProductByIdSpec.cs @@ -0,0 +1,13 @@ +using Ardalis.Specification; +using PyroFetes.Models; + +namespace PyroFetes.Specifications.Products; + +public sealed class GetProductByIdSpec : Specification +{ + public GetProductByIdSpec(int? productId) + { + Query + .Where(p => p.Id == productId); + } +} \ No newline at end of file diff --git a/PyroFetes/Specifications/Suppliers/GetSupplierByIdSpec.cs b/PyroFetes/Specifications/Suppliers/GetSupplierByIdSpec.cs new file mode 100644 index 0000000..add1699 --- /dev/null +++ b/PyroFetes/Specifications/Suppliers/GetSupplierByIdSpec.cs @@ -0,0 +1,13 @@ +using Ardalis.Specification; +using PyroFetes.Models; + +namespace PyroFetes.Specifications.Suppliers; + +public sealed class GetSupplierByIdSpec : Specification +{ + public GetSupplierByIdSpec(int? supplierId) + { + Query + .Where(x => x.Id == supplierId); + } +} \ No newline at end of file