From efa7a0be6f3c84988046f6a276ab425717c56575 Mon Sep 17 00:00:00 2001 From: sanchezvem Date: Sat, 29 Nov 2025 21:58:20 +0100 Subject: [PATCH] added DeleteProductEndpoint.cs --- .../Products/DeleteProductEndpoint.cs | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 PyroFetes/Endpoints/Products/DeleteProductEndpoint.cs diff --git a/PyroFetes/Endpoints/Products/DeleteProductEndpoint.cs b/PyroFetes/Endpoints/Products/DeleteProductEndpoint.cs new file mode 100644 index 0000000..1e48163 --- /dev/null +++ b/PyroFetes/Endpoints/Products/DeleteProductEndpoint.cs @@ -0,0 +1,38 @@ +using FastEndpoints; +using PyroFetes.Endpoints.Deliverers; +using PyroFetes.Models; +using PyroFetes.Repositories; +using PyroFetes.Specifications.Deliverers; +using PyroFetes.Specifications.Products; + +namespace PyroFetes.Endpoints.Products; + +public class DeleteProductsRequest +{ + public int ProductId { get; set; } +} +public class DeleteProductEndpoint(ProductsRepository productsRepository) : Endpoint +{ + public override void Configure() + { + 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); + + if (product == null) + { + await Send.NotFoundAsync(ct); + return; + } + + await productsRepository.DeleteAsync(product, ct); + + await Send.OkAsync(ct); + } + +} \ No newline at end of file