forked from sanchezvem/pyrofetes-backend
Refactor all code
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
using FastEndpoints;
|
||||
using PyroFetes.Endpoints.Deliverers;
|
||||
using PyroFetes.Models;
|
||||
using PyroFetes.Repositories;
|
||||
using PyroFetes.Specifications.Deliverers;
|
||||
using PyroFetes.Specifications.Products;
|
||||
|
||||
namespace PyroFetes.Endpoints.Products;
|
||||
@@ -11,28 +9,26 @@ public class DeleteProductsRequest
|
||||
{
|
||||
public int ProductId { get; set; }
|
||||
}
|
||||
|
||||
public class DeleteProductEndpoint(ProductsRepository productsRepository) : Endpoint<DeleteProductsRequest>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Delete("/products/{@id}", x=>new {x.ProductId});
|
||||
Delete("/products/{@Id}", x => new { x.ProductId });
|
||||
AllowAnonymous();
|
||||
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(DeleteProductsRequest req, CancellationToken ct)
|
||||
{
|
||||
Product? product = await productsRepository.FirstOrDefaultAsync(new GetProductByIdSpec(req.ProductId), ct);
|
||||
Product? product = await productsRepository.SingleOrDefaultAsync(new GetProductByIdSpec(req.ProductId), ct);
|
||||
|
||||
if (product == null)
|
||||
if (product is null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
await productsRepository.DeleteAsync(product, ct);
|
||||
|
||||
await productsRepository.DeleteAsync(product, ct);
|
||||
await Send.OkAsync(ct);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes.DTO.Product.Response;
|
||||
using PyroFetes.Models;
|
||||
using PyroFetes.Repositories;
|
||||
|
||||
namespace PyroFetes.Endpoints.Products;
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
using AutoMapper;
|
||||
using FastEndpoints;
|
||||
using PyroFetes.DTO.Product.Response;
|
||||
using PyroFetes.Repositories;
|
||||
@@ -15,7 +14,7 @@ public class GetAllProductsUnderLimitEndpoint(ProductsRepository productsReposit
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CancellationToken ct)
|
||||
{
|
||||
{
|
||||
await Send.OkAsync(await productsRepository.ProjectToListAsync<GetProductDto>(new GetProductsUnderLimitSpec(), ct), ct);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes.DTO.Product.Response;
|
||||
using PyroFetes.Models;
|
||||
using PyroFetes.Repositories;
|
||||
@@ -18,7 +17,7 @@ public class GetProductEndpoint(
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/products/{@Id}", x => new {x.Id});
|
||||
Get("/products/{@Id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
@@ -26,12 +25,12 @@ public class GetProductEndpoint(
|
||||
{
|
||||
Product? product = await productsRepository.FirstOrDefaultAsync(new GetProductByIdSpec(req.Id), ct);
|
||||
|
||||
if (product == null)
|
||||
if (product is null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
await Send.OkAsync(mapper.Map<GetProductDto>(product), ct);
|
||||
|
||||
await Send.OkAsync(mapper.Map<GetProductDto>(product), ct);
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,12 @@
|
||||
using FastEndpoints;
|
||||
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(
|
||||
ProductsRepository productsRepository,
|
||||
AutoMapper.IMapper mapper) : Endpoint<PatchProductMinimalStockDto, GetProductDto>
|
||||
public class PatchProductMinimalStockEndpoint(ProductsRepository productsRepository, AutoMapper.IMapper mapper) : Endpoint<PatchProductMinimalStockDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
@@ -20,17 +16,17 @@ public class PatchProductMinimalStockEndpoint(
|
||||
|
||||
public override async Task HandleAsync(PatchProductMinimalStockDto req, CancellationToken ct)
|
||||
{
|
||||
Product? product = await productsRepository.FirstOrDefaultAsync(new GetProductByIdSpec(req.Id), ct);
|
||||
|
||||
if (product == null)
|
||||
Product? product = await productsRepository.SingleOrDefaultAsync(new GetProductByIdSpec(req.Id), ct);
|
||||
|
||||
if (product is null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
product.MinimalQuantity = req.MinimalQuantity;
|
||||
mapper.Map(req, product);
|
||||
|
||||
await productsRepository.UpdateAsync(product, ct);
|
||||
|
||||
await Send.OkAsync(mapper.Map<GetProductDto>(product), ct);
|
||||
await Send.NoContentAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -1,46 +1,32 @@
|
||||
using FastEndpoints;
|
||||
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(
|
||||
ProductsRepository productsRepository,
|
||||
AutoMapper.IMapper mapper) : Endpoint<UpdateProductDto, GetProductDto>
|
||||
public class UpdateProductEndpoint(ProductsRepository productsRepository, AutoMapper.IMapper mapper) : Endpoint<UpdateProductDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Put("/products/{@Id}", x => new {x.Id});
|
||||
Put("/products/{@Id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(UpdateProductDto req, CancellationToken ct)
|
||||
{
|
||||
Product? product = await productsRepository.FirstOrDefaultAsync(new GetProductByIdSpec(req.Id), ct);
|
||||
|
||||
if (product == null)
|
||||
Product? product = await productsRepository.SingleOrDefaultAsync(new GetProductByIdSpec(req.Id), ct);
|
||||
|
||||
if (product is null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
product.Reference = req.References;
|
||||
product.Name = req.Name;
|
||||
product.Duration = req.Duration;
|
||||
product.Caliber = req.Caliber;
|
||||
product.ApprovalNumber = req.ApprovalNumber;
|
||||
product.Weight = req.Weight;
|
||||
product.Nec = req.Nec;
|
||||
product.Image = req.Image;
|
||||
product.Link = req.Link;
|
||||
product.MinimalQuantity = req.MinimalQuantity;
|
||||
|
||||
mapper.Map(req, product);
|
||||
|
||||
await productsRepository.UpdateAsync(product, ct);
|
||||
|
||||
await Send.OkAsync(mapper.Map<GetProductDto>(product), ct);
|
||||
await Send.NoContentAsync(ct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user