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

@@ -32,6 +32,6 @@ public class DeletePriceEndpoint(PricesRepository pricesRepository) : Endpoint<D
await pricesRepository.DeleteAsync(price, ct); await pricesRepository.DeleteAsync(price, ct);
await Send.OkAsync(ct); await Send.NoContentAsync(ct);
} }
} }

View File

@@ -2,10 +2,11 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Product.Response; using PyroFetes.DTO.Product.Response;
using PyroFetes.Models; using PyroFetes.Models;
using PyroFetes.Repositories;
namespace PyroFetes.Endpoints.Products; namespace PyroFetes.Endpoints.Products;
public class GetAllProductsEndpoint(PyroFetesDbContext database) : EndpointWithoutRequest<List<GetProductDto>> public class GetAllProductsEndpoint(ProductsRepository productsRepository) : EndpointWithoutRequest<List<GetProductDto>>
{ {
public override void Configure() public override void Configure()
{ {
@@ -15,23 +16,6 @@ public class GetAllProductsEndpoint(PyroFetesDbContext database) : EndpointWitho
public override async Task HandleAsync(CancellationToken ct) public override async Task HandleAsync(CancellationToken ct)
{ {
List<GetProductDto> product = await database.Products await Send.OkAsync(await productsRepository.ProjectToListAsync<GetProductDto>(ct), ct);
.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);
} }
} }

View File

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

View File

@@ -3,11 +3,14 @@ using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Product.Request; using PyroFetes.DTO.Product.Request;
using PyroFetes.DTO.Product.Response; using PyroFetes.DTO.Product.Response;
using PyroFetes.Models; using PyroFetes.Models;
using PyroFetes.Repositories;
using PyroFetes.Specifications.Products;
namespace PyroFetes.Endpoints.Products; namespace PyroFetes.Endpoints.Products;
public class PatchProductMinimalStockEndpoint(PyroFetesDbContext database) public class PatchProductMinimalStockEndpoint(
: Endpoint<PatchProductMinimalStockDto, GetProductDto> ProductsRepository productsRepository,
AutoMapper.IMapper mapper) : Endpoint<PatchProductMinimalStockDto, GetProductDto>
{ {
public override void Configure() public override void Configure()
{ {
@@ -17,7 +20,8 @@ public class PatchProductMinimalStockEndpoint(PyroFetesDbContext database)
public override async Task HandleAsync(PatchProductMinimalStockDto req, CancellationToken ct) 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) if (product == null)
{ {
await Send.NotFoundAsync(ct); await Send.NotFoundAsync(ct);
@@ -25,22 +29,8 @@ public class PatchProductMinimalStockEndpoint(PyroFetesDbContext database)
} }
product.MinimalQuantity = req.MinimalQuantity; product.MinimalQuantity = req.MinimalQuantity;
await database.SaveChangesAsync(ct); await productsRepository.UpdateAsync(product, ct);
GetProductDto responseDto = new() await Send.OkAsync(mapper.Map<GetProductDto>(product), ct);
{
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);
} }
} }

View File

