Refactored Quotation

This commit is contained in:
Cristiano
2025-11-20 14:34:09 +01:00
parent f6383265ba
commit ee9b4675dd
5 changed files with 29 additions and 96 deletions

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.Quotations;
namespace PyroFetes.Endpoints.Quotations; namespace PyroFetes.Endpoints.Quotations;
@@ -9,7 +11,9 @@ public class DeleteQuotationRequest
public int Id { get; set; } public int Id { get; set; }
} }
public class DeleteQuotationEndpoint(PyroFetesDbContext database) : Endpoint<DeleteQuotationRequest> public class DeleteQuotationEndpoint(
QuotationsRepository quotationsRepository,
QuotationProductsRepository quotationProductsRepository) : Endpoint<DeleteQuotationRequest>
{ {
public override void Configure() public override void Configure()
{ {
@@ -19,9 +23,7 @@ public class DeleteQuotationEndpoint(PyroFetesDbContext database) : Endpoint<Del
public override async Task HandleAsync(DeleteQuotationRequest req, CancellationToken ct) public override async Task HandleAsync(DeleteQuotationRequest req, CancellationToken ct)
{ {
Quotation? quotation = await database.Quotations Quotation? quotation = await quotationsRepository.FirstOrDefaultAsync(new GetQuotationByIdSpec(req.Id), ct);
.Include(q => q.QuotationProducts)
.SingleOrDefaultAsync(q => q.Id == req.Id, ct);
if (quotation == null) if (quotation == null)
{ {
@@ -31,11 +33,10 @@ public class DeleteQuotationEndpoint(PyroFetesDbContext database) : Endpoint<Del
if (quotation.QuotationProducts != null && quotation.QuotationProducts.Any()) if (quotation.QuotationProducts != null && quotation.QuotationProducts.Any())
{ {
database.QuotationProducts.RemoveRange(quotation.QuotationProducts); await quotationProductsRepository.DeleteRangeAsync(quotation.QuotationProducts, ct);
} }
database.Quotations.Remove(quotation); await quotationsRepository.DeleteAsync(quotation, ct);
await database.SaveChangesAsync(ct);
await Send.NoContentAsync(ct); await Send.NoContentAsync(ct);
} }

View File

@@ -3,10 +3,11 @@ using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Quotation.Response; using PyroFetes.DTO.Quotation.Response;
using PyroFetes.DTO.QuotationProduct.Response; using PyroFetes.DTO.QuotationProduct.Response;
using PyroFetes.Models; using PyroFetes.Models;
using PyroFetes.Repositories;
namespace PyroFetes.Endpoints.Quotations; namespace PyroFetes.Endpoints.Quotations;
public class GetAllQuotationEndpoint(PyroFetesDbContext database) : EndpointWithoutRequest<List<GetQuotationDto>> public class GetAllQuotationEndpoint(QuotationsRepository quotationsRepository) : EndpointWithoutRequest<List<GetQuotationDto>>
{ {
public override void Configure() public override void Configure()
{ {
@@ -16,35 +17,6 @@ public class GetAllQuotationEndpoint(PyroFetesDbContext database) : EndpointWith
public override async Task HandleAsync(CancellationToken ct) public override async Task HandleAsync(CancellationToken ct)
{ {
List<GetQuotationDto> quotations = await database.Quotations await Send.OkAsync(await quotationsRepository.ProjectToListAsync<GetQuotationDto>(ct), ct);
.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);
} }
} }

View File

@@ -3,6 +3,8 @@ using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Quotation.Response; using PyroFetes.DTO.Quotation.Response;
using PyroFetes.DTO.QuotationProduct.Response; using PyroFetes.DTO.QuotationProduct.Response;
using PyroFetes.Models; using PyroFetes.Models;
using PyroFetes.Repositories;
using PyroFetes.Specifications.Quotations;
namespace PyroFetes.Endpoints.Quotations; namespace PyroFetes.Endpoints.Quotations;
@@ -11,7 +13,9 @@ public class GetQuotationRequest
public int Id { get; set; } public int Id { get; set; }
} }
public class GetQuotationEndpoint(PyroFetesDbContext database) : Endpoint<GetQuotationRequest, GetQuotationDto> public class GetQuotationEndpoint(
QuotationsRepository quotationsRepository,
AutoMapper.IMapper mapper) : Endpoint<GetQuotationRequest, GetQuotationDto>
{ {
public override void Configure() public override void Configure()
{ {
@@ -21,8 +25,7 @@ public class GetQuotationEndpoint(PyroFetesDbContext database) : Endpoint<GetQuo
public override async Task HandleAsync(GetQuotationRequest req, CancellationToken ct) public override async Task HandleAsync(GetQuotationRequest req, CancellationToken ct)
{ {
Quotation? quotation = await database.Quotations Quotation? quotation = await quotationsRepository.FirstOrDefaultAsync(new GetQuotationByIdSpec(req.Id), ct);
.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
if (quotation == null) if (quotation == null)
{ {
@@ -30,32 +33,6 @@ public class GetQuotationEndpoint(PyroFetesDbContext database) : Endpoint<GetQuo
return; return;
} }
GetQuotationDto responseDto = new() await Send.OkAsync(mapper.Map<GetQuotationDto>(quotation), ct);
{
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);
} }
} }

View File

@@ -4,10 +4,14 @@ using PyroFetes.DTO.Quotation.Request;
using PyroFetes.DTO.Quotation.Response; using PyroFetes.DTO.Quotation.Response;
using PyroFetes.DTO.QuotationProduct.Response; using PyroFetes.DTO.QuotationProduct.Response;
using PyroFetes.Models; using PyroFetes.Models;
using PyroFetes.Repositories;
using PyroFetes.Specifications.Quotations;
namespace PyroFetes.Endpoints.Quotations; namespace PyroFetes.Endpoints.Quotations;
public class PatchQuotationConditionsSaleEndpoint(PyroFetesDbContext database) : Endpoint<PatchQuotationConditionsSaleDto, GetQuotationDto> public class PatchQuotationConditionsSaleEndpoint(
QuotationsRepository quotationsRepository,
AutoMapper.IMapper mapper) : Endpoint<PatchQuotationConditionsSaleDto, GetQuotationDto>
{ {
public override void Configure() public override void Configure()
{ {
@@ -17,7 +21,8 @@ public class PatchQuotationConditionsSaleEndpoint(PyroFetesDbContext database) :
public override async Task HandleAsync(PatchQuotationConditionsSaleDto req, CancellationToken ct) 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) if (quotation == null)
{ {
await Send.NotFoundAsync(ct); await Send.NotFoundAsync(ct);
@@ -25,32 +30,9 @@ public class PatchQuotationConditionsSaleEndpoint(PyroFetesDbContext database) :
} }
quotation.ConditionsSale = req.ConditionsSale; quotation.ConditionsSale = req.ConditionsSale;
await database.SaveChangesAsync(ct); await quotationsRepository.UpdateAsync(quotation, ct);
GetQuotationDto responseDto = new()
{ await Send.OkAsync(mapper.Map<GetQuotationDto>(quotation), ct);
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);
} }
} }

View File

@@ -8,6 +8,7 @@ public sealed class GetQuotationByIdSpec : Specification<Quotation>
public GetQuotationByIdSpec(int quotationId) public GetQuotationByIdSpec(int quotationId)
{ {
Query Query
.Include(q => q.QuotationProducts)
.Where(x => x.Id == quotationId); .Where(x => x.Id == quotationId);
} }
} }