Refactored QuotationProduct
This commit is contained in:
@@ -3,10 +3,17 @@ using Microsoft.EntityFrameworkCore;
|
|||||||
using PyroFetes.DTO.QuotationProduct.Request;
|
using PyroFetes.DTO.QuotationProduct.Request;
|
||||||
using PyroFetes.DTO.QuotationProduct.Response;
|
using PyroFetes.DTO.QuotationProduct.Response;
|
||||||
using PyroFetes.Models;
|
using PyroFetes.Models;
|
||||||
|
using PyroFetes.Repositories;
|
||||||
|
using PyroFetes.Specifications.Products;
|
||||||
|
using PyroFetes.Specifications.Quotations;
|
||||||
|
|
||||||
namespace PyroFetes.Endpoints.QuotationProducts;
|
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()
|
public override void Configure()
|
||||||
{
|
{
|
||||||
@@ -16,14 +23,15 @@ public class CreateQuotationProductEndpoint(PyroFetesDbContext database) : Endpo
|
|||||||
|
|
||||||
public override async Task HandleAsync(CreateQuotationProductDto req, CancellationToken ct)
|
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)
|
if (product == null)
|
||||||
{
|
{
|
||||||
await Send.NotFoundAsync(ct);
|
await Send.NotFoundAsync(ct);
|
||||||
return;
|
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)
|
if (quotation == null)
|
||||||
{
|
{
|
||||||
@@ -32,8 +40,8 @@ public class CreateQuotationProductEndpoint(PyroFetesDbContext database) : Endpo
|
|||||||
Message = req.QuotationMessage ?? "",
|
Message = req.QuotationMessage ?? "",
|
||||||
ConditionsSale = req.QuotationConditionsSale,
|
ConditionsSale = req.QuotationConditionsSale,
|
||||||
};
|
};
|
||||||
database.Quotations.Add(quotation);
|
|
||||||
await database.SaveChangesAsync(ct);
|
await quotationsRepository.AddAsync(quotation, ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
QuotationProduct quotationProduct = new QuotationProduct()
|
QuotationProduct quotationProduct = new QuotationProduct()
|
||||||
@@ -42,28 +50,9 @@ public class CreateQuotationProductEndpoint(PyroFetesDbContext database) : Endpo
|
|||||||
QuotationId = quotation.Id,
|
QuotationId = quotation.Id,
|
||||||
Quantity = req.Quantity
|
Quantity = req.Quantity
|
||||||
};
|
};
|
||||||
database.QuotationProducts.Add(quotationProduct);
|
|
||||||
await database.SaveChangesAsync(ct);
|
|
||||||
|
|
||||||
GetQuotationProductDto responseDto = new GetQuotationProductDto()
|
await quotationProductsRepository.AddAsync(quotationProduct, ct);
|
||||||
{
|
|
||||||
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 Send.OkAsync(responseDto, ct);
|
await Send.OkAsync(mapper.Map<GetQuotationProductDto>(quotationProduct), ct);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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.QuotationProducts;
|
||||||
|
|
||||||
namespace PyroFetes.Endpoints.QuotationProducts;
|
namespace PyroFetes.Endpoints.QuotationProducts;
|
||||||
|
|
||||||
@@ -10,7 +12,7 @@ public class DeleteQuotationProductRequest
|
|||||||
public int QuotationId { get; set; }
|
public int QuotationId { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class DeleteQuotationProductEndpoint(PyroFetesDbContext database) : Endpoint<DeleteQuotationProductRequest>
|
public class DeleteQuotationProductEndpoint(QuotationProductsRepository quotationProductsRepository) : Endpoint<DeleteQuotationProductRequest>
|
||||||
{
|
{
|
||||||
public override void Configure()
|
public override void Configure()
|
||||||
{
|
{
|
||||||
@@ -20,8 +22,9 @@ public class DeleteQuotationProductEndpoint(PyroFetesDbContext database) : Endpo
|
|||||||
|
|
||||||
public override async Task HandleAsync(DeleteQuotationProductRequest req, CancellationToken ct)
|
public override async Task HandleAsync(DeleteQuotationProductRequest req, CancellationToken ct)
|
||||||
{
|
{
|
||||||
QuotationProduct? quotationProduct = await database.QuotationProducts
|
QuotationProduct? quotationProduct =
|
||||||
.SingleOrDefaultAsync(qo => qo.ProductId == req.ProductId && qo.QuotationId == req.QuotationId, ct);
|
await quotationProductsRepository.FirstOrDefaultAsync(
|
||||||
|
new GetQuotationProductByProductIdAndQuotationIdSpec(req.ProductId, req.QuotationId), ct);
|
||||||
|
|
||||||
if (quotationProduct == null)
|
if (quotationProduct == null)
|
||||||
{
|
{
|
||||||
@@ -29,8 +32,7 @@ public class DeleteQuotationProductEndpoint(PyroFetesDbContext database) : Endpo
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
database.QuotationProducts.Remove(quotationProduct);
|
await quotationProductsRepository.DeleteAsync(quotationProduct, ct);
|
||||||
await database.SaveChangesAsync(ct);
|
|
||||||
|
|
||||||
await Send.NoContentAsync(ct);
|
await Send.NoContentAsync(ct);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,10 +3,14 @@ using Microsoft.EntityFrameworkCore;
|
|||||||
using PyroFetes.DTO.QuotationProduct.Request;
|
using PyroFetes.DTO.QuotationProduct.Request;
|
||||||
using PyroFetes.DTO.QuotationProduct.Response;
|
using PyroFetes.DTO.QuotationProduct.Response;
|
||||||
using PyroFetes.Models;
|
using PyroFetes.Models;
|
||||||
|
using PyroFetes.Repositories;
|
||||||
|
using PyroFetes.Specifications.QuotationProducts;
|
||||||
|
|
||||||
namespace PyroFetes.Endpoints.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()
|
public override void Configure()
|
||||||
{
|
{
|
||||||
@@ -16,7 +20,9 @@ public class PatchQuotationProductQuantityEndpoint(PyroFetesDbContext database)
|
|||||||
|
|
||||||
public override async Task HandleAsync(PatchQuotationProductQuantityDto req, CancellationToken ct)
|
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)
|
if (quotationProduct == null)
|
||||||
{
|
{
|
||||||
await Send.NotFoundAsync(ct);
|
await Send.NotFoundAsync(ct);
|
||||||
@@ -24,14 +30,8 @@ public class PatchQuotationProductQuantityEndpoint(PyroFetesDbContext database)
|
|||||||
}
|
}
|
||||||
|
|
||||||
quotationProduct.Quantity = req.Quantity;
|
quotationProduct.Quantity = req.Quantity;
|
||||||
await database.SaveChangesAsync(ct);
|
await quotationProductsRepository.UpdateAsync(quotationProduct, ct);
|
||||||
|
|
||||||
GetQuotationProductDto responseDto = new()
|
await Send.OkAsync(mapper.Map<GetQuotationProductDto>(quotationProduct), ct);
|
||||||
{
|
|
||||||
ProductId = quotationProduct.ProductId,
|
|
||||||
QuotationId = quotationProduct.QuotationId,
|
|
||||||
Quantity = quotationProduct.Quantity
|
|
||||||
};
|
|
||||||
await Send.OkAsync(responseDto, ct);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
5
PyroFetes/Repositories/QuotationProductsRepository.cs
Normal file
5
PyroFetes/Repositories/QuotationProductsRepository.cs
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
using PyroFetes.Models;
|
||||||
|
|
||||||
|
namespace PyroFetes.Repositories;
|
||||||
|
|
||||||
|
public class QuotationProductsRepository(PyroFetesDbContext pyrofetesContext, AutoMapper.IMapper mapper) : PyrofetesRepository<QuotationProduct>(pyrofetesContext, mapper);
|
||||||
5
PyroFetes/Repositories/QuotationsRepository.cs
Normal file
5
PyroFetes/Repositories/QuotationsRepository.cs
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
using PyroFetes.Models;
|
||||||
|
|
||||||
|
namespace PyroFetes.Repositories;
|
||||||
|
|
||||||
|
public class QuotationsRepository(PyroFetesDbContext pyrofetesContext, AutoMapper.IMapper mapper) : PyrofetesRepository<Quotation>(pyrofetesContext, mapper);
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
13
PyroFetes/Specifications/Quotations/GetQuotationByIdSpec.cs
Normal file
13
PyroFetes/Specifications/Quotations/GetQuotationByIdSpec.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user