add some Extensions Methods for XQuery Pages Counting ...
This commit is contained in:
@@ -6,12 +6,16 @@ using System.Reflection;
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using xCommons.Extensions;
|
using xCommons.Extensions;
|
||||||
|
using xDataService.Configuration;
|
||||||
using xDataService.Constants;
|
using xDataService.Constants;
|
||||||
using xExceptions.Constants;
|
using xExceptions.Constants;
|
||||||
using xModels.Base;
|
using xModels.Base;
|
||||||
|
using xModels.Dtos;
|
||||||
|
|
||||||
namespace xDataService.Extensions {
|
namespace xDataService.Extensions
|
||||||
public static class IQueryableExtensions {
|
{
|
||||||
|
public static class IQueryableExtensions
|
||||||
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Apply Search Filter on a Query
|
/// Apply Search Filter on a Query
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -23,7 +27,8 @@ namespace xDataService.Extensions {
|
|||||||
this IQueryable<T> source,
|
this IQueryable<T> source,
|
||||||
string filter
|
string filter
|
||||||
)
|
)
|
||||||
where T : XBaseEntity<TKey> {
|
where T : XBaseEntity<TKey>
|
||||||
|
{
|
||||||
//
|
//
|
||||||
// Apply Filter ...
|
// Apply Filter ...
|
||||||
return source.Where(r => r.PropValuesContains(filter));
|
return source.Where(r => r.PropValuesContains(filter));
|
||||||
@@ -40,7 +45,8 @@ namespace xDataService.Extensions {
|
|||||||
this IQueryable<T> source,
|
this IQueryable<T> source,
|
||||||
string filter
|
string filter
|
||||||
)
|
)
|
||||||
where T : XBaseEntity<TKey> {
|
where T : XBaseEntity<TKey>
|
||||||
|
{
|
||||||
//
|
//
|
||||||
// Where Clause ...
|
// Where Clause ...
|
||||||
Expression<Func<T, bool>> whereClause = r => r
|
Expression<Func<T, bool>> whereClause = r => r
|
||||||
@@ -58,10 +64,12 @@ namespace xDataService.Extensions {
|
|||||||
//
|
//
|
||||||
var result = new List<T>();
|
var result = new List<T>();
|
||||||
await
|
await
|
||||||
foreach (var entity in enumerator) {
|
foreach (var entity in enumerator)
|
||||||
|
{
|
||||||
//
|
//
|
||||||
var isApproved = whereFunc(entity);
|
var isApproved = whereFunc(entity);
|
||||||
if (isApproved) {
|
if (isApproved)
|
||||||
|
{
|
||||||
result.Add(entity);
|
result.Add(entity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -81,7 +89,8 @@ namespace xDataService.Extensions {
|
|||||||
this IEnumerable<T> source,
|
this IEnumerable<T> source,
|
||||||
string filter
|
string filter
|
||||||
)
|
)
|
||||||
where T : XBaseEntity<TKey> {
|
where T : XBaseEntity<TKey>
|
||||||
|
{
|
||||||
//
|
//
|
||||||
// Where Clause ...
|
// Where Clause ...
|
||||||
Expression<Func<T, bool>> whereClause = r => r
|
Expression<Func<T, bool>> whereClause = r => r
|
||||||
@@ -100,10 +109,12 @@ namespace xDataService.Extensions {
|
|||||||
//
|
//
|
||||||
var result = new List<T>();
|
var result = new List<T>();
|
||||||
await
|
await
|
||||||
foreach (var entity in enumerator) {
|
foreach (var entity in enumerator)
|
||||||
|
{
|
||||||
//
|
//
|
||||||
var isApproved = whereFunc(entity);
|
var isApproved = whereFunc(entity);
|
||||||
if (isApproved) {
|
if (isApproved)
|
||||||
|
{
|
||||||
result.Add(entity);
|
result.Add(entity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -124,7 +135,8 @@ namespace xDataService.Extensions {
|
|||||||
this IEnumerable<T> source,
|
this IEnumerable<T> source,
|
||||||
string filter
|
string filter
|
||||||
)
|
)
|
||||||
where T : class {
|
where T : class
|
||||||
|
{
|
||||||
//
|
//
|
||||||
// Apply Filter ...
|
// Apply Filter ...
|
||||||
return source.Where(r => r.PropValuesContains(filter));
|
return source.Where(r => r.PropValuesContains(filter));
|
||||||
@@ -140,14 +152,16 @@ namespace xDataService.Extensions {
|
|||||||
public static IOrderedQueryable<TSource> OrderBy<TSource>(
|
public static IOrderedQueryable<TSource> OrderBy<TSource>(
|
||||||
this IQueryable<TSource> query,
|
this IQueryable<TSource> query,
|
||||||
string propertyName
|
string propertyName
|
||||||
) {
|
)
|
||||||
|
{
|
||||||
//
|
//
|
||||||
var entityType = typeof(TSource);
|
var entityType = typeof(TSource);
|
||||||
|
|
||||||
//
|
//
|
||||||
// Create x => x.PropName
|
// Create x => x.PropName
|
||||||
var propertyInfo = entityType.GetProperty(propertyName);
|
var propertyInfo = entityType.GetProperty(propertyName);
|
||||||
if (propertyInfo.IsNull ()) {
|
if (propertyInfo.IsNull())
|
||||||
|
{
|
||||||
XException.InvalidArgs.Throw();
|
XException.InvalidArgs.Throw();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -161,7 +175,8 @@ namespace xDataService.Extensions {
|
|||||||
var enumarableType = typeof(System.Linq.Queryable);
|
var enumarableType = typeof(System.Linq.Queryable);
|
||||||
var method = enumarableType.GetMethods()
|
var method = enumarableType.GetMethods()
|
||||||
.Where(m => m.Name == "OrderBy" && m.IsGenericMethodDefinition)
|
.Where(m => m.Name == "OrderBy" && m.IsGenericMethodDefinition)
|
||||||
.Where (m => {
|
.Where(m =>
|
||||||
|
{
|
||||||
//
|
//
|
||||||
var parameters = m.GetParameters().ToList();
|
var parameters = m.GetParameters().ToList();
|
||||||
|
|
||||||
@@ -198,11 +213,13 @@ namespace xDataService.Extensions {
|
|||||||
public static IOrderedEnumerable<TSource> OrderBy<TSource>(
|
public static IOrderedEnumerable<TSource> OrderBy<TSource>(
|
||||||
this IEnumerable<TSource> query,
|
this IEnumerable<TSource> query,
|
||||||
string propertyName
|
string propertyName
|
||||||
) {
|
)
|
||||||
|
{
|
||||||
//
|
//
|
||||||
var entityType = typeof(TSource);
|
var entityType = typeof(TSource);
|
||||||
var propertyInfo = entityType.GetProperty(propertyName);
|
var propertyInfo = entityType.GetProperty(propertyName);
|
||||||
if (propertyInfo.IsNull ()) {
|
if (propertyInfo.IsNull())
|
||||||
|
{
|
||||||
XException.InvalidArgs.Throw();
|
XException.InvalidArgs.Throw();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -220,14 +237,16 @@ namespace xDataService.Extensions {
|
|||||||
public static IOrderedQueryable<TSource> OrderByDescending<TSource>(
|
public static IOrderedQueryable<TSource> OrderByDescending<TSource>(
|
||||||
this IQueryable<TSource> query,
|
this IQueryable<TSource> query,
|
||||||
string propertyName
|
string propertyName
|
||||||
) {
|
)
|
||||||
|
{
|
||||||
//
|
//
|
||||||
var entityType = typeof(TSource);
|
var entityType = typeof(TSource);
|
||||||
|
|
||||||
//
|
//
|
||||||
// Create x => x.PropName
|
// Create x => x.PropName
|
||||||
var propertyInfo = entityType.GetProperty(propertyName);
|
var propertyInfo = entityType.GetProperty(propertyName);
|
||||||
if (propertyInfo.IsNull ()) {
|
if (propertyInfo.IsNull())
|
||||||
|
{
|
||||||
XException.InvalidArgs.Throw();
|
XException.InvalidArgs.Throw();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -241,7 +260,8 @@ namespace xDataService.Extensions {
|
|||||||
var enumarableType = typeof(System.Linq.Queryable);
|
var enumarableType = typeof(System.Linq.Queryable);
|
||||||
var method = enumarableType.GetMethods()
|
var method = enumarableType.GetMethods()
|
||||||
.Where(m => m.Name == "OrderByDescending" && m.IsGenericMethodDefinition)
|
.Where(m => m.Name == "OrderByDescending" && m.IsGenericMethodDefinition)
|
||||||
.Where (m => {
|
.Where(m =>
|
||||||
|
{
|
||||||
//
|
//
|
||||||
var parameters = m.GetParameters().ToList();
|
var parameters = m.GetParameters().ToList();
|
||||||
|
|
||||||
@@ -278,11 +298,13 @@ namespace xDataService.Extensions {
|
|||||||
public static IOrderedEnumerable<TSource> OrderByDescending<TSource>(
|
public static IOrderedEnumerable<TSource> OrderByDescending<TSource>(
|
||||||
this IEnumerable<TSource> query,
|
this IEnumerable<TSource> query,
|
||||||
string propertyName
|
string propertyName
|
||||||
) {
|
)
|
||||||
|
{
|
||||||
//
|
//
|
||||||
var entityType = typeof(TSource);
|
var entityType = typeof(TSource);
|
||||||
var propertyInfo = entityType.GetProperty(propertyName);
|
var propertyInfo = entityType.GetProperty(propertyName);
|
||||||
if (propertyInfo.IsNull ()) {
|
if (propertyInfo.IsNull())
|
||||||
|
{
|
||||||
XException.InvalidArgs.Throw();
|
XException.InvalidArgs.Throw();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -305,15 +327,18 @@ namespace xDataService.Extensions {
|
|||||||
bool isAscending,
|
bool isAscending,
|
||||||
IDictionary<string, Expression<Func<T, object>>> columnsMap = null
|
IDictionary<string, Expression<Func<T, object>>> columnsMap = null
|
||||||
)
|
)
|
||||||
where T : XBaseEntity<TKey> {
|
where T : XBaseEntity<TKey>
|
||||||
|
{
|
||||||
//
|
//
|
||||||
if (columnsMap == null) {
|
if (columnsMap == null)
|
||||||
|
{
|
||||||
//
|
//
|
||||||
columnsMap = source.FirstOrDefault()
|
columnsMap = source.FirstOrDefault()
|
||||||
.GetDefaultColumnsMap();
|
.GetDefaultColumnsMap();
|
||||||
|
|
||||||
//
|
//
|
||||||
if (columnsMap == null) {
|
if (columnsMap == null)
|
||||||
|
{
|
||||||
return source;
|
return source;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -323,15 +348,19 @@ namespace xDataService.Extensions {
|
|||||||
var sortByItem = columnsMap.Keys
|
var sortByItem = columnsMap.Keys
|
||||||
.FirstOrDefault(p =>
|
.FirstOrDefault(p =>
|
||||||
p.ToNormalString() == sortBy.ToNormalString());
|
p.ToNormalString() == sortBy.ToNormalString());
|
||||||
if (sortByItem.IsNullOrEmpty ()) {
|
if (sortByItem.IsNullOrEmpty())
|
||||||
|
{
|
||||||
return source;
|
return source;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Apply Sorting ...
|
// Apply Sorting ...
|
||||||
if (isAscending) {
|
if (isAscending)
|
||||||
|
{
|
||||||
return source.OrderBy(sortByItem);
|
return source.OrderBy(sortByItem);
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
return source.OrderByDescending(sortByItem);
|
return source.OrderByDescending(sortByItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -351,15 +380,18 @@ namespace xDataService.Extensions {
|
|||||||
bool isAscending,
|
bool isAscending,
|
||||||
IDictionary<string, Expression<Func<T, object>>> columnsMap = null
|
IDictionary<string, Expression<Func<T, object>>> columnsMap = null
|
||||||
)
|
)
|
||||||
where T : XBaseEntity<TKey> {
|
where T : XBaseEntity<TKey>
|
||||||
|
{
|
||||||
//
|
//
|
||||||
if (columnsMap == null) {
|
if (columnsMap == null)
|
||||||
|
{
|
||||||
//
|
//
|
||||||
columnsMap = source.FirstOrDefault()
|
columnsMap = source.FirstOrDefault()
|
||||||
.GetDefaultColumnsMap();
|
.GetDefaultColumnsMap();
|
||||||
|
|
||||||
//
|
//
|
||||||
if (columnsMap == null) {
|
if (columnsMap == null)
|
||||||
|
{
|
||||||
return source;
|
return source;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -369,15 +401,19 @@ namespace xDataService.Extensions {
|
|||||||
var sortByItem = columnsMap.Keys
|
var sortByItem = columnsMap.Keys
|
||||||
.FirstOrDefault(p =>
|
.FirstOrDefault(p =>
|
||||||
p.ToNormalString() == sortBy.ToNormalString());
|
p.ToNormalString() == sortBy.ToNormalString());
|
||||||
if (sortByItem.IsNullOrEmpty ()) {
|
if (sortByItem.IsNullOrEmpty())
|
||||||
|
{
|
||||||
return source;
|
return source;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Apply Sorting ...
|
// Apply Sorting ...
|
||||||
if (isAscending) {
|
if (isAscending)
|
||||||
|
{
|
||||||
return source.OrderBy(sortByItem);
|
return source.OrderBy(sortByItem);
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
return source.OrderByDescending(sortByItem);
|
return source.OrderByDescending(sortByItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -397,15 +433,18 @@ namespace xDataService.Extensions {
|
|||||||
bool isAscending,
|
bool isAscending,
|
||||||
IDictionary<string, Expression<Func<T, object>>> columnsMap = null
|
IDictionary<string, Expression<Func<T, object>>> columnsMap = null
|
||||||
)
|
)
|
||||||
where T : class {
|
where T : class
|
||||||
|
{
|
||||||
//
|
//
|
||||||
if (columnsMap == null) {
|
if (columnsMap == null)
|
||||||
|
{
|
||||||
//
|
//
|
||||||
columnsMap = source.FirstOrDefault()
|
columnsMap = source.FirstOrDefault()
|
||||||
.GetDefaultColumnsMap();
|
.GetDefaultColumnsMap();
|
||||||
|
|
||||||
//
|
//
|
||||||
if (columnsMap == null) {
|
if (columnsMap == null)
|
||||||
|
{
|
||||||
return source;
|
return source;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -415,15 +454,19 @@ namespace xDataService.Extensions {
|
|||||||
var sortByItem = columnsMap.Keys
|
var sortByItem = columnsMap.Keys
|
||||||
.FirstOrDefault(p =>
|
.FirstOrDefault(p =>
|
||||||
p.ToNormalString() == sortBy.ToNormalString());
|
p.ToNormalString() == sortBy.ToNormalString());
|
||||||
if (sortByItem.IsNullOrEmpty ()) {
|
if (sortByItem.IsNullOrEmpty())
|
||||||
|
{
|
||||||
return source;
|
return source;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Apply Sorting ...
|
// Apply Sorting ...
|
||||||
if (isAscending) {
|
if (isAscending)
|
||||||
|
{
|
||||||
return source.OrderBy(sortByItem);
|
return source.OrderBy(sortByItem);
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
return source.OrderByDescending(sortByItem);
|
return source.OrderByDescending(sortByItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -440,17 +483,20 @@ namespace xDataService.Extensions {
|
|||||||
this IQueryable<T> source,
|
this IQueryable<T> source,
|
||||||
int? page,
|
int? page,
|
||||||
int? pageSize
|
int? pageSize
|
||||||
) {
|
)
|
||||||
|
{
|
||||||
//
|
//
|
||||||
if (!page.HasValue ||
|
if (!page.HasValue ||
|
||||||
page <= 0) {
|
page <= 0)
|
||||||
|
{
|
||||||
page = 1;
|
page = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
if (!pageSize.HasValue ||
|
if (!pageSize.HasValue ||
|
||||||
pageSize <= 0 ||
|
pageSize <= 0 ||
|
||||||
pageSize > XDataServiceConstants.MAX_AVAILABLE_PAGE_SIZE) {
|
pageSize > XDataServiceConstants.MAX_AVAILABLE_PAGE_SIZE)
|
||||||
|
{
|
||||||
pageSize = XDataServiceConstants.DEFAULT_PAGE_SIZE;
|
pageSize = XDataServiceConstants.DEFAULT_PAGE_SIZE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -476,17 +522,20 @@ namespace xDataService.Extensions {
|
|||||||
this IEnumerable<T> source,
|
this IEnumerable<T> source,
|
||||||
int? page,
|
int? page,
|
||||||
int? pageSize
|
int? pageSize
|
||||||
) {
|
)
|
||||||
|
{
|
||||||
//
|
//
|
||||||
if (!page.HasValue ||
|
if (!page.HasValue ||
|
||||||
page <= 0) {
|
page <= 0)
|
||||||
|
{
|
||||||
page = 1;
|
page = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
if (!pageSize.HasValue ||
|
if (!pageSize.HasValue ||
|
||||||
pageSize <= 0 ||
|
pageSize <= 0 ||
|
||||||
pageSize > XDataServiceConstants.MAX_AVAILABLE_PAGE_SIZE) {
|
pageSize > XDataServiceConstants.MAX_AVAILABLE_PAGE_SIZE)
|
||||||
|
{
|
||||||
pageSize = XDataServiceConstants.DEFAULT_PAGE_SIZE;
|
pageSize = XDataServiceConstants.DEFAULT_PAGE_SIZE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -499,5 +548,64 @@ namespace xDataService.Extensions {
|
|||||||
.Skip((pageVal - 1) * pageSizeVal)
|
.Skip((pageVal - 1) * pageSizeVal)
|
||||||
.Take(pageSizeVal);
|
.Take(pageSizeVal);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Count Query Pages based on Specified Total Number ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <param name="totalItems"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static int CountPages(this XQuery source, int totalItems)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
int result = 0;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Validate and Calculate ...
|
||||||
|
if (!source.IsNull() && source.PageSize > 0 && totalItems > 0)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
result = totalItems / source.PageSize;
|
||||||
|
if (totalItems % source.PageSize > 0)
|
||||||
|
{
|
||||||
|
result++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Normalize an XQuery based on Data Service Configurations ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <param name="configuration"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static XQuery NormalizeQuery(
|
||||||
|
this XQuery source,
|
||||||
|
XDataServiceConfiguration configuration //
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (source.IsNull())
|
||||||
|
{
|
||||||
|
source = new XQuery();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Page Size Normalization ...
|
||||||
|
if (source.PageSize < configuration.PagingConfiguration.MinAvailablePageSize)
|
||||||
|
{
|
||||||
|
source.PageSize = configuration.PagingConfiguration.MinAvailablePageSize;
|
||||||
|
}
|
||||||
|
if (source.PageSize > configuration.PagingConfiguration.MaxAvailablePageSize)
|
||||||
|
{
|
||||||
|
source.PageSize = configuration.PagingConfiguration.MaxAvailablePageSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return source;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user