using Gridify;
using Gridify.EntityFramework;
namespace GerbilManagerWebAPI.Common
{
/// Standard paged list envelope returned by every list endpoint.
public record PagedResult(IReadOnlyList Items, int TotalCount, int Page, int PageSize);
public static class QueryableExtensions
{
///
/// Apply a Gridify query (filter/order/page) to an EF query and project each
/// row to a DTO, returning the standard paged envelope. Filter/orderBy names
/// are the ENTITY property names (case-insensitive), e.g. "status==Active",
/// "orderBy=dateOfBirth", "litterId==...".
///
public static async Task> ToPagedResultAsync(
this IQueryable source,
GridifyQuery query,
Func map)
{
query.Page = query.Page <= 0 ? 1 : query.Page;
query.PageSize = query.PageSize <= 0 ? 20 : query.PageSize;
Paging paging = await source.GridifyAsync(query);
var items = paging.Data.Select(map).ToList();
return new PagedResult(items, paging.Count, query.Page, query.PageSize);
}
}
}