diff --git a/PyroFetes/Endpoints/Quotations/DeleteQuotationEndpoint.cs b/PyroFetes/Endpoints/Quotations/DeleteQuotationEndpoint.cs index dbb0bfe..06ef33c 100644 --- a/PyroFetes/Endpoints/Quotations/DeleteQuotationEndpoint.cs +++ b/PyroFetes/Endpoints/Quotations/DeleteQuotationEndpoint.cs @@ -1,6 +1,8 @@ using FastEndpoints; using Microsoft.EntityFrameworkCore; using PyroFetes.Models; +using PyroFetes.Repositories; +using PyroFetes.Specifications.Quotations; namespace PyroFetes.Endpoints.Quotations; @@ -9,7 +11,9 @@ public class DeleteQuotationRequest public int Id { get; set; } } -public class DeleteQuotationEndpoint(PyroFetesDbContext database) : Endpoint +public class DeleteQuotationEndpoint( + QuotationsRepository quotationsRepository, + QuotationProductsRepository quotationProductsRepository) : Endpoint { public override void Configure() { @@ -19,9 +23,7 @@ public class DeleteQuotationEndpoint(PyroFetesDbContext database) : Endpoint q.QuotationProducts) - .SingleOrDefaultAsync(q => q.Id == req.Id, ct); + Quotation? quotation = await quotationsRepository.FirstOrDefaultAsync(new GetQuotationByIdSpec(req.Id), ct); if (quotation == null) { @@ -31,11 +33,10 @@ public class DeleteQuotationEndpoint(PyroFetesDbContext database) : Endpoint> +public class GetAllQuotationEndpoint(QuotationsRepository quotationsRepository) : EndpointWithoutRequest> { public override void Configure() { @@ -16,35 +17,6 @@ public class GetAllQuotationEndpoint(PyroFetesDbContext database) : EndpointWith public override async Task HandleAsync(CancellationToken ct) { - List quotations = await database.Quotations - .Include(q => q.QuotationProducts!) - .ThenInclude(qp => qp.Product) - .Select(q => new GetQuotationDto - { - Id = q.Id, - Message = q.Message, - ConditionsSale = q.ConditionsSale, - GetQuotationProductDto = q.QuotationProducts.Select(qp => new GetQuotationProductDto - { - Quantity = qp.Quantity, - QuotationId = q.Id, - QuotationMessage = q.Message, - QuotationConditionsSale = q.ConditionsSale, - ProductId = qp.ProductId, - ProductReferences = qp.Product.Reference, - ProductName = qp.Product.Name, - ProductDuration = qp.Product.Duration, - ProductCaliber = qp.Product.Caliber, - ProductApprovalNumber = qp.Product.ApprovalNumber, - ProductWeight = qp.Product.Weight, - ProductNec = qp.Product.Nec, - ProductImage = qp.Product.Image, - ProductLink = qp.Product.Link, - ProductMinimalQuantity = qp.Product.MinimalQuantity, - }).ToList() - }) - .ToListAsync(ct); - - await Send.OkAsync(quotations, ct); + await Send.OkAsync(await quotationsRepository.ProjectToListAsync(ct), ct); } } \ No newline at end of file diff --git a/PyroFetes/Endpoints/Quotations/GetQuotationEndpoint.cs b/PyroFetes/Endpoints/Quotations/GetQuotationEndpoint.cs index d600e0f..6c2355b 100644 --- a/PyroFetes/Endpoints/Quotations/GetQuotationEndpoint.cs +++ b/PyroFetes/Endpoints/Quotations/GetQuotationEndpoint.cs @@ -3,6 +3,8 @@ using Microsoft.EntityFrameworkCore; using PyroFetes.DTO.Quotation.Response; using PyroFetes.DTO.QuotationProduct.Response; using PyroFetes.Models; +using PyroFetes.Repositories; +using PyroFetes.Specifications.Quotations; namespace PyroFetes.Endpoints.Quotations; @@ -11,7 +13,9 @@ public class GetQuotationRequest public int Id { get; set; } } -public class GetQuotationEndpoint(PyroFetesDbContext database) : Endpoint +public class GetQuotationEndpoint( + QuotationsRepository quotationsRepository, + AutoMapper.IMapper mapper) : Endpoint { public override void Configure() { @@ -21,8 +25,7 @@ public class GetQuotationEndpoint(PyroFetesDbContext database) : Endpoint x.Id == req.Id, ct); + Quotation? quotation = await quotationsRepository.FirstOrDefaultAsync(new GetQuotationByIdSpec(req.Id), ct); if (quotation == null) { @@ -30,32 +33,6 @@ public class GetQuotationEndpoint(PyroFetesDbContext database) : Endpoint new GetQuotationProductDto - { - Quantity = qp.Quantity, - QuotationId = quotation.Id, - QuotationMessage = quotation.Message, - QuotationConditionsSale = quotation.ConditionsSale, - ProductId = qp.ProductId, - ProductReferences = qp.Product.Reference, - ProductName = qp.Product.Name, - ProductDuration = qp.Product.Duration, - ProductCaliber = qp.Product.Caliber, - ProductApprovalNumber = qp.Product.ApprovalNumber, - ProductWeight = qp.Product.Weight, - ProductNec = qp.Product.Nec, - ProductImage = qp.Product.Image, - ProductLink = qp.Product.Link, - ProductMinimalQuantity = qp.Product.MinimalQuantity, - }).ToList() - }; - - await Send.OkAsync(responseDto, ct); + await Send.OkAsync(mapper.Map(quotation), ct); } } \ No newline at end of file diff --git a/PyroFetes/Endpoints/Quotations/PatchQuotationConditionsSaleEndpoint.cs b/PyroFetes/Endpoints/Quotations/PatchQuotationConditionsSaleEndpoint.cs index 1462f7e..c68d1e9 100644 --- a/PyroFetes/Endpoints/Quotations/PatchQuotationConditionsSaleEndpoint.cs +++ b/PyroFetes/Endpoints/Quotations/PatchQuotationConditionsSaleEndpoint.cs @@ -4,10 +4,14 @@ using PyroFetes.DTO.Quotation.Request; using PyroFetes.DTO.Quotation.Response; using PyroFetes.DTO.QuotationProduct.Response; using PyroFetes.Models; +using PyroFetes.Repositories; +using PyroFetes.Specifications.Quotations; namespace PyroFetes.Endpoints.Quotations; -public class PatchQuotationConditionsSaleEndpoint(PyroFetesDbContext database) : Endpoint +public class PatchQuotationConditionsSaleEndpoint( + QuotationsRepository quotationsRepository, + AutoMapper.IMapper mapper) : Endpoint { public override void Configure() { @@ -17,7 +21,8 @@ public class PatchQuotationConditionsSaleEndpoint(PyroFetesDbContext database) : public override async Task HandleAsync(PatchQuotationConditionsSaleDto req, CancellationToken ct) { - Quotation? quotation = await database.Quotations.SingleOrDefaultAsync(x => x.Id == req.Id, ct); + Quotation? quotation = await quotationsRepository.FirstOrDefaultAsync(new GetQuotationByIdSpec(req.Id), ct); + if (quotation == null) { await Send.NotFoundAsync(ct); @@ -25,32 +30,9 @@ public class PatchQuotationConditionsSaleEndpoint(PyroFetesDbContext database) : } quotation.ConditionsSale = req.ConditionsSale; - await database.SaveChangesAsync(ct); + await quotationsRepository.UpdateAsync(quotation, ct); - GetQuotationDto responseDto = new() - { - Id = quotation.Id, - Message = quotation.Message, - ConditionsSale = quotation.ConditionsSale, - GetQuotationProductDto = quotation.QuotationProducts.Select(qp => new GetQuotationProductDto - { - Quantity = qp.Quantity, - QuotationId = quotation.Id, - QuotationMessage = quotation.Message, - QuotationConditionsSale = quotation.ConditionsSale, - ProductId = qp.ProductId, - ProductReferences = qp.Product.Reference, - ProductName = qp.Product.Name, - ProductDuration = qp.Product.Duration, - ProductCaliber = qp.Product.Caliber, - ProductApprovalNumber = qp.Product.ApprovalNumber, - ProductWeight = qp.Product.Weight, - ProductNec = qp.Product.Nec, - ProductImage = qp.Product.Image, - ProductLink = qp.Product.Link, - ProductMinimalQuantity = qp.Product.MinimalQuantity, - }).ToList() - }; - await Send.OkAsync(responseDto, ct); + + await Send.OkAsync(mapper.Map(quotation), ct); } } \ No newline at end of file diff --git a/PyroFetes/Specifications/Quotations/GetQuotationByIdSpec.cs b/PyroFetes/Specifications/Quotations/GetQuotationByIdSpec.cs index 89ff725..37424e9 100644 --- a/PyroFetes/Specifications/Quotations/GetQuotationByIdSpec.cs +++ b/PyroFetes/Specifications/Quotations/GetQuotationByIdSpec.cs @@ -8,6 +8,7 @@ public sealed class GetQuotationByIdSpec : Specification public GetQuotationByIdSpec(int quotationId) { Query + .Include(q => q.QuotationProducts) .Where(x => x.Id == quotationId); } } \ No newline at end of file