diff --git a/PyroFetes/Endpoints/PurchaseProduct/CreatePurchaseProductEndpoint.cs b/PyroFetes/Endpoints/PurchaseProduct/CreatePurchaseProductEndpoint.cs index 0578342..f060c3a 100644 --- a/PyroFetes/Endpoints/PurchaseProduct/CreatePurchaseProductEndpoint.cs +++ b/PyroFetes/Endpoints/PurchaseProduct/CreatePurchaseProductEndpoint.cs @@ -5,8 +5,7 @@ using PyroFetes.DTO.PurchaseProduct.Response; namespace PyroFetes.Endpoints.PurchaseProduct; -public class CreatePurchaseProductEndpoint(PyroFetesDbContext database) - : Endpoint +public class CreatePurchaseProductEndpoint(PyroFetesDbContext database) : Endpoint { public override void Configure() { diff --git a/PyroFetes/Endpoints/PurchaseProduct/DeletePurchaseProductEndpoint.cs b/PyroFetes/Endpoints/PurchaseProduct/DeletePurchaseProductEndpoint.cs index 1f532ff..7af1030 100644 --- a/PyroFetes/Endpoints/PurchaseProduct/DeletePurchaseProductEndpoint.cs +++ b/PyroFetes/Endpoints/PurchaseProduct/DeletePurchaseProductEndpoint.cs @@ -1,6 +1,41 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; + namespace PyroFetes.Endpoints.PurchaseProduct; -public class DeletePurchaseProductEndpoint +public class DeletePurchaseOrderRequest { - + public int Id { get; set; } +} + +public class DeletePurchaseOrderEndpoint(PyroFetesDbContext database) : Endpoint +{ + public override void Configure() + { + Delete("/api/purchaseOrders/{Id:int}"); + AllowAnonymous(); + } + + public override async Task HandleAsync(DeletePurchaseOrderRequest req, CancellationToken ct) + { + var purchaseOrder = await database.PurchaseOrders + .Include(po => po.PurchaseProducts) + .SingleOrDefaultAsync(po => po.Id == req.Id, ct); + + if (purchaseOrder == null) + { + await Send.NotFoundAsync(ct); + return; + } + + if (purchaseOrder.PurchaseProducts != null && purchaseOrder.PurchaseProducts.Any()) + { + database.PurchaseProducts.RemoveRange(purchaseOrder.PurchaseProducts); + } + + database.PurchaseOrders.Remove(purchaseOrder); + await database.SaveChangesAsync(ct); + + await Send.NoContentAsync(ct); + } } \ No newline at end of file diff --git a/PyroFetes/Endpoints/PurchaseProduct/UpdatePurchaseProductEndpoint.cs b/PyroFetes/Endpoints/PurchaseProduct/UpdatePurchaseProductEndpoint.cs deleted file mode 100644 index 3373ae9..0000000 --- a/PyroFetes/Endpoints/PurchaseProduct/UpdatePurchaseProductEndpoint.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace PyroFetes.Endpoints.PurchaseProduct; - -public class UpdatePurchaseProductEndpoint -{ - -} \ No newline at end of file