forked from sanchezvem/PyroFetes
Advanced refactoring
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user