Advanced refactoring

This commit is contained in:
Cristiano
2025-11-20 14:04:13 +01:00
parent bd653c149c
commit 7bf0b5bfd1
16 changed files with 123 additions and 207 deletions

View File

@@ -2,10 +2,11 @@
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Product.Response;
using PyroFetes.Models;
using PyroFetes.Repositories;
namespace PyroFetes.Endpoints.Products;
public class GetAllProductsEndpoint(PyroFetesDbContext database) : EndpointWithoutRequest<List<GetProductDto>>
public class GetAllProductsEndpoint(ProductsRepository productsRepository) : EndpointWithoutRequest<List<GetProductDto>>
{
public override void Configure()
{
@@ -15,23 +16,6 @@ public class GetAllProductsEndpoint(PyroFetesDbContext database) : EndpointWitho
public override async Task HandleAsync(CancellationToken ct)
{
List<GetProductDto> product = await database.Products
.Select(product => new GetProductDto()
{
Id = product.Id,
References = product.Reference,
Name = product.Name,
Duration = product.Duration,
Caliber = product.Caliber,
ApprovalNumber = product.ApprovalNumber,
Weight = product.Weight,
Nec = product.Nec,
Image = product.Image,
Link = product.Link,
MinimalQuantity = product.MinimalQuantity,
})
.ToListAsync(ct);
await Send.OkAsync(product, ct);
await Send.OkAsync(await productsRepository.ProjectToListAsync<GetProductDto>(ct), ct);
}
}

View File

@@ -2,6 +2,8 @@
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Product.Response;
using PyroFetes.Models;
using PyroFetes.Repositories;
using PyroFetes.Specifications.Products;
namespace PyroFetes.Endpoints.Products;
@@ -10,7 +12,9 @@ public class GetProductRequest
public int Id { get; set; }
}
public class GetProductEndpoint(PyroFetesDbContext database) : Endpoint<GetProductRequest, GetProductDto>
public class GetProductEndpoint(
ProductsRepository productsRepository,
AutoMapper.IMapper mapper) : Endpoint<GetProductRequest, GetProductDto>
{
public override void Configure()
{
@@ -20,8 +24,7 @@ public class GetProductEndpoint(PyroFetesDbContext database) : Endpoint<GetProdu
public override async Task HandleAsync(GetProductRequest req, CancellationToken ct)
{
Product? product = await database.Products
.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
Product? product = await productsRepository.FirstOrDefaultAsync(new GetProductByIdSpec(req.Id), ct);
if (product == null)
{
@@ -29,21 +32,6 @@ public class GetProductEndpoint(PyroFetesDbContext database) : Endpoint<GetProdu
return;
}
GetProductDto responseDto = new()
{
Id = product.Id,
References = product.Reference,
Name = product.Name,
Duration = product.Duration,
Caliber = product.Caliber,
ApprovalNumber = product.ApprovalNumber,
Weight = product.Weight,
Nec = product.Nec,
Image = product.Image,
Link = product.Link,
MinimalQuantity = product.MinimalQuantity,
};
await Send.OkAsync(responseDto, ct);
await Send.OkAsync(mapper.Map<GetProductDto>(product), ct);
}
}

View File

@@ -3,11 +3,14 @@ using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Product.Request;
using PyroFetes.DTO.Product.Response;
using PyroFetes.Models;
using PyroFetes.Repositories;
using PyroFetes.Specifications.Products;
namespace PyroFetes.Endpoints.Products;
public class PatchProductMinimalStockEndpoint(PyroFetesDbContext database)
: Endpoint<PatchProductMinimalStockDto, GetProductDto>
public class PatchProductMinimalStockEndpoint(
ProductsRepository productsRepository,
AutoMapper.IMapper mapper) : Endpoint<PatchProductMinimalStockDto, GetProductDto>
{
public override void Configure()
{
@@ -17,7 +20,8 @@ public class PatchProductMinimalStockEndpoint(PyroFetesDbContext database)
public override async Task HandleAsync(PatchProductMinimalStockDto req, CancellationToken ct)
{
Product? product = await database.Products.SingleOrDefaultAsync(po => po.Id == req.Id, ct);
Product? product = await productsRepository.FirstOrDefaultAsync(new GetProductByIdSpec(req.Id), ct);
if (product == null)
{
await Send.NotFoundAsync(ct);
@@ -25,22 +29,8 @@ public class PatchProductMinimalStockEndpoint(PyroFetesDbContext database)
}
product.MinimalQuantity = req.MinimalQuantity;
await database.SaveChangesAsync(ct);
GetProductDto responseDto = new()
{
Id = product.Id,
References = product.Reference,
Name = product.Name,
Duration = product.Duration,
Caliber = product.Caliber,
ApprovalNumber = product.ApprovalNumber,
Weight = product.Weight,
Nec = product.Nec,
Image = product.Image,
Link = product.Link,
MinimalQuantity = product.MinimalQuantity,
};
await Send.OkAsync(responseDto, ct);
await productsRepository.UpdateAsync(product, ct);
await Send.OkAsync(mapper.Map<GetProductDto>(product), ct);
}
}

View File

@@ -3,10 +3,14 @@ using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Product.Request;
using PyroFetes.DTO.Product.Response;
using PyroFetes.Models;
using PyroFetes.Repositories;
using PyroFetes.Specifications.Products;
namespace PyroFetes.Endpoints.Products;
public class UpdateProductEndpoint(PyroFetesDbContext database) : Endpoint<UpdateProductDto, GetProductDto>
public class UpdateProductEndpoint(
ProductsRepository productsRepository,
AutoMapper.IMapper mapper) : Endpoint<UpdateProductDto, GetProductDto>
{
public override void Configure()
{
@@ -16,7 +20,7 @@ public class UpdateProductEndpoint(PyroFetesDbContext database) : Endpoint<Updat
public override async Task HandleAsync(UpdateProductDto req, CancellationToken ct)
{
Product? product = await database.Products.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
Product? product = await productsRepository.FirstOrDefaultAsync(new GetProductByIdSpec(req.Id), ct);
if (product == null)
{
@@ -34,23 +38,9 @@ public class UpdateProductEndpoint(PyroFetesDbContext database) : Endpoint<Updat
product.Image = req.Image;
product.Link = req.Link;
product.MinimalQuantity = req.MinimalQuantity;
await database.SaveChangesAsync(ct);
GetProductDto responseDto = new()
{
Id = product.Id,
References = product.Reference,
Name = product.Name,
Duration = product.Duration,
Caliber = product.Caliber,
ApprovalNumber = product.ApprovalNumber,
Weight = product.Weight,
Nec = product.Nec,
Image = product.Image,
Link = product.Link,
MinimalQuantity = product.MinimalQuantity,
};
await productsRepository.UpdateAsync(product, ct);
await Send.OkAsync(responseDto, ct);
await Send.OkAsync(mapper.Map<GetProductDto>(product), ct);
}
}