Adapted Price Endpoints

This commit is contained in:
Cristiano
2025-11-13 16:31:00 +01:00
parent c6d4ef2c58
commit ae834d1e3c
12 changed files with 139 additions and 106 deletions

View File

@@ -7,7 +7,7 @@ using PyroFetes.Repositories;
namespace PyroFetes.Endpoints.Deliverers;
public class CreateDelivererEndpoint(
DeliverersRepository deliverersRepository,
DeliverersRepository deliverersRepository,
AutoMapper.IMapper mapper) : Endpoint<CreateDelivererDto, GetDelivererDto>
{
public override void Configure()

View File

@@ -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<CreatePriceDto, GetPriceDto>
{
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);
}
}

View File

@@ -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<CreatePriceDto, GetPriceDto>
{
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<GetPriceDto>(priceAdded), ct);
}
}

View File

@@ -2,4 +2,4 @@ using PyroFetes.Models;
namespace PyroFetes.Repositories;
public class DeliverersRepository(PyroFetesDbContext lmdContext, AutoMapper.IMapper mapper) : PyrofetesRepository<Deliverer>(lmdContext, mapper);
public class DeliverersRepository(PyroFetesDbContext pyrofetesContext, AutoMapper.IMapper mapper) : PyrofetesRepository<Deliverer>(pyrofetesContext, mapper);

View File

@@ -0,0 +1,5 @@
using PyroFetes.Models;
namespace PyroFetes.Repositories;
public class PricesRepository(PyroFetesDbContext pyrofetesContext, AutoMapper.IMapper mapper) : PyrofetesRepository<Price>(pyrofetesContext, mapper);

View File

@@ -0,0 +1,5 @@
using PyroFetes.Models;
namespace PyroFetes.Repositories;
public class ProductsRepository(PyroFetesDbContext pyrofetesContext, AutoMapper.IMapper mapper) : PyrofetesRepository<Product>(pyrofetesContext, mapper);

View File

@@ -0,0 +1,5 @@
using PyroFetes.Models;
namespace PyroFetes.Repositories;
public class SuppliersRepository(PyroFetesDbContext pyrofetesContext, AutoMapper.IMapper mapper) : PyrofetesRepository<Supplier>(pyrofetesContext, mapper);

View File

@@ -0,0 +1,13 @@
using Ardalis.Specification;
using PyroFetes.Models;
namespace PyroFetes.Specifications.Prices;
public sealed class GetPriceByProductIdAndSupplierIdSpec : Specification<Price>
{
public GetPriceByProductIdAndSupplierIdSpec(int? productId, int? supplierId)
{
Query
.Where(p => p.ProductId == productId && p.SupplierId == supplierId);
}
}

View File

@@ -0,0 +1,13 @@
using Ardalis.Specification;
using PyroFetes.Models;
namespace PyroFetes.Specifications.Products;
public sealed class GetProductByIdSpec : Specification<Product>
{
public GetProductByIdSpec(int? productId)
{
Query
.Where(p => p.Id == productId);
}
}

View File

@@ -0,0 +1,13 @@
using Ardalis.Specification;
using PyroFetes.Models;
namespace PyroFetes.Specifications.Suppliers;
public sealed class GetSupplierByIdSpec : Specification<Supplier>
{
public GetSupplierByIdSpec(int? supplierId)
{
Query
.Where(x => x.Id == supplierId);
}
}