using System.Linq.Expressions; using BlackRock.Reporting.API.Core.Models; namespace BlackRock.Reporting.API.Core.Extensions { public static class IQueryableExtensions { public static IQueryable ApplyOrdering( this IQueryable query, SortFilter queryObj, Dictionary>> columnsMap) { if (string.IsNullOrWhiteSpace(queryObj.SortBy) || !columnsMap.ContainsKey(queryObj.SortBy)) return query; if (queryObj.IsSortAscending.GetValueOrDefault()) return query.OrderBy(columnsMap[queryObj.SortBy]); return query.OrderByDescending(columnsMap[queryObj.SortBy]); } public static IQueryable ApplyPagging(this IQueryable query,PaginationFilter queryObj) { if (queryObj.Page is null) queryObj.Page = 1; if (queryObj.PageSize is null) queryObj.PageSize = 10; return query.Skip((queryObj.Page.GetValueOrDefault() - 1) * queryObj.PageSize.GetValueOrDefault()) .Take(queryObj.PageSize.GetValueOrDefault()); } } }