Refactored Quotation
This commit is contained in:
@@ -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<DeleteQuotationRequest>
|
||||
public class DeleteQuotationEndpoint(
|
||||
QuotationsRepository quotationsRepository,
|
||||
QuotationProductsRepository quotationProductsRepository) : Endpoint<DeleteQuotationRequest>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
@@ -19,9 +23,7 @@ public class DeleteQuotationEndpoint(PyroFetesDbContext database) : Endpoint<Del
|
||||
|
||||
public override async Task HandleAsync(DeleteQuotationRequest req, CancellationToken ct)
|
||||
{
|
||||
Quotation? quotation = await database.Quotations
|
||||
.Include(q => 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<Del
|
||||
|
||||
if (quotation.QuotationProducts != null && quotation.QuotationProducts.Any())
|
||||
{
|
||||
database.QuotationProducts.RemoveRange(quotation.QuotationProducts);
|
||||
await quotationProductsRepository.DeleteRangeAsync(quotation.QuotationProducts, ct);
|
||||
}
|
||||
|
||||
database.Quotations.Remove(quotation);
|
||||
await database.SaveChangesAsync(ct);
|
||||
await quotationsRepository.DeleteAsync(quotation, ct);
|
||||
|
||||
await Send.NoContentAsync(ct);
|
||||
}
|
||||
|
||||
@@ -3,10 +3,11 @@ using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes.DTO.Quotation.Response;
|
||||
using PyroFetes.DTO.QuotationProduct.Response;
|
||||
using PyroFetes.Models;
|
||||
using PyroFetes.Repositories;
|
||||
|
||||
namespace PyroFetes.Endpoints.Quotations;
|
||||
|
||||
public class GetAllQuotationEndpoint(PyroFetesDbContext database) : EndpointWithoutRequest<List<GetQuotationDto>>
|
||||
public class GetAllQuotationEndpoint(QuotationsRepository quotationsRepository) : EndpointWithoutRequest<List<GetQuotationDto>>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
@@ -16,35 +17,6 @@ public class GetAllQuotationEndpoint(PyroFetesDbContext database) : EndpointWith
|
||||
|
||||
public override async Task HandleAsync(CancellationToken ct)
|
||||
{
|
||||
List<GetQuotationDto> 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<GetQuotationDto>(ct), ct);
|
||||
}
|
||||
}
|
||||
@@ -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<GetQuotationRequest, GetQuotationDto>
|
||||
public class GetQuotationEndpoint(
|
||||
QuotationsRepository quotationsRepository,
|
||||
AutoMapper.IMapper mapper) : Endpoint<GetQuotationRequest, GetQuotationDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
@@ -21,8 +25,7 @@ public class GetQuotationEndpoint(PyroFetesDbContext database) : Endpoint<GetQuo
|
||||
|
||||
public override async Task HandleAsync(GetQuotationRequest 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)
|
||||
{
|
||||
@@ -30,32 +33,6 @@ public class GetQuotationEndpoint(PyroFetesDbContext database) : Endpoint<GetQuo
|
||||
return;
|
||||
}
|
||||
|
||||
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<GetQuotationDto>(quotation), ct);
|
||||
}
|
||||
}
|
||||
@@ -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<PatchQuotationConditionsSaleDto, GetQuotationDto>
|
||||
public class PatchQuotationConditionsSaleEndpoint(
|
||||
QuotationsRepository quotationsRepository,
|
||||
AutoMapper.IMapper mapper) : Endpoint<PatchQuotationConditionsSaleDto, GetQuotationDto>
|
||||
{
|
||||
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<GetQuotationDto>(quotation), ct);
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ public sealed class GetQuotationByIdSpec : Specification<Quotation>
|
||||
public GetQuotationByIdSpec(int quotationId)
|
||||
{
|
||||
Query
|
||||
.Include(q => q.QuotationProducts)
|
||||
.Where(x => x.Id == quotationId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user