diff --git a/PyroFetes/Endpoints/QuotationProducts/CreateQuotationProductEndpoint.cs b/PyroFetes/Endpoints/QuotationProducts/CreateQuotationProductEndpoint.cs index 6e24781..0c64357 100644 --- a/PyroFetes/Endpoints/QuotationProducts/CreateQuotationProductEndpoint.cs +++ b/PyroFetes/Endpoints/QuotationProducts/CreateQuotationProductEndpoint.cs @@ -3,10 +3,17 @@ using Microsoft.EntityFrameworkCore; using PyroFetes.DTO.QuotationProduct.Request; using PyroFetes.DTO.QuotationProduct.Response; using PyroFetes.Models; +using PyroFetes.Repositories; +using PyroFetes.Specifications.Products; +using PyroFetes.Specifications.Quotations; namespace PyroFetes.Endpoints.QuotationProducts; -public class CreateQuotationProductEndpoint(PyroFetesDbContext database) : Endpoint +public class CreateQuotationProductEndpoint( + QuotationProductsRepository quotationProductsRepository, + ProductsRepository productsRepository, + QuotationsRepository quotationsRepository, + AutoMapper.IMapper mapper) : Endpoint { public override void Configure() { @@ -16,14 +23,15 @@ public class CreateQuotationProductEndpoint(PyroFetesDbContext database) : Endpo public override async Task HandleAsync(CreateQuotationProductDto 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; } - Quotation? quotation = await database.Quotations.FirstOrDefaultAsync(q => q.Id == req.QuotationId, ct); + Quotation? quotation = await quotationsRepository.FirstOrDefaultAsync(new GetQuotationByIdSpec(req.QuotationId), ct); if (quotation == null) { @@ -32,8 +40,8 @@ public class CreateQuotationProductEndpoint(PyroFetesDbContext database) : Endpo Message = req.QuotationMessage ?? "", ConditionsSale = req.QuotationConditionsSale, }; - database.Quotations.Add(quotation); - await database.SaveChangesAsync(ct); + + await quotationsRepository.AddAsync(quotation, ct); } QuotationProduct quotationProduct = new QuotationProduct() @@ -42,28 +50,9 @@ public class CreateQuotationProductEndpoint(PyroFetesDbContext database) : Endpo QuotationId = quotation.Id, Quantity = req.Quantity }; - database.QuotationProducts.Add(quotationProduct); - await database.SaveChangesAsync(ct); - GetQuotationProductDto responseDto = new GetQuotationProductDto() - { - 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, - Quantity = quotationProduct.Quantity, - QuotationMessage = quotation.Message, - QuotationConditionsSale = quotation.ConditionsSale, - QuotationId = quotation.Id, - }; + await quotationProductsRepository.AddAsync(quotationProduct, ct); - await Send.OkAsync(responseDto, ct); + await Send.OkAsync(mapper.Map(quotationProduct), ct); } } diff --git a/PyroFetes/Endpoints/QuotationProducts/DeleteQuotationProductEndpoint.cs b/PyroFetes/Endpoints/QuotationProducts/DeleteQuotationProductEndpoint.cs index 2ee2d1e..c680325 100644 --- a/PyroFetes/Endpoints/QuotationProducts/DeleteQuotationProductEndpoint.cs +++ b/PyroFetes/Endpoints/QuotationProducts/DeleteQuotationProductEndpoint.cs @@ -1,6 +1,8 @@ using FastEndpoints; using Microsoft.EntityFrameworkCore; using PyroFetes.Models; +using PyroFetes.Repositories; +using PyroFetes.Specifications.QuotationProducts; namespace PyroFetes.Endpoints.QuotationProducts; @@ -10,7 +12,7 @@ public class DeleteQuotationProductRequest public int QuotationId { get; set; } } -public class DeleteQuotationProductEndpoint(PyroFetesDbContext database) : Endpoint +public class DeleteQuotationProductEndpoint(QuotationProductsRepository quotationProductsRepository) : Endpoint { public override void Configure() { @@ -20,8 +22,9 @@ public class DeleteQuotationProductEndpoint(PyroFetesDbContext database) : Endpo public override async Task HandleAsync(DeleteQuotationProductRequest req, CancellationToken ct) { - QuotationProduct? quotationProduct = await database.QuotationProducts - .SingleOrDefaultAsync(qo => qo.ProductId == req.ProductId && qo.QuotationId == req.QuotationId, ct); + QuotationProduct? quotationProduct = + await quotationProductsRepository.FirstOrDefaultAsync( + new GetQuotationProductByProductIdAndQuotationIdSpec(req.ProductId, req.QuotationId), ct); if (quotationProduct == null) { @@ -29,8 +32,7 @@ public class DeleteQuotationProductEndpoint(PyroFetesDbContext database) : Endpo return; } - database.QuotationProducts.Remove(quotationProduct); - await database.SaveChangesAsync(ct); + await quotationProductsRepository.DeleteAsync(quotationProduct, ct); await Send.NoContentAsync(ct); } diff --git a/PyroFetes/Endpoints/QuotationProducts/PatchQuotationProductQuantityEndpoint.cs b/PyroFetes/Endpoints/QuotationProducts/PatchQuotationProductQuantityEndpoint.cs index 68034c9..26f3691 100644 --- a/PyroFetes/Endpoints/QuotationProducts/PatchQuotationProductQuantityEndpoint.cs +++ b/PyroFetes/Endpoints/QuotationProducts/PatchQuotationProductQuantityEndpoint.cs @@ -3,10 +3,14 @@ using Microsoft.EntityFrameworkCore; using PyroFetes.DTO.QuotationProduct.Request; using PyroFetes.DTO.QuotationProduct.Response; using PyroFetes.Models; +using PyroFetes.Repositories; +using PyroFetes.Specifications.QuotationProducts; namespace PyroFetes.Endpoints.QuotationProducts; -public class PatchQuotationProductQuantityEndpoint(PyroFetesDbContext database) : Endpoint +public class PatchQuotationProductQuantityEndpoint( + QuotationProductsRepository quotationProductsRepository, + AutoMapper.IMapper mapper) : Endpoint { public override void Configure() { @@ -16,7 +20,9 @@ public class PatchQuotationProductQuantityEndpoint(PyroFetesDbContext database) public override async Task HandleAsync(PatchQuotationProductQuantityDto req, CancellationToken ct) { - QuotationProduct? quotationProduct = await database.QuotationProducts.SingleOrDefaultAsync(qo => qo.ProductId == req.ProductId && qo.QuotationId == req.QuotationId, ct); + QuotationProduct? quotationProduct = + await quotationProductsRepository.FirstOrDefaultAsync( + new GetQuotationProductByProductIdAndQuotationIdSpec(req.ProductId, req.QuotationId), ct); if (quotationProduct == null) { await Send.NotFoundAsync(ct); @@ -24,14 +30,8 @@ public class PatchQuotationProductQuantityEndpoint(PyroFetesDbContext database) } quotationProduct.Quantity = req.Quantity; - await database.SaveChangesAsync(ct); - - GetQuotationProductDto responseDto = new() - { - ProductId = quotationProduct.ProductId, - QuotationId = quotationProduct.QuotationId, - Quantity = quotationProduct.Quantity - }; - await Send.OkAsync(responseDto, ct); + await quotationProductsRepository.UpdateAsync(quotationProduct, ct); + + await Send.OkAsync(mapper.Map(quotationProduct), ct); } } \ No newline at end of file diff --git a/PyroFetes/Repositories/QuotationProductsRepository.cs b/PyroFetes/Repositories/QuotationProductsRepository.cs new file mode 100644 index 0000000..0d42cdc --- /dev/null +++ b/PyroFetes/Repositories/QuotationProductsRepository.cs @@ -0,0 +1,5 @@ +using PyroFetes.Models; + +namespace PyroFetes.Repositories; + +public class QuotationProductsRepository(PyroFetesDbContext pyrofetesContext, AutoMapper.IMapper mapper) : PyrofetesRepository(pyrofetesContext, mapper); diff --git a/PyroFetes/Repositories/QuotationsRepository.cs b/PyroFetes/Repositories/QuotationsRepository.cs new file mode 100644 index 0000000..1b99ed9 --- /dev/null +++ b/PyroFetes/Repositories/QuotationsRepository.cs @@ -0,0 +1,5 @@ +using PyroFetes.Models; + +namespace PyroFetes.Repositories; + +public class QuotationsRepository(PyroFetesDbContext pyrofetesContext, AutoMapper.IMapper mapper) : PyrofetesRepository(pyrofetesContext, mapper); \ No newline at end of file diff --git a/PyroFetes/Specifications/QuotationProducts/GetQuotationProductByProductIdAndQuotationIdSpec.cs b/PyroFetes/Specifications/QuotationProducts/GetQuotationProductByProductIdAndQuotationIdSpec.cs new file mode 100644 index 0000000..9c3b65a --- /dev/null +++ b/PyroFetes/Specifications/QuotationProducts/GetQuotationProductByProductIdAndQuotationIdSpec.cs @@ -0,0 +1,13 @@ +using Ardalis.Specification; +using PyroFetes.Models; + +namespace PyroFetes.Specifications.QuotationProducts; + +public sealed class GetQuotationProductByProductIdAndQuotationIdSpec : Specification +{ + public GetQuotationProductByProductIdAndQuotationIdSpec(int productId, int quotationId) + { + Query + .Where(x => x.ProductId == productId && x.QuotationId == quotationId); + } +} \ No newline at end of file diff --git a/PyroFetes/Specifications/Quotations/GetQuotationByIdSpec.cs b/PyroFetes/Specifications/Quotations/GetQuotationByIdSpec.cs new file mode 100644 index 0000000..89ff725 --- /dev/null +++ b/PyroFetes/Specifications/Quotations/GetQuotationByIdSpec.cs @@ -0,0 +1,13 @@ +using Ardalis.Specification; +using PyroFetes.Models; + +namespace PyroFetes.Specifications.Quotations; + +public sealed class GetQuotationByIdSpec : Specification +{ + public GetQuotationByIdSpec(int quotationId) + { + Query + .Where(x => x.Id == quotationId); + } +} \ No newline at end of file