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

@@ -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

@@ -0,0 +1,36 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
namespace PyroFetes.Endpoints.QuotationProduct;
public class DeletePriceRequest
{
public int ProductId { get; set; }
public int SupplierId { get; set; }
}
public class DeletePriceEndpoint(PyroFetesDbContext database) : Endpoint<DeletePriceRequest>
{
public override void Configure()
{
Delete("/api/prices/{@ProductId}/{@SupplierId}", x => new {x.ProductId, x.SupplierId});
AllowAnonymous();
}
public override async Task HandleAsync(DeletePriceRequest req, CancellationToken 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;
}
database.Prices.Remove(price);
await database.SaveChangesAsync(ct);
await Send.NoContentAsync(ct);
}
}

View File

@@ -0,0 +1,36 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Price.Request;
using PyroFetes.DTO.Price.Response;
namespace PyroFetes.Endpoints.Price;
public class PatchPriceEndpoint(PyroFetesDbContext database) : Endpoint<PatchPriceSellingPriceDto, GetPriceDto>
{
public override void Configure()
{
Patch("/api/prices/{@ProductId}/{@SupplierId}/SellingPrice", x => new { x.ProductId, x.SupplierId });
AllowAnonymous();
}
public override async Task HandleAsync(PatchPriceSellingPriceDto req, CancellationToken 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;
}
price.SellingPrice = req.SellingPrice;
await database.SaveChangesAsync(ct);
GetPriceDto responseDto = new()
{
ProductId = price.ProductId,
SupplierId = price.SupplierId,
SellingPrice = price.SellingPrice
};
await Send.OkAsync(responseDto, ct);
}
}