Refactored QuotationProduct

This commit is contained in:
Cristiano
2025-11-20 14:20:25 +01:00
parent 7bf0b5bfd1
commit f6383265ba
7 changed files with 69 additions and 42 deletions

View File

@@ -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<CreateQuotationProductDto, GetQuotationProductDto>
public class CreateQuotationProductEndpoint(
QuotationProductsRepository quotationProductsRepository,
ProductsRepository productsRepository,
QuotationsRepository quotationsRepository,
AutoMapper.IMapper mapper) : Endpoint<CreateQuotationProductDto, GetQuotationProductDto>
{
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<GetQuotationProductDto>(quotationProduct), ct);
}
}

View File

@@ -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<DeleteQuotationProductRequest>
public class DeleteQuotationProductEndpoint(QuotationProductsRepository quotationProductsRepository) : Endpoint<DeleteQuotationProductRequest>
{
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);
}

View File

@@ -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<PatchQuotationProductQuantityDto, GetQuotationProductDto>
public class PatchQuotationProductQuantityEndpoint(
QuotationProductsRepository quotationProductsRepository,
AutoMapper.IMapper mapper) : Endpoint<PatchQuotationProductQuantityDto, GetQuotationProductDto>
{
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);
await quotationProductsRepository.UpdateAsync(quotationProduct, ct);
GetQuotationProductDto responseDto = new()
{
ProductId = quotationProduct.ProductId,
QuotationId = quotationProduct.QuotationId,
Quantity = quotationProduct.Quantity
};
await Send.OkAsync(responseDto, ct);
await Send.OkAsync(mapper.Map<GetQuotationProductDto>(quotationProduct), ct);
}
}

View File

@@ -0,0 +1,5 @@
using PyroFetes.Models;
namespace PyroFetes.Repositories;
public class QuotationProductsRepository(PyroFetesDbContext pyrofetesContext, AutoMapper.IMapper mapper) : PyrofetesRepository<QuotationProduct>(pyrofetesContext, mapper);

View File

@@ -0,0 +1,5 @@
using PyroFetes.Models;
namespace PyroFetes.Repositories;
public class QuotationsRepository(PyroFetesDbContext pyrofetesContext, AutoMapper.IMapper mapper) : PyrofetesRepository<Quotation>(pyrofetesContext, mapper);

View File

@@ -0,0 +1,13 @@
using Ardalis.Specification;
using PyroFetes.Models;
namespace PyroFetes.Specifications.QuotationProducts;
public sealed class GetQuotationProductByProductIdAndQuotationIdSpec : Specification<QuotationProduct>
{
public GetQuotationProductByProductIdAndQuotationIdSpec(int productId, int quotationId)
{
Query
.Where(x => x.ProductId == productId && x.QuotationId == quotationId);
}
}

View File

@@ -0,0 +1,13 @@
using Ardalis.Specification;
using PyroFetes.Models;
namespace PyroFetes.Specifications.Quotations;
public sealed class GetQuotationByIdSpec : Specification<Quotation>
{
public GetQuotationByIdSpec(int quotationId)
{
Query
.Where(x => x.Id == quotationId);
}
}