creating endpoints from PurchaseOrder

This commit is contained in:
2025-10-16 23:28:27 +01:00
parent 8bd7fadabc
commit 4f12911263
6 changed files with 157 additions and 17 deletions

View File

@@ -1,7 +1,10 @@
namespace PyroFetes.DTO.PurchaseOrder.Response;
using PyroFetes.DTO.PurchaseProduct.Response;
namespace PyroFetes.DTO.PurchaseOrder.Response;
public class GetPurchaseOrderDto
{
public int Id { get; set; }
public string? PurchaseConditions { get; set; }
public List<GetPurchaseProductDto>? GetPurchaseProductDto { get; set; }
}

View File

@@ -0,0 +1,41 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
namespace PyroFetes.Endpoints.PurchaseOrder;
public class DeletePurchaseOrderRequest
{
public int Id { get; set; }
}
public class DeletePurchaseOrderEndpoint(PyroFetesDbContext database) : Endpoint<DeletePurchaseOrderRequest>
{
public override void Configure()
{
Delete("/api/purchaseOrders/{Id}", x => new {x.Id});
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);
}
}

View File

@@ -0,0 +1,45 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.PurchaseOrder.Response;
using PyroFetes.DTO.PurchaseProduct.Response;
namespace PyroFetes.Endpoints.PurchaseOrder;
public class GetAllPurchaseOrderEndpoint(PyroFetesDbContext database) : EndpointWithoutRequest<List<GetPurchaseOrderDto>>
{
public override void Configure()
{
Get("/api/purchaseOrders");
}
public override async Task HandleAsync(CancellationToken ct)
{
var purchaseOrder = await database.PurchaseOrders
.Include(p => p.PurchaseProducts)
.Select(purchaseOrder => new GetPurchaseOrderDto()
{
Id = purchaseOrder.Id,
PurchaseConditions = purchaseOrder.PurchaseConditions,
GetPurchaseProductDto = purchaseOrder.PurchaseProducts
.Select(p => new GetPurchaseProductDto
{
ProductId = p.ProductId,
ProductReferences = p.Product.Reference,
ProductName = p.Product.Name,
ProductDuration = p.Product.Duration,
ProductCaliber = p.Product.Caliber,
ProductApprovalNumber = p.Product.ApprovalNumber,
ProductWeight = p.Product.Weight,
ProductNec = p.Product.Nec,
ProductImage = p.Product.Image,
ProductLink = p.Product.Link,
ProductMinimalQuantity = p.Product.MinimalQuantity,
PurchaseOrderId = p.PurchaseOrderId,
Quantity = p.Quantity,
}).ToList()
})
.ToListAsync(ct);
await Send.OkAsync(purchaseOrder, ct);
}
}

View File

@@ -0,0 +1,56 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.PurchaseOrder.Response;
using PyroFetes.DTO.PurchaseProduct.Response;
namespace PyroFetes.Endpoints.PurchaseOrder;
public class GetPurchaseOrderRequest
{
public int Id { get; set; }
}
public class GetPurchaseOrderEndpoint(PyroFetesDbContext database) : Endpoint<GetPurchaseOrderRequest, GetPurchaseOrderDto>
{
public override void Configure()
{
Get("/api/purchaseOrders/{@Id}", x => new {x.Id});
}
public override async Task HandleAsync(GetPurchaseOrderRequest req, CancellationToken ct)
{
var purchaseOrder = await database.PurchaseOrders
.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
if (purchaseOrder == null)
{
await Send.NotFoundAsync(ct);
return;
}
GetPurchaseOrderDto responseDto = new()
{
Id = purchaseOrder.Id,
PurchaseConditions = purchaseOrder.PurchaseConditions,
GetPurchaseProductDto = purchaseOrder.PurchaseProducts
.Select(p => new GetPurchaseProductDto
{
ProductId = p.ProductId,
ProductReferences = p.Product.Reference,
ProductName = p.Product.Name,
ProductDuration = p.Product.Duration,
ProductCaliber = p.Product.Caliber,
ProductApprovalNumber = p.Product.ApprovalNumber,
ProductWeight = p.Product.Weight,
ProductNec = p.Product.Nec,
ProductImage = p.Product.Image,
ProductLink = p.Product.Link,
ProductMinimalQuantity = p.Product.MinimalQuantity,
PurchaseOrderId = p.PurchaseOrderId,
Quantity = p.Quantity,
}).ToList()
};
await Send.OkAsync(responseDto, ct);
}
}

View File

@@ -3,37 +3,32 @@ using Microsoft.EntityFrameworkCore;
namespace PyroFetes.Endpoints.PurchaseProduct;
public class DeletePurchaseOrderRequest
public class DeletePurchaseProductRequest
{
public int Id { get; set; }
public int ProductId { get; set; }
public int PurchaseOrderId { get; set; }
}
public class DeletePurchaseOrderEndpoint(PyroFetesDbContext database) : Endpoint<DeletePurchaseOrderRequest>
public class DeletePurchaseOrderEndpoint(PyroFetesDbContext database) : Endpoint<DeletePurchaseProductRequest>
{
public override void Configure()
{
Delete("/api/purchaseOrders/{Id}", x => new {x.Id});
Delete("/api/purchaseProducts/{ProductId}/{PurchaseOrderId}", x => new {x.ProductId, x.PurchaseOrderId});
AllowAnonymous();
}
public override async Task HandleAsync(DeletePurchaseOrderRequest req, CancellationToken ct)
public override async Task HandleAsync(DeletePurchaseProductRequest req, CancellationToken ct)
{
var purchaseOrder = await database.PurchaseOrders
.Include(po => po.PurchaseProducts)
.SingleOrDefaultAsync(po => po.Id == req.Id, ct);
var purchaseProduct = await database.PurchaseProducts
.SingleOrDefaultAsync(po => po.ProductId == req.ProductId && po.PurchaseOrderId == req.PurchaseOrderId, ct);
if (purchaseOrder == null)
if (purchaseProduct == null)
{
await Send.NotFoundAsync(ct);
return;
}
if (purchaseOrder.PurchaseProducts != null && purchaseOrder.PurchaseProducts.Any())
{
database.PurchaseProducts.RemoveRange(purchaseOrder.PurchaseProducts);
}
database.PurchaseOrders.Remove(purchaseOrder);
database.PurchaseProducts.Remove(purchaseProduct);
await database.SaveChangesAsync(ct);
await Send.NoContentAsync(ct);

View File

@@ -9,7 +9,7 @@ public class PatchPurchaseProductQuantityEndpoint(PyroFetesDbContext database) :
{
public override void Configure()
{
Patch("/api/purchaseOrders/{ProductId}/{PurchaseOrderId}/Quantity", x => new { x.ProductId, x.PurchaseOrderId });
Patch("/api/purchaseProducts/{ProductId}/{PurchaseOrderId}/Quantity", x => new { x.ProductId, x.PurchaseOrderId });
AllowAnonymous();
}