Actualiser PyroFetes/Endpoints/Price/CreatePriceEndpoint.cs

This commit is contained in:
2025-11-09 18:46:24 +01:00
parent 304c06ed19
commit 7d92f80de3

View File

@@ -15,6 +15,7 @@ public class CreatePriceEndpoint(PyroFetesDbContext database) : Endpoint<CreateP
public override async Task HandleAsync(CreatePriceDto req, CancellationToken ct) public override async Task HandleAsync(CreatePriceDto req, CancellationToken ct)
{ {
// Gestion du fournisseur
var supplier = await database.Suppliers.FirstOrDefaultAsync(s => s.Id == req.SupplierId, ct); var supplier = await database.Suppliers.FirstOrDefaultAsync(s => s.Id == req.SupplierId, ct);
if (supplier == null) if (supplier == null)
{ {
@@ -27,56 +28,58 @@ public class CreatePriceEndpoint(PyroFetesDbContext database) : Endpoint<CreateP
City = req.SupplierCity, City = req.SupplierCity,
ZipCode = req.SupplierZipCode, ZipCode = req.SupplierZipCode,
DeliveryDelay = req.SupplierDeliveryDelay DeliveryDelay = req.SupplierDeliveryDelay
}; };
database.Suppliers.Add(supplier); database.Suppliers.Add(supplier);
await database.SaveChangesAsync(ct); await database.SaveChangesAsync(ct);
} }
var product = await database.Products.SingleOrDefaultAsync(p => p.Reference == req.ProductReferences, ct); // Gestion du produit
if (product != null) var product = await database.Products.SingleOrDefaultAsync(p => p.Id == req.ProductId, ct);
if (product == null)
{ {
await Send.NotFoundAsync(ct); 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; return;
} }
var productAdded = new Models.Product() // Création du prix
{
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(productAdded);
await database.SaveChangesAsync(ct);
var price = await database.Prices.SingleOrDefaultAsync(p => p.ProductId == req.ProductId && p.SupplierId == req.SupplierId, ct);
if (price != null)
{
await Send.NotFoundAsync(ct);
return;
}
var priceAdded = new Models.Price() var priceAdded = new Models.Price()
{ {
SellingPrice = req.SellingPrice, SellingPrice = req.SellingPrice,
SupplierId = supplier.Id, SupplierId = supplier.Id,
ProductId = productAdded.Id, ProductId = product.Id
}; };
database.Prices.Add(priceAdded); database.Prices.Add(priceAdded);
await database.SaveChangesAsync(ct); await database.SaveChangesAsync(ct);
// Création du DTO de réponse
var responseDto = new GetPriceDto() var responseDto = new GetPriceDto()
{ {
SellingPrice = priceAdded.SellingPrice, SellingPrice = priceAdded.SellingPrice,
SupplierId = supplier.Id, SupplierId = supplier.Id,
ProductId = productAdded.Id, ProductId = product.Id,
SupplierName = supplier.Name, SupplierName = supplier.Name,
SupplierEmail = supplier.Email, SupplierEmail = supplier.Email,
SupplierPhone = supplier.Phone, SupplierPhone = supplier.Phone,
@@ -84,16 +87,16 @@ public class CreatePriceEndpoint(PyroFetesDbContext database) : Endpoint<CreateP
SupplierCity = supplier.City, SupplierCity = supplier.City,
SupplierZipCode = supplier.ZipCode, SupplierZipCode = supplier.ZipCode,
SupplierDeliveryDelay = supplier.DeliveryDelay, SupplierDeliveryDelay = supplier.DeliveryDelay,
ProductReferences = productAdded.Reference, ProductReferences = product.Reference,
ProductName = productAdded.Name, ProductName = product.Name,
ProductDuration = productAdded.Duration, ProductDuration = product.Duration,
ProductCaliber = productAdded.Caliber, ProductCaliber = product.Caliber,
ProductApprovalNumber = productAdded.ApprovalNumber, ProductApprovalNumber = product.ApprovalNumber,
ProductWeight = productAdded.Weight, ProductWeight = product.Weight,
ProductNec = productAdded.Nec, ProductNec = product.Nec,
ProductImage = productAdded.Image, ProductImage = product.Image,
ProductLink = productAdded.Link, ProductLink = product.Link,
ProductMinimalQuantity = productAdded.MinimalQuantity ProductMinimalQuantity = product.MinimalQuantity
}; };
await Send.OkAsync(responseDto, ct); await Send.OkAsync(responseDto, ct);