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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user