@@ -3,10 +3,14 @@ using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Product.Request; using PyroFetes.DTO.Product.Request;
using PyroFetes.DTO.Product.Response; using PyroFetes.DTO.Product.Response;
using PyroFetes.Models; using PyroFetes.Models;
using PyroFetes.Repositories;
using PyroFetes.Specifications.Products;
namespace PyroFetes.Endpoints.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() public override void Configure()
{ {
@@ -16,7 +20,7 @@ public class UpdateProductEndpoint(PyroFetesDbContext database) : Endpoint<Updat
public override async Task HandleAsync(UpdateProductDto req, CancellationToken ct) 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) if (product == null)
{ {
@@ -34,23 +38,9 @@ public class UpdateProductEndpoint(PyroFetesDbContext database) : Endpoint<Updat
product.Image = req.Image; product.Image = req.Image;
product.Link = req.Link; product.Link = req.Link;
product.MinimalQuantity = req.MinimalQuantity; product.MinimalQuantity = req.MinimalQuantity;
await database.SaveChangesAsync(ct);
GetProductDto responseDto = new() await productsRepository.UpdateAsync(product, ct);
{
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

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

View File

@@ -2,10 +2,11 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.PurchaseOrder.Response; using PyroFetes.DTO.PurchaseOrder.Response;
using PyroFetes.DTO.PurchaseProduct.Response; using PyroFetes.DTO.PurchaseProduct.Response;
using PyroFetes.Repositories;
namespace PyroFetes.Endpoints.PurchaseOrders; namespace PyroFetes.Endpoints.PurchaseOrders;
public class GetAllPurchaseOrderEndpoint(PyroFetesDbContext database) : EndpointWithoutRequest<List<GetPurchaseOrderDto>> public class GetAllPurchaseOrderEndpoint(PurchaseOrdersRepository purchaseOrdersRepository) : EndpointWithoutRequest<List<GetPurchaseOrderDto>>
{ {
public override void Configure() public override void Configure()
{ {
@@ -15,32 +16,6 @@ public class GetAllPurchaseOrderEndpoint(PyroFetesDbContext database) : Endpoint
public override async Task HandleAsync(CancellationToken ct) public override async Task HandleAsync(CancellationToken ct)
{ {
List<GetPurchaseOrderDto> purchaseOrder = await database.PurchaseOrders await Send.OkAsync(await purchaseOrdersRepository.ProjectToListAsync<GetPurchaseOrderDto>(ct), ct);
.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);
} }
} }

View File

@@ -3,6 +3,8 @@ using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.PurchaseOrder.Response; using PyroFetes.DTO.PurchaseOrder.Response;
using PyroFetes.DTO.PurchaseProduct.Response; using PyroFetes.DTO.PurchaseProduct.Response;
using PyroFetes.Models; using PyroFetes.Models;
using PyroFetes.Repositories;
using PyroFetes.Specifications.PurchaseOrders;
namespace PyroFetes.Endpoints.PurchaseOrders; namespace PyroFetes.Endpoints.PurchaseOrders;
@@ -11,7 +13,9 @@ public class GetPurchaseOrderRequest
public int Id { get; set; } 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() public override void Configure()
{ {
@@ -21,8 +25,7 @@ public class GetPurchaseOrderEndpoint(PyroFetesDbContext database) : Endpoint<Ge
public override async Task HandleAsync(GetPurchaseOrderRequest req, CancellationToken ct) public override async Task HandleAsync(GetPurchaseOrderRequest req, CancellationToken ct)
{ {
PurchaseOrder? purchaseOrder = await database.PurchaseOrders PurchaseOrder? purchaseOrder = await purchaseOrdersRepository.FirstOrDefaultAsync(new GetPurchaseOrderByIdSpec(req.Id), ct);
.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
if (purchaseOrder == null) if (purchaseOrder == null)
{ {
@@ -30,29 +33,6 @@ public class GetPurchaseOrderEndpoint(PyroFetesDbContext database) : Endpoint<Ge
return; return;
} }
GetPurchaseOrderDto responseDto = new() await Send.OkAsync(mapper.Map<GetPurchaseOrderDto>(purchaseOrder), ct);
{
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);
} }
} }

View File

@@ -4,10 +4,14 @@ using PyroFetes.DTO.PurchaseOrder.Request;
using PyroFetes.DTO.PurchaseOrder.Response; using PyroFetes.DTO.PurchaseOrder.Response;
using PyroFetes.DTO.PurchaseProduct.Response; using PyroFetes.DTO.PurchaseProduct.Response;
using PyroFetes.Models; using PyroFetes.Models;
using PyroFetes.Repositories;
using PyroFetes.Specifications.PurchaseOrders;
namespace PyroFetes.Endpoints.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() public override void Configure()
{ {
@@ -17,7 +21,7 @@ public class PatchPurchaseOrderPurchaseConditionsEndpoint(PyroFetesDbContext dat
public override async Task HandleAsync(PatchPurchaseOrderPurchaseConditionsDto req, CancellationToken ct) 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) if (purchaseOrder == null)
{ {
await Send.NotFoundAsync(ct); await Send.NotFoundAsync(ct);
@@ -25,30 +29,8 @@ public class PatchPurchaseOrderPurchaseConditionsEndpoint(PyroFetesDbContext dat
} }
purchaseOrder.PurchaseConditions = req.PurchaseConditions; purchaseOrder.PurchaseConditions = req.PurchaseConditions;
await database.SaveChangesAsync(ct); await purchaseOrdersRepository.UpdateAsync(purchaseOrder, ct);
GetPurchaseOrderDto responseDto = new() await Send.OkAsync(mapper.Map<GetPurchaseOrderDto>(purchaseOrder), ct);
{
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);
} }
} }

View File

@@ -3,10 +3,17 @@ using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.PurchaseProduct.Request; using PyroFetes.DTO.PurchaseProduct.Request;
using PyroFetes.DTO.PurchaseProduct.Response; using PyroFetes.DTO.PurchaseProduct.Response;
using PyroFetes.Models; using PyroFetes.Models;
using PyroFetes.Repositories;
using PyroFetes.Specifications.Products;
using PyroFetes.Specifications.PurchaseOrders;
namespace PyroFetes.Endpoints.PurchaseProducts; 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() public override void Configure()
{ {
@@ -16,14 +23,14 @@ public class CreatePurchaseProductEndpoint(PyroFetesDbContext database) : Endpoi
public override async Task HandleAsync(CreatePurchaseProductDto req, CancellationToken ct) 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) if (product == null)
{ {
await Send.NotFoundAsync(ct); await Send.NotFoundAsync(ct);
return; 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) if (purchaseOrder == null)
{ {
@@ -31,8 +38,7 @@ public class CreatePurchaseProductEndpoint(PyroFetesDbContext database) : Endpoi
{ {
PurchaseConditions = req.PurchaseOrderPurchaseConditions ?? "Conditions non précisées" PurchaseConditions = req.PurchaseOrderPurchaseConditions ?? "Conditions non précisées"
}; };
database.PurchaseOrders.Add(purchaseOrder); await purchaseOrdersRepository.AddAsync(purchaseOrder, ct);
await database.SaveChangesAsync(ct);
} }
PurchaseProduct purchaseProduct = new PurchaseProduct() PurchaseProduct purchaseProduct = new PurchaseProduct()
@@ -41,28 +47,9 @@ public class CreatePurchaseProductEndpoint(PyroFetesDbContext database) : Endpoi
PurchaseOrderId = purchaseOrder.Id, PurchaseOrderId = purchaseOrder.Id,
Quantity = req.Quantity Quantity = req.Quantity
}; };
database.PurchaseProducts.Add(purchaseProduct);
await database.SaveChangesAsync(ct);
GetPurchaseProductDto responseDto = new GetPurchaseProductDto() await purchaseProductsRepository.AddAsync(purchaseProduct, ct);
{
ProductId = product.Id, await Send.OkAsync(mapper.Map<GetPurchaseProductDto>(purchaseProduct), ct);
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);
} }
} }

View File

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

View File

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