Compare commits

...

2 Commits

Author SHA1 Message Date
Cristiano
7bf0b5bfd1 Advanced refactoring 2025-11-20 14:04:13 +01:00
Cristiano
bd653c149c Finished refactoring prices endpoints 2025-11-19 18:05:28 +01:00
17 changed files with 138 additions and 223 deletions

View File

@@ -1,6 +1,8 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.Models;
using PyroFetes.Repositories;
using PyroFetes.Specifications.Prices;
namespace PyroFetes.Endpoints.Prices;
@@ -10,7 +12,7 @@ public class DeletePriceRequest
public int SupplierId { get; set; }
}
public class DeletePriceEndpoint(PyroFetesDbContext database) : Endpoint<DeletePriceRequest>
public class DeletePriceEndpoint(PricesRepository pricesRepository) : Endpoint<DeletePriceRequest>
{
public override void Configure()
{
@@ -20,8 +22,7 @@ public class DeletePriceEndpoint(PyroFetesDbContext database) : Endpoint<DeleteP
public override async Task HandleAsync(DeletePriceRequest req, CancellationToken ct)
{
Price? price = await database.Prices
.SingleOrDefaultAsync(p => p.ProductId == req.ProductId && p.SupplierId == req.SupplierId, ct);
Price? price = await pricesRepository.FirstOrDefaultAsync(new GetPriceByProductIdAndSupplierIdSpec(req.ProductId,req.SupplierId), ct);
if (price == null)
{
@@ -29,8 +30,7 @@ public class DeletePriceEndpoint(PyroFetesDbContext database) : Endpoint<DeleteP
return;
}
database.Prices.Remove(price);
await database.SaveChangesAsync(ct);
await pricesRepository.DeleteAsync(price, ct);
await Send.NoContentAsync(ct);
}

View File

@@ -1,12 +1,15 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
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(PyroFetesDbContext database) : Endpoint<PatchPriceSellingPriceDto, GetPriceDto>
public class PatchPriceEndpoint(
PricesRepository pricesRepository,
AutoMapper.IMapper mapper) : Endpoint<PatchPriceSellingPriceDto, GetPriceDto>
{
public override void Configure()
{
@@ -16,7 +19,8 @@ public class PatchPriceEndpoint(PyroFetesDbContext database) : Endpoint<PatchPri
public override async Task HandleAsync(PatchPriceSellingPriceDto req, CancellationToken ct)
{
Price? price = await database.Prices.SingleOrDefaultAsync(p => p.ProductId == req.ProductId && p.SupplierId == req.SupplierId, ct);
Price? price = await pricesRepository.FirstOrDefaultAsync(new GetPriceByProductIdAndSupplierIdSpec(req.ProductId, req.SupplierId),ct);
if (price == null)
{
await Send.NotFoundAsync(ct);
@@ -24,14 +28,9 @@ public class PatchPriceEndpoint(PyroFetesDbContext database) : Endpoint<PatchPri
}
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);
await pricesRepository.UpdateAsync(price, ct);
await Send.OkAsync(mapper.Map<GetPriceDto>(price), ct);
}
}

View File

@@ -2,10 +2,11 @@
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Product.Response;
using PyroFetes.Models;
using PyroFetes.Repositories;
namespace PyroFetes.Endpoints.Products;
public class GetAllProductsEndpoint(PyroFetesDbContext database) : EndpointWithoutRequest<List<GetProductDto>>
public class GetAllProductsEndpoint(ProductsRepository productsRepository) : EndpointWithoutRequest<List<GetProductDto>>
{
public override void Configure()
{
@@ -15,23 +16,6 @@ public class GetAllProductsEndpoint(PyroFetesDbContext database) : EndpointWitho
public override async Task HandleAsync(CancellationToken ct)
{
List<GetProductDto> product = await database.Products
.Select(product => new GetProductDto()
{
Id = product.Id,
References = product.Reference,
Name = product.Name,
Duration = product.Duration,
Caliber = product.Caliber,
ApprovalNumber = product.ApprovalNumber,
Weight = product.Weight,
Nec = product.Nec,
Image = product.Image,
Link = product.Link,
MinimalQuantity = product.MinimalQuantity,
})
.ToListAsync(ct);
await Send.OkAsync(product, ct);
await Send.OkAsync(await productsRepository.ProjectToListAsync<GetProductDto>(ct), ct);
}
}

View File

@@ -2,6 +2,8 @@
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Product.Response;
using PyroFetes.Models;
using PyroFetes.Repositories;
using PyroFetes.Specifications.Products;
namespace PyroFetes.Endpoints.Products;
@@ -10,7 +12,9 @@ public class GetProductRequest
public int Id { get; set; }
}
public class GetProductEndpoint(PyroFetesDbContext database) : Endpoint<GetProductRequest, GetProductDto>
public class GetProductEndpoint(
ProductsRepository productsRepository,
AutoMapper.IMapper mapper) : Endpoint<GetProductRequest, GetProductDto>
{
public override void Configure()
{
@@ -20,8 +24,7 @@ public class GetProductEndpoint(PyroFetesDbContext database) : Endpoint<GetProdu
public override async Task HandleAsync(GetProductRequest req, CancellationToken ct)
{
Product? product = await database.Products
.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
Product? product = await productsRepository.FirstOrDefaultAsync(new GetProductByIdSpec(req.Id), ct);
if (product == null)
{
@@ -29,21 +32,6 @@ public class GetProductEndpoint(PyroFetesDbContext database) : Endpoint<GetProdu
return;
}
GetProductDto responseDto = new()
{
Id = product.Id,
References = product.Reference,
Name = product.Name,
Duration = product.Duration,
Caliber = product.Caliber,
ApprovalNumber = product.ApprovalNumber,
Weight = product.Weight,
Nec = product.Nec,
Image = product.Image,
Link = product.Link,
MinimalQuantity = product.MinimalQuantity,
};
await Send.OkAsync(responseDto, ct);
await Send.OkAsync(mapper.Map<GetProductDto>(product), ct);
}
}

View File

@@ -3,11 +3,14 @@ using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Product.Request;
using PyroFetes.DTO.Product.Response;
using PyroFetes.Models;
using PyroFetes.Repositories;
using PyroFetes.Specifications.Products;
namespace PyroFetes.Endpoints.Products;
public class PatchProductMinimalStockEndpoint(PyroFetesDbContext database)
: Endpoint<PatchProductMinimalStockDto, GetProductDto>
public class PatchProductMinimalStockEndpoint(
ProductsRepository productsRepository,
AutoMapper.IMapper mapper) : Endpoint<PatchProductMinimalStockDto, GetProductDto>
{
public override void Configure()
{
@@ -17,7 +20,8 @@ public class PatchProductMinimalStockEndpoint(PyroFetesDbContext database)
public override async Task HandleAsync(PatchProductMinimalStockDto req, CancellationToken ct)
{
Product? product = await database.Products.SingleOrDefaultAsync(po => po.Id == req.Id, ct);
Product? product = await productsRepository.FirstOrDefaultAsync(new GetProductByIdSpec(req.Id), ct);
if (product == null)
{
await Send.NotFoundAsync(ct);
@@ -25,22 +29,8 @@ public class PatchProductMinimalStockEndpoint(PyroFetesDbContext database)
}
product.MinimalQuantity = req.MinimalQuantity;
await database.SaveChangesAsync(ct);
GetProductDto responseDto = new()
{
Id = product.Id,
References = product.Reference,
Name = product.Name,
Duration = product.Duration,
Caliber = product.Caliber,
ApprovalNumber = product.ApprovalNumber,
Weight = product.Weight,
Nec = product.Nec,
Image = product.Image,
Link = product.Link,
MinimalQuantity = product.MinimalQuantity,
};
await Send.OkAsync(responseDto, ct);
await productsRepository.UpdateAsync(product, ct);
await Send.OkAsync(mapper.Map<GetProductDto>(product), ct);
}
}

View File

@@ -3,10 +3,14 @@ using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Product.Request;
using PyroFetes.DTO.Product.Response;
using PyroFetes.Models;
using PyroFetes.Repositories;
using PyroFetes.Specifications.Products;
namespace PyroFetes.Endpoints.Products;
public class UpdateProductEndpoint(PyroFetesDbContext database) : Endpoint<UpdateProductDto, GetProductDto>
public class UpdateProductEndpoint(
ProductsRepository productsRepository,
AutoMapper.IMapper mapper) : Endpoint<UpdateProductDto, GetProductDto>
{
public override void Configure()
{
@@ -16,7 +20,7 @@ public class UpdateProductEndpoint(PyroFetesDbContext database) : Endpoint<Updat
public override async Task HandleAsync(UpdateProductDto req, CancellationToken ct)
{
Product? product = await database.Products.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
Product? product = await productsRepository.FirstOrDefaultAsync(new GetProductByIdSpec(req.Id), ct);
if (product == null)
{
@@ -34,23 +38,9 @@ public class UpdateProductEndpoint(PyroFetesDbContext database) : Endpoint<Updat
product.Image = req.Image;
product.Link = req.Link;
product.MinimalQuantity = req.MinimalQuantity;
await database.SaveChangesAsync(ct);
GetProductDto responseDto = new()
{
Id = product.Id,
References = product.Reference,
Name = product.Name,
Duration = product.Duration,
Caliber = product.Caliber,
ApprovalNumber = product.ApprovalNumber,
Weight = product.Weight,
Nec = product.Nec,
Image = product.Image,
Link = product.Link,
MinimalQuantity = product.MinimalQuantity,
};
await productsRepository.UpdateAsync(product, ct);
await Send.OkAsync(responseDto, ct);
await Send.OkAsync(mapper.Map<GetProductDto>(product), ct);
}
}

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);
}
}

View File

@@ -3,10 +3,17 @@ using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.PurchaseProduct.Request;
using PyroFetes.DTO.PurchaseProduct.Response;
using PyroFetes.Models;
using PyroFetes.Repositories;
using PyroFetes.Specifications.Products;
using PyroFetes.Specifications.PurchaseOrders;
namespace PyroFetes.Endpoints.PurchaseProducts;
public class CreatePurchaseProductEndpoint(PyroFetesDbContext database) : Endpoint<CreatePurchaseProductDto, GetPurchaseProductDto>
public class CreatePurchaseProductEndpoint(
ProductsRepository productsRepository,
PurchaseOrdersRepository purchaseOrdersRepository,
PurchaseProductsRepository purchaseProductsRepository,
AutoMapper.IMapper mapper) : Endpoint<CreatePurchaseProductDto, GetPurchaseProductDto>
{
public override void Configure()
{
@@ -16,14 +23,14 @@ public class CreatePurchaseProductEndpoint(PyroFetesDbContext database) : Endpoi
public override async Task HandleAsync(CreatePurchaseProductDto req, CancellationToken ct)
{
Product? product = await database.Products.FirstOrDefaultAsync(p => p.Id == req.ProductId, ct);
Product? product = await productsRepository.FirstOrDefaultAsync(new GetProductByIdSpec(req.ProductId), ct);
if (product == null)
{
await Send.NotFoundAsync(ct);
return;
}
PurchaseOrder? purchaseOrder = await database.PurchaseOrders.FirstOrDefaultAsync(po => po.Id == req.PurchaseOrderId, ct);
PurchaseOrder? purchaseOrder = await purchaseOrdersRepository.FirstOrDefaultAsync(new GetPurchaseOrderByIdSpec(req.PurchaseOrderId), ct);
if (purchaseOrder == null)
{
@@ -31,8 +38,7 @@ public class CreatePurchaseProductEndpoint(PyroFetesDbContext database) : Endpoi
{
PurchaseConditions = req.PurchaseOrderPurchaseConditions ?? "Conditions non précisées"
};
database.PurchaseOrders.Add(purchaseOrder);
await database.SaveChangesAsync(ct);
await purchaseOrdersRepository.AddAsync(purchaseOrder, ct);
}
PurchaseProduct purchaseProduct = new PurchaseProduct()
@@ -41,28 +47,9 @@ public class CreatePurchaseProductEndpoint(PyroFetesDbContext database) : Endpoi
PurchaseOrderId = purchaseOrder.Id,
Quantity = req.Quantity
};
database.PurchaseProducts.Add(purchaseProduct);
await database.SaveChangesAsync(ct);
GetPurchaseProductDto responseDto = new GetPurchaseProductDto()
{
ProductId = product.Id,
ProductReferences = product.Reference,
ProductName = product.Name,
ProductDuration = product.Duration,
ProductCaliber = product.Caliber,
ProductApprovalNumber = product.ApprovalNumber,
ProductWeight = product.Weight,
ProductNec = product.Nec,
ProductImage = product.Image,
ProductLink = product.Link,
ProductMinimalQuantity = product.MinimalQuantity,
PurchaseOrderId = purchaseOrder.Id,
PurchaseOrderPurchaseConditions = purchaseOrder.PurchaseConditions,
Quantity = purchaseProduct.Quantity
};
await Send.OkAsync(responseDto, ct);
await purchaseProductsRepository.AddAsync(purchaseProduct, ct);
await Send.OkAsync(mapper.Map<GetPurchaseProductDto>(purchaseProduct), ct);
}
}

View File

@@ -1,6 +1,8 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.Models;
using PyroFetes.Repositories;
using PyroFetes.Specifications.PurchaseProducts;
namespace PyroFetes.Endpoints.PurchaseProducts;
@@ -10,7 +12,7 @@ public class DeletePurchaseProductRequest
public int PurchaseOrderId { get; set; }
}
public class DeletePurchaseOrderEndpoint(PyroFetesDbContext database) : Endpoint<DeletePurchaseProductRequest>
public class DeletePurchaseProductEndpoint(PurchaseProductsRepository purchaseProductsRepository) : Endpoint<DeletePurchaseProductRequest>
{
public override void Configure()
{
@@ -20,8 +22,8 @@ public class DeletePurchaseOrderEndpoint(PyroFetesDbContext database) : Endpoint
public override async Task HandleAsync(DeletePurchaseProductRequest req, CancellationToken ct)
{
PurchaseProduct? purchaseProduct = await database.PurchaseProducts
.SingleOrDefaultAsync(po => po.ProductId == req.ProductId && po.PurchaseOrderId == req.PurchaseOrderId, ct);
PurchaseProduct? purchaseProduct = await purchaseProductsRepository.FirstOrDefaultAsync(
new GetPurchaseProductByProductIdAndPurchaseOrderIdSpec(req.ProductId, req.PurchaseOrderId), ct);
if (purchaseProduct == null)
{
@@ -29,8 +31,7 @@ public class DeletePurchaseOrderEndpoint(PyroFetesDbContext database) : Endpoint
return;
}
database.PurchaseProducts.Remove(purchaseProduct);
await database.SaveChangesAsync(ct);
await purchaseProductsRepository.DeleteAsync(purchaseProduct, ct);
await Send.NoContentAsync(ct);
}

View File

@@ -3,10 +3,14 @@ using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.PurchaseProduct.Request;
using PyroFetes.DTO.PurchaseProduct.Response;
using PyroFetes.Models;
using PyroFetes.Repositories;
using PyroFetes.Specifications.PurchaseProducts;
namespace PyroFetes.Endpoints.PurchaseProducts;
public class PatchPurchaseProductQuantityEndpoint(PyroFetesDbContext database) : Endpoint<PatchPurchaseProductQuantityDto, GetPurchaseProductDto>
public class PatchPurchaseProductQuantityEndpoint(
PurchaseProductsRepository purchaseProductsRepository,
AutoMapper.IMapper mapper) : Endpoint<PatchPurchaseProductQuantityDto, GetPurchaseProductDto>
{
public override void Configure()
{
@@ -16,7 +20,10 @@ public class PatchPurchaseProductQuantityEndpoint(PyroFetesDbContext database) :
public override async Task HandleAsync(PatchPurchaseProductQuantityDto req, CancellationToken ct)
{
PurchaseProduct? purchaseProduct = await database.PurchaseProducts.SingleOrDefaultAsync(po => po.ProductId == req.ProductId && po.PurchaseOrderId == req.PurchaseOrderId, ct);
PurchaseProduct? purchaseProduct =
await purchaseProductsRepository.FirstOrDefaultAsync(
new GetPurchaseProductByProductIdAndPurchaseOrderIdSpec(req.ProductId, req.PurchaseOrderId), ct);
if (purchaseProduct == null)
{
await Send.NotFoundAsync(ct);
@@ -24,14 +31,8 @@ public class PatchPurchaseProductQuantityEndpoint(PyroFetesDbContext database) :
}
purchaseProduct.Quantity = req.Quantity;
await database.SaveChangesAsync(ct);
await purchaseProductsRepository.UpdateAsync(purchaseProduct, ct);
GetPurchaseProductDto responseDto = new()
{
ProductId = purchaseProduct.ProductId,
PurchaseOrderId = purchaseProduct.PurchaseOrderId,
Quantity = purchaseProduct.Quantity
};
await Send.OkAsync(responseDto, ct);
await Send.OkAsync(mapper.Map<GetPurchaseProductDto>(purchaseProduct), ct);
}
}

View File

@@ -0,0 +1,5 @@
using PyroFetes.Models;
namespace PyroFetes.Repositories;
public class PurchaseOrdersRepository(PyroFetesDbContext pyrofetesContext, AutoMapper.IMapper mapper) : PyrofetesRepository<PurchaseOrder>(pyrofetesContext, mapper);

View File

@@ -0,0 +1,5 @@
using PyroFetes.Models;
namespace PyroFetes.Repositories;
public class PurchaseProductsRepository(PyroFetesDbContext pyrofetesContext, AutoMapper.IMapper mapper) : PyrofetesRepository<PurchaseProduct>(pyrofetesContext, mapper);

View File

@@ -0,0 +1,14 @@
using Ardalis.Specification;
using PyroFetes.Models;
namespace PyroFetes.Specifications.PurchaseOrders;
public sealed class GetPurchaseOrderByIdSpec : Specification<PurchaseOrder>
{
public GetPurchaseOrderByIdSpec(int purchaseOrderId)
{
Query
.Include(po => po.PurchaseProducts)
.Where(po => po.Id == purchaseOrderId);
}
}

View File

@@ -0,0 +1,13 @@
using Ardalis.Specification;
using PyroFetes.Models;
namespace PyroFetes.Specifications.PurchaseProducts;
public sealed class GetPurchaseProductByProductIdAndPurchaseOrderIdSpec : Specification<PurchaseProduct>
{
public GetPurchaseProductByProductIdAndPurchaseOrderIdSpec(int productId, int purchaseOrderId)
{
Query
.Where(p => p.ProductId == productId && p.PurchaseOrderId == purchaseOrderId);
}
}