Advanced refactoring

This commit is contained in:
Cristiano
2025-11-20 14:04:13 +01:00
parent bd653c149c
commit 7bf0b5bfd1
16 changed files with 123 additions and 207 deletions

View File

@@ -1,6 +1,8 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.Models;
using PyroFetes.Repositories;
using PyroFetes.Specifications.PurchaseOrders;
namespace PyroFetes.Endpoints.PurchaseOrders;
@@ -9,7 +11,9 @@ public class DeletePurchaseOrderRequest
public int Id { get; set; }
}
public class DeletePurchaseOrderEndpoint(PyroFetesDbContext database) : Endpoint<DeletePurchaseOrderRequest>
public class DeletePurchaseOrderEndpoint(
PurchaseOrdersRepository purchaseOrdersRepository,
PurchaseProductsRepository purchaseProductsRepository) : Endpoint<DeletePurchaseOrderRequest>
{
public override void Configure()
{
@@ -19,9 +23,7 @@ public class DeletePurchaseOrderEndpoint(PyroFetesDbContext database) : Endpoint
public override async Task HandleAsync(DeletePurchaseOrderRequest req, CancellationToken ct)
{
PurchaseOrder? purchaseOrder = await database.PurchaseOrders
.Include(po => po.PurchaseProducts)
.SingleOrDefaultAsync(po => po.Id == req.Id, ct);
PurchaseOrder? purchaseOrder = await purchaseOrdersRepository.FirstOrDefaultAsync(new GetPurchaseOrderByIdSpec(req.Id), ct);
if (purchaseOrder == null)
{
@@ -31,11 +33,10 @@ public class DeletePurchaseOrderEndpoint(PyroFetesDbContext database) : Endpoint
if (purchaseOrder.PurchaseProducts != null && purchaseOrder.PurchaseProducts.Any())
{
database.PurchaseProducts.RemoveRange(purchaseOrder.PurchaseProducts);
await purchaseProductsRepository.DeleteRangeAsync(purchaseOrder.PurchaseProducts, ct);
}
database.PurchaseOrders.Remove(purchaseOrder);
await database.SaveChangesAsync(ct);
await purchaseOrdersRepository.DeleteAsync(purchaseOrder, ct);
await Send.NoContentAsync(ct);
}

View File

@@ -2,10 +2,11 @@
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.PurchaseOrder.Response;
using PyroFetes.DTO.PurchaseProduct.Response;
using PyroFetes.Repositories;
namespace PyroFetes.Endpoints.PurchaseOrders;
public class GetAllPurchaseOrderEndpoint(PyroFetesDbContext database) : EndpointWithoutRequest<List<GetPurchaseOrderDto>>
public class GetAllPurchaseOrderEndpoint(PurchaseOrdersRepository purchaseOrdersRepository) : EndpointWithoutRequest<List<GetPurchaseOrderDto>>
{
public override void Configure()
{
@@ -15,32 +16,6 @@ public class GetAllPurchaseOrderEndpoint(PyroFetesDbContext database) : Endpoint
public override async Task HandleAsync(CancellationToken ct)
{
List<GetPurchaseOrderDto> purchaseOrder = await database.PurchaseOrders
.Include(p => p.PurchaseProducts)
.Select(purchaseOrder => new GetPurchaseOrderDto()
{
Id = purchaseOrder.Id,
PurchaseConditions = purchaseOrder.PurchaseConditions,
GetPurchaseProductDto = purchaseOrder.PurchaseProducts
.Select(p => new GetPurchaseProductDto
{
ProductId = p.ProductId,
ProductReferences = p.Product.Reference,
ProductName = p.Product.Name,
ProductDuration = p.Product.Duration,
ProductCaliber = p.Product.Caliber,
ProductApprovalNumber = p.Product.ApprovalNumber,
ProductWeight = p.Product.Weight,
ProductNec = p.Product.Nec,
ProductImage = p.Product.Image,
ProductLink = p.Product.Link,
ProductMinimalQuantity = p.Product.MinimalQuantity,
PurchaseOrderId = p.PurchaseOrderId,
Quantity = p.Quantity,
}).ToList()
})
.ToListAsync(ct);
await Send.OkAsync(purchaseOrder, ct);
await Send.OkAsync(await purchaseOrdersRepository.ProjectToListAsync<GetPurchaseOrderDto>(ct), ct);
}
}

View File

@@ -3,6 +3,8 @@ using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.PurchaseOrder.Response;
using PyroFetes.DTO.PurchaseProduct.Response;
using PyroFetes.Models;
using PyroFetes.Repositories;
using PyroFetes.Specifications.PurchaseOrders;
namespace PyroFetes.Endpoints.PurchaseOrders;
@@ -11,7 +13,9 @@ public class GetPurchaseOrderRequest
public int Id { get; set; }
}
public class GetPurchaseOrderEndpoint(PyroFetesDbContext database) : Endpoint<GetPurchaseOrderRequest, GetPurchaseOrderDto>
public class GetPurchaseOrderEndpoint(
PurchaseOrdersRepository purchaseOrdersRepository,
AutoMapper.IMapper mapper) : Endpoint<GetPurchaseOrderRequest, GetPurchaseOrderDto>
{
public override void Configure()
{
@@ -21,8 +25,7 @@ public class GetPurchaseOrderEndpoint(PyroFetesDbContext database) : Endpoint<Ge
public override async Task HandleAsync(GetPurchaseOrderRequest req, CancellationToken ct)
{
PurchaseOrder? purchaseOrder = await database.PurchaseOrders
.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
PurchaseOrder? purchaseOrder = await purchaseOrdersRepository.FirstOrDefaultAsync(new GetPurchaseOrderByIdSpec(req.Id), ct);
if (purchaseOrder == null)
{
@@ -30,29 +33,6 @@ public class GetPurchaseOrderEndpoint(PyroFetesDbContext database) : Endpoint<Ge
return;
}
GetPurchaseOrderDto responseDto = new()
{
Id = purchaseOrder.Id,
PurchaseConditions = purchaseOrder.PurchaseConditions,
GetPurchaseProductDto = purchaseOrder.PurchaseProducts
.Select(p => new GetPurchaseProductDto
{
ProductId = p.ProductId,
ProductReferences = p.Product.Reference,
ProductName = p.Product.Name,
ProductDuration = p.Product.Duration,
ProductCaliber = p.Product.Caliber,
ProductApprovalNumber = p.Product.ApprovalNumber,
ProductWeight = p.Product.Weight,
ProductNec = p.Product.Nec,
ProductImage = p.Product.Image,
ProductLink = p.Product.Link,
ProductMinimalQuantity = p.Product.MinimalQuantity,
PurchaseOrderId = p.PurchaseOrderId,
Quantity = p.Quantity,
}).ToList()
};
await Send.OkAsync(responseDto, ct);
await Send.OkAsync(mapper.Map<GetPurchaseOrderDto>(purchaseOrder), ct);
}
}

View File

@@ -4,10 +4,14 @@ 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(PyroFetesDbContext database) : Endpoint<PatchPurchaseOrderPurchaseConditionsDto, GetPurchaseOrderDto>
public class PatchPurchaseOrderPurchaseConditionsEndpoint(
PurchaseOrdersRepository purchaseOrdersRepository,
AutoMapper.IMapper mapper) : Endpoint<PatchPurchaseOrderPurchaseConditionsDto, GetPurchaseOrderDto>
{
public override void Configure()
{
@@ -17,7 +21,7 @@ public class PatchPurchaseOrderPurchaseConditionsEndpoint(PyroFetesDbContext dat
public override async Task HandleAsync(PatchPurchaseOrderPurchaseConditionsDto req, CancellationToken ct)
{
PurchaseOrder? purchaseOrder = await database.PurchaseOrders.SingleOrDefaultAsync(po => po.Id == req.Id, ct);
PurchaseOrder? purchaseOrder = await purchaseOrdersRepository.FirstOrDefaultAsync(new GetPurchaseOrderByIdSpec(req.Id), ct);
if (purchaseOrder == null)
{
await Send.NotFoundAsync(ct);
@@ -25,30 +29,8 @@ public class PatchPurchaseOrderPurchaseConditionsEndpoint(PyroFetesDbContext dat
}
purchaseOrder.PurchaseConditions = req.PurchaseConditions;
await database.SaveChangesAsync(ct);
await purchaseOrdersRepository.UpdateAsync(purchaseOrder, ct);
GetPurchaseOrderDto responseDto = new()
{
Id = purchaseOrder.Id,
PurchaseConditions = purchaseOrder.PurchaseConditions,
GetPurchaseProductDto = purchaseOrder.PurchaseProducts
.Select(p => new GetPurchaseProductDto
{
ProductId = p.ProductId,
ProductReferences = p.Product.Reference,
ProductName = p.Product.Name,
ProductDuration = p.Product.Duration,
ProductCaliber = p.Product.Caliber,
ProductApprovalNumber = p.Product.ApprovalNumber,
ProductWeight = p.Product.Weight,
ProductNec = p.Product.Nec,
ProductImage = p.Product.Image,
ProductLink = p.Product.Link,
ProductMinimalQuantity = p.Product.MinimalQuantity,
PurchaseOrderId = p.PurchaseOrderId,
Quantity = p.Quantity,
}).ToList()
};
await Send.OkAsync(responseDto, ct);
await Send.OkAsync(mapper.Map<GetPurchaseOrderDto>(purchaseOrder), ct);
}
}