diff --git a/PyroFetes/Endpoints/Prices/DeletePriceEndpoint.cs b/PyroFetes/Endpoints/Prices/DeletePriceEndpoint.cs index 513e8d9..260c7cb 100644 --- a/PyroFetes/Endpoints/Prices/DeletePriceEndpoint.cs +++ b/PyroFetes/Endpoints/Prices/DeletePriceEndpoint.cs @@ -32,6 +32,6 @@ public class DeletePriceEndpoint(PricesRepository pricesRepository) : Endpoint> +public class GetAllProductsEndpoint(ProductsRepository productsRepository) : EndpointWithoutRequest> { public override void Configure() { @@ -15,23 +16,6 @@ public class GetAllProductsEndpoint(PyroFetesDbContext database) : EndpointWitho public override async Task HandleAsync(CancellationToken ct) { - List 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(ct), ct); } } \ No newline at end of file diff --git a/PyroFetes/Endpoints/Products/GetProductEndpoint.cs b/PyroFetes/Endpoints/Products/GetProductEndpoint.cs index d1c39f8..7e458a3 100644 --- a/PyroFetes/Endpoints/Products/GetProductEndpoint.cs +++ b/PyroFetes/Endpoints/Products/GetProductEndpoint.cs @@ -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 +public class GetProductEndpoint( + ProductsRepository productsRepository, + AutoMapper.IMapper mapper) : Endpoint { public override void Configure() { @@ -20,8 +24,7 @@ public class GetProductEndpoint(PyroFetesDbContext database) : Endpoint 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(product), ct); } } \ No newline at end of file diff --git a/PyroFetes/Endpoints/Products/PatchProductMinimalStockEndpoint.cs b/PyroFetes/Endpoints/Products/PatchProductMinimalStockEndpoint.cs index 69995ad..51bfd1d 100644 --- a/PyroFetes/Endpoints/Products/PatchProductMinimalStockEndpoint.cs +++ b/PyroFetes/Endpoints/Products/PatchProductMinimalStockEndpoint.cs @@ -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 +public class PatchProductMinimalStockEndpoint( + ProductsRepository productsRepository, + AutoMapper.IMapper mapper) : Endpoint { 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(product), ct); } } \ No newline at end of file diff --git a/PyroFetes/Endpoints/Products/UpdateProductEndpoint.cs b/PyroFetes/Endpoints/Products/UpdateProductEndpoint.cs index 8dc8172..83df9ac 100644 --- a/PyroFetes/Endpoints/Products/UpdateProductEndpoint.cs +++ b/PyroFetes/Endpoints/Products/UpdateProductEndpoint.cs @@ -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 +public class UpdateProductEndpoint( + ProductsRepository productsRepository, + AutoMapper.IMapper mapper) : Endpoint { public override void Configure() { @@ -16,7 +20,7 @@ public class UpdateProductEndpoint(PyroFetesDbContext database) : Endpoint 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(product), ct); } } \ No newline at end of file diff --git a/PyroFetes/Endpoints/PurchaseOrders/DeletePurchaseOrderEndpoint.cs b/PyroFetes/Endpoints/PurchaseOrders/DeletePurchaseOrderEndpoint.cs index a2d68a8..23098d8 100644 --- a/PyroFetes/Endpoints/PurchaseOrders/DeletePurchaseOrderEndpoint.cs +++ b/PyroFetes/Endpoints/PurchaseOrders/DeletePurchaseOrderEndpoint.cs @@ -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 +public class DeletePurchaseOrderEndpoint( + PurchaseOrdersRepository purchaseOrdersRepository, + PurchaseProductsRepository purchaseProductsRepository) : Endpoint { 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); } diff --git a/PyroFetes/Endpoints/PurchaseOrders/GetAllPurchaseOrderEndpoint.cs b/PyroFetes/Endpoints/PurchaseOrders/GetAllPurchaseOrderEndpoint.cs index 69c1fb1..f60fc95 100644 --- a/PyroFetes/Endpoints/PurchaseOrders/GetAllPurchaseOrderEndpoint.cs +++ b/PyroFetes/Endpoints/PurchaseOrders/GetAllPurchaseOrderEndpoint.cs @@ -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> +public class GetAllPurchaseOrderEndpoint(PurchaseOrdersRepository purchaseOrdersRepository) : EndpointWithoutRequest> { public override void Configure() { @@ -15,32 +16,6 @@ public class GetAllPurchaseOrderEndpoint(PyroFetesDbContext database) : Endpoint public override async Task HandleAsync(CancellationToken ct) { - List 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(ct), ct); } } \ No newline at end of file diff --git a/PyroFetes/Endpoints/PurchaseOrders/GetPurchaseOrderEndpoint.cs b/PyroFetes/Endpoints/PurchaseOrders/GetPurchaseOrderEndpoint.cs index 374ec4c..16823ed 100644 --- a/PyroFetes/Endpoints/PurchaseOrders/GetPurchaseOrderEndpoint.cs +++ b/PyroFetes/Endpoints/PurchaseOrders/GetPurchaseOrderEndpoint.cs @@ -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 +public class GetPurchaseOrderEndpoint( + PurchaseOrdersRepository purchaseOrdersRepository, + AutoMapper.IMapper mapper) : Endpoint { public override void Configure() { @@ -21,8 +25,7 @@ public class GetPurchaseOrderEndpoint(PyroFetesDbContext database) : Endpoint 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 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(purchaseOrder), ct); } } \ No newline at end of file diff --git a/PyroFetes/Endpoints/PurchaseOrders/PatchPurchaseOrderPurchaseConditionsEndpoint.cs b/PyroFetes/Endpoints/PurchaseOrders/PatchPurchaseOrderPurchaseConditionsEndpoint.cs index 04beca3..c422f4a 100644 --- a/PyroFetes/Endpoints/PurchaseOrders/PatchPurchaseOrderPurchaseConditionsEndpoint.cs +++ b/PyroFetes/Endpoints/PurchaseOrders/PatchPurchaseOrderPurchaseConditionsEndpoint.cs @@ -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 +public class PatchPurchaseOrderPurchaseConditionsEndpoint( + PurchaseOrdersRepository purchaseOrdersRepository, + AutoMapper.IMapper mapper) : Endpoint { 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(purchaseOrder), ct); } } \ No newline at end of file diff --git a/PyroFetes/Endpoints/PurchaseProducts/CreatePurchaseProductEndpoint.cs b/PyroFetes/Endpoints/PurchaseProducts/CreatePurchaseProductEndpoint.cs index 3aad50d..464f1f2 100644 --- a/PyroFetes/Endpoints/PurchaseProducts/CreatePurchaseProductEndpoint.cs +++ b/PyroFetes/Endpoints/PurchaseProducts/CreatePurchaseProductEndpoint.cs @@ -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 +public class CreatePurchaseProductEndpoint( + ProductsRepository productsRepository, + PurchaseOrdersRepository purchaseOrdersRepository, + PurchaseProductsRepository purchaseProductsRepository, + AutoMapper.IMapper mapper) : Endpoint { 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(purchaseProduct), ct); } } diff --git a/PyroFetes/Endpoints/PurchaseProducts/DeletePurchaseProductEndpoint.cs b/PyroFetes/Endpoints/PurchaseProducts/DeletePurchaseProductEndpoint.cs index 3a06268..811cade 100644 --- a/PyroFetes/Endpoints/PurchaseProducts/DeletePurchaseProductEndpoint.cs +++ b/PyroFetes/Endpoints/PurchaseProducts/DeletePurchaseProductEndpoint.cs @@ -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 +public class DeletePurchaseProductEndpoint(PurchaseProductsRepository purchaseProductsRepository) : Endpoint { 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); } diff --git a/PyroFetes/Endpoints/PurchaseProducts/PatchPurchaseProductQuantityEndpoint.cs b/PyroFetes/Endpoints/PurchaseProducts/PatchPurchaseProductQuantityEndpoint.cs index 1e72074..17a88c4 100644 --- a/PyroFetes/Endpoints/PurchaseProducts/PatchPurchaseProductQuantityEndpoint.cs +++ b/PyroFetes/Endpoints/PurchaseProducts/PatchPurchaseProductQuantityEndpoint.cs @@ -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 +public class PatchPurchaseProductQuantityEndpoint( + PurchaseProductsRepository purchaseProductsRepository, + AutoMapper.IMapper mapper) : Endpoint { 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(purchaseProduct), ct); } } \ No newline at end of file diff --git a/PyroFetes/Repositories/PurchaseOrdersRepository.cs b/PyroFetes/Repositories/PurchaseOrdersRepository.cs new file mode 100644 index 0000000..d5ce765 --- /dev/null +++ b/PyroFetes/Repositories/PurchaseOrdersRepository.cs @@ -0,0 +1,5 @@ +using PyroFetes.Models; + +namespace PyroFetes.Repositories; + +public class PurchaseOrdersRepository(PyroFetesDbContext pyrofetesContext, AutoMapper.IMapper mapper) : PyrofetesRepository(pyrofetesContext, mapper); \ No newline at end of file diff --git a/PyroFetes/Repositories/PurchaseProductsRepository.cs b/PyroFetes/Repositories/PurchaseProductsRepository.cs new file mode 100644 index 0000000..b5de25b --- /dev/null +++ b/PyroFetes/Repositories/PurchaseProductsRepository.cs @@ -0,0 +1,5 @@ +using PyroFetes.Models; + +namespace PyroFetes.Repositories; + +public class PurchaseProductsRepository(PyroFetesDbContext pyrofetesContext, AutoMapper.IMapper mapper) : PyrofetesRepository(pyrofetesContext, mapper); \ No newline at end of file diff --git a/PyroFetes/Specifications/PurchaseOrders/GetPurchaseOrderByIdSpec.cs b/PyroFetes/Specifications/PurchaseOrders/GetPurchaseOrderByIdSpec.cs new file mode 100644 index 0000000..272ad75 --- /dev/null +++ b/PyroFetes/Specifications/PurchaseOrders/GetPurchaseOrderByIdSpec.cs @@ -0,0 +1,14 @@ +using Ardalis.Specification; +using PyroFetes.Models; + +namespace PyroFetes.Specifications.PurchaseOrders; + +public sealed class GetPurchaseOrderByIdSpec : Specification +{ + public GetPurchaseOrderByIdSpec(int purchaseOrderId) + { + Query + .Include(po => po.PurchaseProducts) + .Where(po => po.Id == purchaseOrderId); + } +} \ No newline at end of file diff --git a/PyroFetes/Specifications/PurchaseProducts/GetPurchaseProductByProductIdAndPurchaseOrderIdSpec.cs b/PyroFetes/Specifications/PurchaseProducts/GetPurchaseProductByProductIdAndPurchaseOrderIdSpec.cs new file mode 100644 index 0000000..5d35c62 --- /dev/null +++ b/PyroFetes/Specifications/PurchaseProducts/GetPurchaseProductByProductIdAndPurchaseOrderIdSpec.cs @@ -0,0 +1,13 @@ +using Ardalis.Specification; +using PyroFetes.Models; + +namespace PyroFetes.Specifications.PurchaseProducts; + +public sealed class GetPurchaseProductByProductIdAndPurchaseOrderIdSpec : Specification +{ + public GetPurchaseProductByProductIdAndPurchaseOrderIdSpec(int productId, int purchaseOrderId) + { + Query + .Where(p => p.ProductId == productId && p.PurchaseOrderId == purchaseOrderId); + } +} \ No newline at end of file