Refactored WarehouseProduct

This commit is contained in:
Cristiano
2025-11-20 16:09:04 +01:00
parent 165c9b9322
commit 669938d677
6 changed files with 43 additions and 8 deletions

View File

@@ -2,6 +2,8 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.WareHouseProduct.Response; using PyroFetes.DTO.WareHouseProduct.Response;
using PyroFetes.Models; using PyroFetes.Models;
using PyroFetes.Repositories;
using PyroFetes.Specifications.WarehouseProducts;
namespace PyroFetes.Endpoints.WareHouseProducts; namespace PyroFetes.Endpoints.WareHouseProducts;
@@ -10,7 +12,8 @@ public class GetTotalQuantityRequest
public int ProductId { get; set; } public int ProductId { get; set; }
} }
public class GetTotalQuantityEndpoint(PyroFetesDbContext database) : Endpoint<GetTotalQuantityRequest, GetTotalQuantityDto> public class GetTotalQuantityEndpoint(
WarehouseProductsRepository warehouseProductsRepository) : Endpoint<GetTotalQuantityRequest, GetTotalQuantityDto>
{ {
public override void Configure() public override void Configure()
{ {
@@ -20,8 +23,7 @@ public class GetTotalQuantityEndpoint(PyroFetesDbContext database) : Endpoint<Ge
public override async Task HandleAsync(GetTotalQuantityRequest req, CancellationToken ct) public override async Task HandleAsync(GetTotalQuantityRequest req, CancellationToken ct)
{ {
bool exists = await database.WarehouseProducts bool exists = await warehouseProductsRepository.AnyAsync(new GetWarehouseProductByProductIdSpec(req.ProductId), ct);
.AnyAsync(wp => wp.ProductId == req.ProductId, ct);
if (!exists) if (!exists)
{ {
@@ -29,9 +31,9 @@ public class GetTotalQuantityEndpoint(PyroFetesDbContext database) : Endpoint<Ge
return; return;
} }
int totalQuantity = await database.WarehouseProducts int totalQuantity =
.Where(wp => wp.ProductId == req.ProductId) await warehouseProductsRepository.SumAsync(new GetProductTotalQuantitySpec(req.ProductId),
.SumAsync(wp => wp.Quantity, ct); wp => wp.Quantity, ct);
GetTotalQuantityDto responseDto = new() GetTotalQuantityDto responseDto = new()
{ {

View File

@@ -31,6 +31,7 @@ builder.Services.AddScoped<QuotationsRepository>();
builder.Services.AddScoped<SuppliersRepository>(); builder.Services.AddScoped<SuppliersRepository>();
builder.Services.AddScoped<SettingsRepository>(); builder.Services.AddScoped<SettingsRepository>();
builder.Services.AddScoped<UsersRepository>(); builder.Services.AddScoped<UsersRepository>();
builder.Services.AddScoped<WarehouseProductsRepository>();
MapperConfiguration mappingConfig = new(mc => MapperConfiguration mappingConfig = new(mc =>
{ {

View File

@@ -59,9 +59,9 @@ public class PyrofetesRepository<T>(DbContext databaseContext, AutoMapper.IMappe
/// <param name="selector"></param> /// <param name="selector"></param>
/// <param name="cancellationToken"></param> /// <param name="cancellationToken"></param>
/// <returns></returns> /// <returns></returns>
public async Task<decimal> SumAsync( public async Task<int> SumAsync(
ISpecification<T> specification, ISpecification<T> specification,
Expression<Func<T, decimal>> selector, Expression<Func<T, int>> selector,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
return await ApplySpecification(specification).SumAsync(selector, cancellationToken); return await ApplySpecification(specification).SumAsync(selector, cancellationToken);

View File

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

View File

@@ -0,0 +1,14 @@
using Ardalis.Specification;
using PyroFetes.Models;
namespace PyroFetes.Specifications.WarehouseProducts;
public sealed class GetProductTotalQuantitySpec : Specification<WarehouseProduct>
{
public GetProductTotalQuantitySpec(int productId)
{
Query
.Where(wp => wp.ProductId == productId);
}
}

View File

@@ -0,0 +1,13 @@
using Ardalis.Specification;
using PyroFetes.Models;
namespace PyroFetes.Specifications.WarehouseProducts;
public sealed class GetWarehouseProductByProductIdSpec : Specification<WarehouseProduct>
{
public GetWarehouseProductByProductIdSpec(int productId)
{
Query
.Where(x => x.ProductId == productId);
}
}