creating purchaseproduct's endpoint and fix error in dto

This commit is contained in:
2025-10-16 22:53:14 +01:00
parent 791eff9256
commit e4e6c1c3f7
11 changed files with 55 additions and 19 deletions

View File

@@ -1,6 +1,36 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.PurchaseProduct.Request;
using PyroFetes.DTO.PurchaseProduct.Response;
namespace PyroFetes.Endpoints.PurchaseProduct;
public class PatchPurchaseProductQuantityEndpoint
public class PatchPurchaseProductQuantityEndpoint(PyroFetesDbContext database) : Endpoint<PatchPurchaseProductQuantityDto, GetPurchaseProductDto>
{
public override void Configure()
{
Patch("/api/purchaseOrders/{ProductId}/{PurchaseOrderId}/Quantity", x => new { x.ProductId, x.PurchaseOrderId });
AllowAnonymous();
}
public override async Task HandleAsync(PatchPurchaseProductQuantityDto req, CancellationToken ct)
{
var purchaseProduct = await database.PurchaseProducts.SingleOrDefaultAsync(po => po.ProductId == req.ProductId && po.PurchaseOrderId == req.PurchaseOrderId, ct);
if (purchaseProduct == null)
{
await Send.NotFoundAsync(ct);
return;
}
purchaseProduct.Quantity = req.Quantity;
await database.SaveChangesAsync(ct);
GetPurchaseProductDto responseDto = new()
{
ProductId = purchaseProduct.ProductId,
PurchaseOrderId = purchaseProduct.PurchaseOrderId,
Quantity = purchaseProduct.Quantity
};
await Send.OkAsync(responseDto, ct);
}
}