Files
KArtSell.Aegis/docs/Design/kbx-foundation-v36/backend/Modules/OMS/Lookups/Items/Endpoint.cs
T

34 lines
1.5 KiB
C#

using Dapper;
using FastEndpoints;
using Npgsql;
namespace Modules.OMS.Lookups.Items;
public sealed class Endpoint(NpgsqlDataSource dataSource) : EndpointWithoutRequest
{
public override void Configure()
{
Get("/api/lookups/items");
Permissions("erp.item.read");
}
public override async Task HandleAsync(CancellationToken ct)
{
var query = Query<string>("query", false) ?? string.Empty;
var page = Math.Max(1, Query<int>("page", false));
var requestedPageSize = Query<int>("pageSize", false);
var pageSize = Math.Clamp(requestedPageSize == 0 ? 30 : requestedPageSize, 1, 100);
await using var connection = await dataSource.OpenConnectionAsync(ct);
var items = (await connection.QueryAsync(new CommandDefinition(@"
select id, code, name as DisplayName, case when is_active then '사용' else '중지' end as Status
from catalog.items
where is_active = true and (@Query = '' or code ilike '%' || @Query || '%' or name ilike '%' || @Query || '%')
order by code
offset @Offset limit @PageSize;", new { Query = query, Offset = (page - 1) * pageSize, PageSize = pageSize }, cancellationToken: ct))).ToArray();
var count = await connection.ExecuteScalarAsync<int>(new CommandDefinition(@"
select count(*) from catalog.items
where is_active = true and (@Query = '' or code ilike '%' || @Query || '%' or name ilike '%' || @Query || '%');", new { Query = query }, cancellationToken: ct));
await Send.OkAsync(new { items, totalCount = count }, ct);
}
}