Refactor all code
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
using FastEndpoints;
|
||||
using PyroFetes.DTO.PurchaseProduct.Request;
|
||||
using PyroFetes.Models;
|
||||
using PyroFetes.Repositories;
|
||||
using PyroFetes.Specifications.PurchaseProducts;
|
||||
|
||||
namespace PyroFetes.Endpoints.PurchaseOrders;
|
||||
|
||||
public class AddProductFromPurchaseOrderEndpoint(PurchaseProductsRepository purchaseProductsRepository, AutoMapper.IMapper mapper) : Endpoint<CreatePurchaseProductDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Post("/purchaseOrders/{@PurchaseOrderId}/{@ProductId}", x => new { x.PurchaseOrderId, x.ProductId });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CreatePurchaseProductDto req, CancellationToken ct)
|
||||
{
|
||||
PurchaseProduct? purchaseOrderProduct =
|
||||
await purchaseProductsRepository.SingleOrDefaultAsync(new GetPurchaseProductByProductIdAndPurchaseOrderIdSpec(req.ProductId, req.PurchaseOrderId), ct);
|
||||
if (purchaseOrderProduct is not null)
|
||||
{
|
||||
await Send.StringAsync("Le produit est déjà dans le bon de commande", 400, cancellation: ct);
|
||||
return;
|
||||
}
|
||||
|
||||
purchaseOrderProduct = mapper.Map<PurchaseProduct>(req);
|
||||
|
||||
await purchaseProductsRepository.AddAsync(purchaseOrderProduct, ct);
|
||||
await Send.NoContentAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,18 @@
|
||||
using FastEndpoints;
|
||||
using PyroFetes.DTO.PurchaseOrder.Request;
|
||||
using PyroFetes.DTO.PurchaseOrder.Response;
|
||||
using PyroFetes.DTO.PurchaseProduct.Request;
|
||||
using PyroFetes.DTO.PurchaseProduct.Response;
|
||||
using PyroFetes.Models;
|
||||
using PyroFetes.Repositories;
|
||||
using PyroFetes.Specifications.Products;
|
||||
using PyroFetes.Specifications.PurchaseOrders;
|
||||
using PyroFetes.Specifications.PurchaseProducts;
|
||||
|
||||
namespace PyroFetes.Endpoints.PurchaseOrders;
|
||||
|
||||
public class CreatePurchaseOrder(
|
||||
PurchaseOrdersRepository purchaseOrdersRepository,
|
||||
ProductsRepository productsRepository,
|
||||
AutoMapper.IMapper mapper) : Endpoint<CreatePurchaseOrderDto, GetPurchaseOrderDto>
|
||||
PurchaseProductsRepository purchaseProductsRepository,
|
||||
AutoMapper.IMapper mapper) : Endpoint<CreatePurchaseOrderDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
@@ -23,30 +22,35 @@ public class CreatePurchaseOrder(
|
||||
|
||||
public override async Task HandleAsync(CreatePurchaseOrderDto req, CancellationToken ct)
|
||||
{
|
||||
PurchaseOrder purchaseOrder = new PurchaseOrder
|
||||
{
|
||||
PurchaseConditions = req.PurchaseConditions ?? "Conditions non précisées",
|
||||
PurchaseProducts = new List<PurchaseProduct>()
|
||||
};
|
||||
PurchaseOrder purchaseOrder = mapper.Map<PurchaseOrder>(req);
|
||||
|
||||
foreach (var line in req.Products)
|
||||
if (req.Products != null)
|
||||
{
|
||||
var product = await productsRepository.GetByIdAsync(line.ProductId, ct);
|
||||
if (product == null)
|
||||
foreach (CreatePurchaseOrderProductDto line in req.Products)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
Product? product = await productsRepository.SingleOrDefaultAsync(new GetProductByIdSpec(line.ProductId), ct);
|
||||
if (product is null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
PurchaseProduct? purchaseProduct =
|
||||
await purchaseProductsRepository.SingleOrDefaultAsync(new GetPurchaseProductByProductIdAndPurchaseOrderIdSpec(line.ProductId, purchaseOrder.Id), ct);
|
||||
|
||||
if (purchaseProduct is not null)
|
||||
{
|
||||
await Send.StringAsync("Le produit est déjà dans le bon de commande", 400, cancellation: ct);
|
||||
}
|
||||
|
||||
PurchaseProduct? productOnPurchase = mapper.Map<PurchaseProduct>(line);
|
||||
productOnPurchase.PurchaseOrderId = purchaseOrder.Id;
|
||||
|
||||
await purchaseProductsRepository.AddAsync(productOnPurchase, ct);
|
||||
}
|
||||
|
||||
purchaseOrder.PurchaseProducts.Add(new PurchaseProduct
|
||||
{
|
||||
ProductId = product.Id,
|
||||
Quantity = line.Quantity,
|
||||
});
|
||||
}
|
||||
|
||||
await purchaseOrdersRepository.AddAsync(purchaseOrder, ct);
|
||||
|
||||
await Send.OkAsync(mapper.Map<GetPurchaseOrderDto>(purchaseOrder), ct);
|
||||
await Send.NoContentAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using FastEndpoints;
|
||||
using PyroFetes.Models;
|
||||
using PyroFetes.Repositories;
|
||||
using PyroFetes.Specifications.PurchaseProducts;
|
||||
|
||||
namespace PyroFetes.Endpoints.PurchaseOrders;
|
||||
|
||||
public class DeletePurchaseProductRequest
|
||||
{
|
||||
public int ProductId { get; set; }
|
||||
public int PurchaseOrderId { get; set; }
|
||||
}
|
||||
|
||||
public class DeleteProductFromPurchaseOrderEndpoint(PurchaseProductsRepository purchaseProductsRepository) : Endpoint<DeletePurchaseProductRequest>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Delete("/purchaseOrders/{@ProductId}/{@PurchaseOrderId}", x => new { x.ProductId, x.PurchaseOrderId });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(DeletePurchaseProductRequest req, CancellationToken ct)
|
||||
{
|
||||
PurchaseProduct? purchaseProduct =
|
||||
await purchaseProductsRepository.SingleOrDefaultAsync(new GetPurchaseProductByProductIdAndPurchaseOrderIdSpec(req.ProductId, req.PurchaseOrderId), ct);
|
||||
|
||||
if (purchaseProduct is null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
await purchaseProductsRepository.DeleteAsync(purchaseProduct, ct);
|
||||
await Send.NoContentAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes.Models;
|
||||
using PyroFetes.Repositories;
|
||||
using PyroFetes.Specifications.PurchaseOrders;
|
||||
@@ -11,13 +10,11 @@ public class DeletePurchaseOrderRequest
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
||||
public class DeletePurchaseOrderEndpoint(
|
||||
PurchaseOrdersRepository purchaseOrdersRepository,
|
||||
PurchaseProductsRepository purchaseProductsRepository) : Endpoint<DeletePurchaseOrderRequest>
|
||||
public class DeletePurchaseOrderEndpoint(PurchaseOrdersRepository purchaseOrdersRepository) : Endpoint<DeletePurchaseOrderRequest>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Delete("/purchaseOrders/{@Id}", x => new {x.Id});
|
||||
Delete("/purchaseOrders/{@Id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
@@ -25,19 +22,13 @@ public class DeletePurchaseOrderEndpoint(
|
||||
{
|
||||
PurchaseOrder? purchaseOrder = await purchaseOrdersRepository.FirstOrDefaultAsync(new GetPurchaseOrderByIdSpec(req.Id), ct);
|
||||
|
||||
if (purchaseOrder == null)
|
||||
if (purchaseOrder is null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
if (purchaseOrder.PurchaseProducts != null && purchaseOrder.PurchaseProducts.Any())
|
||||
{
|
||||
await purchaseProductsRepository.DeleteRangeAsync(purchaseOrder.PurchaseProducts, ct);
|
||||
}
|
||||
|
||||
await purchaseOrdersRepository.DeleteAsync(purchaseOrder, ct);
|
||||
|
||||
|
||||
await purchaseOrdersRepository.DeleteAsync(purchaseOrder, ct);
|
||||
await Send.NoContentAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes.DTO.PurchaseOrder.Response;
|
||||
using PyroFetes.DTO.PurchaseProduct.Response;
|
||||
using PyroFetes.Repositories;
|
||||
|
||||
namespace PyroFetes.Endpoints.PurchaseOrders;
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes.DTO.PurchaseOrder.Response;
|
||||
using PyroFetes.DTO.PurchaseProduct.Response;
|
||||
using PyroFetes.Models;
|
||||
using PyroFetes.Repositories;
|
||||
using PyroFetes.Specifications.PurchaseOrders;
|
||||
@@ -13,26 +11,24 @@ public class GetPurchaseOrderRequest
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
||||
public class GetPurchaseOrderEndpoint(
|
||||
PurchaseOrdersRepository purchaseOrdersRepository,
|
||||
AutoMapper.IMapper mapper) : Endpoint<GetPurchaseOrderRequest, GetPurchaseOrderDto>
|
||||
public class GetPurchaseOrderEndpoint(PurchaseOrdersRepository purchaseOrdersRepository, AutoMapper.IMapper mapper) : Endpoint<GetPurchaseOrderRequest, GetPurchaseOrderDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/purchaseOrders/{@Id}", x => new {x.Id});
|
||||
Get("/purchaseOrders/{@Id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(GetPurchaseOrderRequest req, CancellationToken ct)
|
||||
{
|
||||
PurchaseOrder? purchaseOrder = await purchaseOrdersRepository.FirstOrDefaultAsync(new GetPurchaseOrderByIdSpec(req.Id), ct);
|
||||
PurchaseOrder? purchaseOrder = await purchaseOrdersRepository.SingleOrDefaultAsync(new GetPurchaseOrderByIdSpec(req.Id), ct);
|
||||
|
||||
if (purchaseOrder == null)
|
||||
if (purchaseOrder is null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
await Send.OkAsync(mapper.Map<GetPurchaseOrderDto>(purchaseOrder), ct);
|
||||
}
|
||||
}
|
||||
@@ -5,37 +5,40 @@ using PyroFetes.Models;
|
||||
using PyroFetes.Repositories;
|
||||
using PyroFetes.Services.Pdf;
|
||||
using PyroFetes.Specifications.PurchaseOrders;
|
||||
|
||||
namespace PyroFetes.Endpoints.PurchaseOrders;
|
||||
|
||||
public class GetPurchaseOrderPdfEndpoint(
|
||||
PurchaseOrdersRepository purchaseOrdersRepository,
|
||||
IPurchaseOrderPdfService purchaseOrderPdfService)
|
||||
IPurchaseOrderPdfService purchaseOrderPdfService,
|
||||
SettingsRepository settingsRepository)
|
||||
: Endpoint<GetPurchaseOrderPdfDto, byte[]>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/purchaseOrders/{@Id}/pdf", x => new {x.Id});
|
||||
Get("/purchaseOrders/{@Id}/pdf", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
Description(b => b.Produces<byte[]>(200, MediaTypeNames.Application.Pdf));
|
||||
}
|
||||
|
||||
|
||||
public override async Task HandleAsync(GetPurchaseOrderPdfDto req, CancellationToken ct)
|
||||
{
|
||||
PurchaseOrder? purchaseOrder = await purchaseOrdersRepository
|
||||
.FirstOrDefaultAsync(new GetPurchaseOrderByIdWithProductsSpec(req.Id), ct);
|
||||
|
||||
if (purchaseOrder == null)
|
||||
PurchaseOrder? purchaseOrder = await purchaseOrdersRepository.SingleOrDefaultAsync(new GetPurchaseOrderByIdWithProductsSpec(req.Id), ct);
|
||||
|
||||
if (purchaseOrder is null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
Setting? setting = await settingsRepository.FirstOrDefaultAsync(ct);
|
||||
|
||||
var bytes = purchaseOrderPdfService.Generate(purchaseOrder, purchaseOrder.PurchaseProducts!);
|
||||
byte[] bytes = purchaseOrderPdfService.Generate(purchaseOrder, purchaseOrder.PurchaseProducts!, setting!);
|
||||
|
||||
await Send.BytesAsync(
|
||||
bytes: bytes,
|
||||
contentType: "application/pdf",
|
||||
fileName: $"bon-de-commande-{purchaseOrder.Id}.pdf",
|
||||
fileName: $"bon-de-commande-{purchaseOrder.Id}{DateOnly.FromDateTime(DateTime.Now)}.pdf",
|
||||
cancellation: ct);
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,14 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes.DTO.PurchaseOrder.Request;
|
||||
using PyroFetes.DTO.PurchaseOrder.Response;
|
||||
using PyroFetes.DTO.PurchaseProduct.Response;
|
||||
using PyroFetes.Models;
|
||||
using PyroFetes.Repositories;
|
||||
using PyroFetes.Specifications.PurchaseOrders;
|
||||
|
||||
namespace PyroFetes.Endpoints.PurchaseOrders;
|
||||
|
||||
public class PatchPurchaseOrderPurchaseConditionsEndpoint(
|
||||
PurchaseOrdersRepository purchaseOrdersRepository,
|
||||
AutoMapper.IMapper mapper) : Endpoint<PatchPurchaseOrderPurchaseConditionsDto, GetPurchaseOrderDto>
|
||||
public class PatchPurchaseOrderPurchaseConditionsEndpoint(PurchaseOrdersRepository purchaseOrdersRepository, AutoMapper.IMapper mapper)
|
||||
: Endpoint<PatchPurchaseOrderPurchaseConditionsDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
@@ -21,16 +18,16 @@ public class PatchPurchaseOrderPurchaseConditionsEndpoint(
|
||||
|
||||
public override async Task HandleAsync(PatchPurchaseOrderPurchaseConditionsDto req, CancellationToken ct)
|
||||
{
|
||||
PurchaseOrder? purchaseOrder = await purchaseOrdersRepository.FirstOrDefaultAsync(new GetPurchaseOrderByIdSpec(req.Id), ct);
|
||||
if (purchaseOrder == null)
|
||||
PurchaseOrder? purchaseOrder = await purchaseOrdersRepository.SingleOrDefaultAsync(new GetPurchaseOrderByIdSpec(req.Id), ct);
|
||||
if (purchaseOrder is null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
purchaseOrder.PurchaseConditions = req.PurchaseConditions;
|
||||
await purchaseOrdersRepository.UpdateAsync(purchaseOrder, ct);
|
||||
|
||||
await Send.OkAsync(mapper.Map<GetPurchaseOrderDto>(purchaseOrder), ct);
|
||||
mapper.Map(req, purchaseOrder);
|
||||
|
||||
await purchaseOrdersRepository.UpdateAsync(purchaseOrder, ct);
|
||||
await Send.NoContentAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using FastEndpoints;
|
||||
using PyroFetes.DTO.PurchaseProduct.Request;
|
||||
using PyroFetes.DTO.PurchaseProduct.Response;
|
||||
using PyroFetes.Models;
|
||||
using PyroFetes.Repositories;
|
||||
using PyroFetes.Specifications.PurchaseProducts;
|
||||
|
||||
namespace PyroFetes.Endpoints.PurchaseOrders;
|
||||
|
||||
public class PatchPurchaseProductQuantityEndpoint(PurchaseProductsRepository purchaseProductsRepository, AutoMapper.IMapper mapper)
|
||||
: Endpoint<PatchPurchaseProductQuantityDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Patch("/purchaseOrders/{@ProductId}/{@PurchaseOrderId}/Quantity", x => new { x.ProductId, x.PurchaseOrderId });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(PatchPurchaseProductQuantityDto req, CancellationToken ct)
|
||||
{
|
||||
PurchaseProduct? purchaseProduct =
|
||||
await purchaseProductsRepository.SingleOrDefaultAsync(new GetPurchaseProductByProductIdAndPurchaseOrderIdSpec(req.ProductId, req.PurchaseOrderId), ct);
|
||||
|
||||
if (purchaseProduct is null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
mapper.Map(req, purchaseProduct);
|
||||
|
||||
await purchaseProductsRepository.SaveChangesAsync(ct);
|
||||
await Send.NoContentAsync(ct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user