Initial commit
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
using FastEndpoints;
|
||||
using PyroFetes.DTO.Price.Request;
|
||||
using PyroFetes.DTO.Price.Response;
|
||||
using PyroFetes.Models;
|
||||
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("/prices");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CreatePriceDto req, CancellationToken ct)
|
||||
{
|
||||
// Gestion du fournisseur
|
||||
Supplier? supplier = await suppliersRepository.FirstOrDefaultAsync(new GetSupplierByIdSpec(req.SupplierId), ct);
|
||||
if (supplier == null)
|
||||
{
|
||||
supplier = new 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
|
||||
Product? product = await productsRepository.FirstOrDefaultAsync(new GetProductByIdSpec(req.ProductId), ct);
|
||||
if (product == null)
|
||||
{
|
||||
product = new 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
|
||||
Price? 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 Price()
|
||||
{
|
||||
SellingPrice = req.SellingPrice,
|
||||
SupplierId = supplier.Id,
|
||||
ProductId = product.Id
|
||||
};
|
||||
await pricesRepository.AddAsync(priceAdded, ct);
|
||||
|
||||
|
||||
await Send.OkAsync(mapper.Map<GetPriceDto>(priceAdded), ct);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes.Models;
|
||||
using PyroFetes.Repositories;
|
||||
using PyroFetes.Specifications.Prices;
|
||||
|
||||
namespace PyroFetes.Endpoints.Prices;
|
||||
|
||||
public class DeletePriceRequest
|
||||
{
|
||||
public int ProductId { get; set; }
|
||||
public int SupplierId { get; set; }
|
||||
}
|
||||
|
||||
public class DeletePriceEndpoint(PricesRepository pricesRepository) : Endpoint<DeletePriceRequest>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Delete("/prices/{@ProductId}/{@SupplierId}", x => new {x.ProductId, x.SupplierId});
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(DeletePriceRequest req, CancellationToken ct)
|
||||
{
|
||||
Price? price = await pricesRepository.FirstOrDefaultAsync(new GetPriceByProductIdAndSupplierIdSpec(req.ProductId,req.SupplierId), ct);
|
||||
|
||||
if (price == null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
await pricesRepository.DeleteAsync(price, ct);
|
||||
|
||||
await Send.NoContentAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using FastEndpoints;
|
||||
using PyroFetes.DTO.Price.Request;
|
||||
using PyroFetes.DTO.Price.Response;
|
||||
using PyroFetes.Models;
|
||||
using PyroFetes.Repositories;
|
||||
using PyroFetes.Specifications.Prices;
|
||||
|
||||
namespace PyroFetes.Endpoints.Prices;
|
||||
|
||||
public class PatchPriceEndpoint(
|
||||
PricesRepository pricesRepository,
|
||||
AutoMapper.IMapper mapper) : Endpoint<PatchPriceSellingPriceDto, GetPriceDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Patch("/prices/{@ProductId}/{@SupplierId}/SellingPrice", x => new { x.ProductId, x.SupplierId });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(PatchPriceSellingPriceDto req, CancellationToken ct)
|
||||
{
|
||||
Price? price = await pricesRepository.FirstOrDefaultAsync(new GetPriceByProductIdAndSupplierIdSpec(req.ProductId, req.SupplierId),ct);
|
||||
|
||||
if (price == null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
price.SellingPrice = req.SellingPrice;
|
||||
|
||||
await pricesRepository.UpdateAsync(price, ct);
|
||||
|
||||
await Send.OkAsync(mapper.Map<GetPriceDto>(price), ct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user