creating endpoint WareHouseProduct

This commit is contained in:
2025-10-17 16:23:39 +01:00
parent 59628717d4
commit fa72c6d777
10 changed files with 92 additions and 81 deletions

View File

@@ -0,0 +1,42 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.WareHouseProduct.Response;
namespace PyroFetes.Endpoints.WareHouseProduct;
public class GetTotalQuantityRequest
{
public int ProductId { get; set; }
}
public class GetTotalQuantityEndpoint(PyroFetesDbContext database) : Endpoint<GetTotalQuantityRequest, GetTotalQuantityDto>
{
public override void Configure()
{
Get("/api/wareHouseProduct/{ProductId}", x => new { x.ProductId });
AllowAnonymous();
}
public override async Task HandleAsync(GetTotalQuantityRequest req, CancellationToken ct)
{
bool exists = await database.WarehouseProducts
.AnyAsync(wp => wp.ProductId == req.ProductId, ct);
if (!exists)
{
await Send.NotFoundAsync(ct);
return;
}
var totalQuantity = await database.WarehouseProducts
.Where(wp => wp.ProductId == req.ProductId)
.SumAsync(wp => wp.Quantity, ct);
GetTotalQuantityDto responseDto = new()
{
ProductId = req.ProductId,
TotalQuantity = totalQuantity
};
await Send.OkAsync(responseDto, ct);
}
}

View File

@@ -0,0 +1,38 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.QuotationProduct.Request;
using PyroFetes.DTO.QuotationProduct.Response;
using PyroFetes.DTO.WareHouseProduct.Request;
using PyroFetes.DTO.WareHouseProduct.Response;
namespace PyroFetes.Endpoints.WareHouseProduct;
public class PatchWareHouseProductQuantityEndpoint(PyroFetesDbContext database) : Endpoint<PatchWareHouseProductQuantityDto, GetWareHouseProductDto>
{
public override void Configure()
{
Patch("/api/wareHouseProduct/{ProductId}/{WareHouseId}/Quantity", x => new { x.ProductId, x.WareHouseId });
AllowAnonymous();
}
public override async Task HandleAsync(PatchWareHouseProductQuantityDto req, CancellationToken ct)
{
var wareHouseProduct = await database.WarehouseProducts.SingleOrDefaultAsync(wp => wp.ProductId == req.ProductId && wp.WarehouseId == req.WareHouseId, ct);
if (wareHouseProduct == null)
{
await Send.NotFoundAsync(ct);
return;
}
wareHouseProduct.Quantity = req.Quantity;
await database.SaveChangesAsync(ct);
GetWareHouseProductDto responseDto = new()
{
ProductId = wareHouseProduct.ProductId,
WareHouseId = wareHouseProduct.WarehouseId,
Quantity = wareHouseProduct.Quantity
};
await Send.OkAsync(responseDto, ct);
}
}