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 Microsoft.EntityFrameworkCore;
|
||||
using xCommons.Extensions;
|
||||
using xDataService.Configuration;
|
||||
using xDataService.Constants;
|
||||
using xExceptions.Constants;
|
||||
using xModels.Base;
|
||||
using xModels.Dtos;
|
||||
|
||||
namespace xDataService.Extensions {
|
||||
public static class IQueryableExtensions {
|
||||
namespace xDataService.Extensions
|
||||
{
|
||||
public static class IQueryableExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Apply Search Filter on a Query
|
||||
/// </summary>
|
||||
@@ -23,7 +27,8 @@ namespace xDataService.Extensions {
|
||||
this IQueryable<T> source,
|
||||
string filter
|
||||
)
|
||||
where T : XBaseEntity<TKey> {
|
||||
where T : XBaseEntity<TKey>
|
||||
{
|
||||
//
|
||||
// Apply Filter ...
|
||||
return source.Where(r => r.PropValuesContains(filter));
|
||||
@@ -40,7 +45,8 @@ namespace xDataService.Extensions {
|
||||
this IQueryable<T> source,
|
||||
string filter
|
||||
)
|
||||
where T : XBaseEntity<TKey> {
|
||||
where T : XBaseEntity<TKey>
|
||||
{
|
||||
//
|
||||
// Where Clause ...
|
||||
Expression<Func<T, bool>> whereClause = r => r
|
||||
@@ -58,10 +64,12 @@ namespace xDataService.Extensions {
|
||||
//
|
||||
var result = new List<T>();
|
||||
await
|
||||
foreach (var entity in enumerator) {
|
||||
foreach (var entity in enumerator)
|
||||
{
|
||||
//
|
||||
var isApproved = whereFunc(entity);
|
||||
if (isApproved) {
|
||||
if (isApproved)
|
||||
{
|
||||
result.Add(entity);
|
||||
}
|
||||
}
|
||||
@@ -81,7 +89,8 @@ namespace xDataService.Extensions {
|
||||
this IEnumerable<T> source,
|
||||
string filter
|
||||
)
|
||||
where T : XBaseEntity<TKey> {
|
||||
where T : XBaseEntity<TKey>
|
||||
{
|
||||
//
|
||||
// Where Clause ...
|
||||
Expression<Func<T, bool>> whereClause = r => r
|
||||
@@ -100,10 +109,12 @@ namespace xDataService.Extensions {
|
||||
//
|
||||
var result = new List<T>();
|
||||
await
|
||||
foreach (var entity in enumerator) {
|
||||
foreach (var entity in enumerator)
|
||||
{
|
||||
//
|
||||
var isApproved = whereFunc(entity);
|
||||
if (isApproved) {
|
||||
if (isApproved)
|
||||
{
|
||||
result.Add(entity);
|
||||
}
|
||||
}
|
||||
@@ -124,7 +135,8 @@ namespace xDataService.Extensions {
|
||||
this IEnumerable<T> source,
|
||||
string filter
|
||||
)
|
||||
where T : class {
|
||||
where T : class
|
||||
{
|
||||
//
|
||||
// Apply Filter ...
|
||||
return source.Where(r => r.PropValuesContains(filter));
|
||||
@@ -140,14 +152,16 @@ namespace xDataService.Extensions {
|
||||
public static IOrderedQueryable<TSource> OrderBy<TSource>(
|
||||
this IQueryable<TSource> query,
|
||||
string propertyName
|
||||
) {
|
||||
)
|
||||
{
|
||||
//
|
||||
var entityType = typeof(TSource);
|
||||
|
||||
//
|
||||
// Create x => x.PropName
|
||||
var propertyInfo = entityType.GetProperty(propertyName);
|
||||
if (propertyInfo.IsNull ()) {
|
||||
if (propertyInfo.IsNull())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
@@ -161,7 +175,8 @@ namespace xDataService.Extensions {
|
||||
var enumarableType = typeof(System.Linq.Queryable);
|
||||
var method = enumarableType.GetMethods()
|
||||
.Where(m => m.Name == "OrderBy" && m.IsGenericMethodDefinition)
|
||||
.Where (m => {
|
||||
.Where(m =>
|
||||
{
|
||||
//
|
||||
var parameters = m.GetParameters().ToList();
|
||||
|
||||
@@ -198,11 +213,13 @@ namespace xDataService.Extensions {
|
||||
public static IOrderedEnumerable<TSource> OrderBy<TSource>(
|
||||
this IEnumerable<TSource> query,
|
||||
string propertyName
|
||||
) {
|
||||
)
|
||||
{
|
||||
//
|
||||
var entityType = typeof(TSource);
|
||||
var propertyInfo = entityType.GetProperty(propertyName);
|
||||
if (propertyInfo.IsNull ()) {
|
||||
if (propertyInfo.IsNull())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
@@ -220,14 +237,16 @@ namespace xDataService.Extensions {
|
||||
public static IOrderedQueryable<TSource> OrderByDescending<TSource>(
|
||||
this IQueryable<TSource> query,
|
||||
string propertyName
|
||||
) {
|
||||
)
|
||||
{
|
||||
//
|
||||
var entityType = typeof(TSource);
|
||||
|
||||
//
|
||||
// Create x => x.PropName
|
||||
var propertyInfo = entityType.GetProperty(propertyName);
|
||||
if (propertyInfo.IsNull ()) {
|
||||
if (propertyInfo.IsNull())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
@@ -241,7 +260,8 @@ namespace xDataService.Extensions {
|
||||
var enumarableType = typeof(System.Linq.Queryable);
|
||||
var method = enumarableType.GetMethods()
|
||||
.Where(m => m.Name == "OrderByDescending" && m.IsGenericMethodDefinition)
|
||||
.Where (m => {
|
||||
.Where(m =>
|
||||
{
|
||||
//
|
||||
var parameters = m.GetParameters().ToList();
|
||||
|
||||
@@ -278,11 +298,13 @@ namespace xDataService.Extensions {
|
||||
public static IOrderedEnumerable<TSource> OrderByDescending<TSource>(
|
||||
this IEnumerable<TSource> query,
|
||||
string propertyName
|
||||
) {
|
||||
)
|
||||
{
|
||||
//
|
||||
var entityType = typeof(TSource);
|
||||
var propertyInfo = entityType.GetProperty(propertyName);
|
||||
if (propertyInfo.IsNull ()) {
|
||||
if (propertyInfo.IsNull())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
@@ -305,15 +327,18 @@ namespace xDataService.Extensions {
|
||||
bool isAscending,
|
||||
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()
|
||||
.GetDefaultColumnsMap();
|
||||
|
||||
//
|
||||
if (columnsMap == null) {
|
||||
if (columnsMap == null)
|
||||
{
|
||||
return source;
|
||||
}
|
||||
}
|
||||
@@ -323,15 +348,19 @@ namespace xDataService.Extensions {
|
||||
var sortByItem = columnsMap.Keys
|
||||
.FirstOrDefault(p =>
|
||||
p.ToNormalString() == sortBy.ToNormalString());
|
||||
if (sortByItem.IsNullOrEmpty ()) {
|
||||
if (sortByItem.IsNullOrEmpty())
|
||||
{
|
||||
return source;
|
||||
}
|
||||
|
||||
//
|
||||
// Apply Sorting ...
|
||||
if (isAscending) {
|
||||
if (isAscending)
|
||||
{
|
||||
return source.OrderBy(sortByItem);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
return source.OrderByDescending(sortByItem);
|
||||
}
|
||||
}
|
||||
@@ -351,15 +380,18 @@ namespace xDataService.Extensions {
|
||||
bool isAscending,
|
||||
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()
|
||||
.GetDefaultColumnsMap();
|
||||
|
||||
//
|
||||
if (columnsMap == null) {
|
||||
if (columnsMap == null)
|
||||
{
|
||||
return source;
|
||||
}
|
||||
}
|
||||
@@ -369,15 +401,19 @@ namespace xDataService.Extensions {
|
||||
var sortByItem = columnsMap.Keys
|
||||
.FirstOrDefault(p =>
|
||||
p.ToNormalString() == sortBy.ToNormalString());
|
||||
if (sortByItem.IsNullOrEmpty ()) {
|
||||
if (sortByItem.IsNullOrEmpty())
|
||||
{
|
||||
return source;
|
||||
}
|
||||
|
||||
//
|
||||
// Apply Sorting ...
|
||||
if (isAscending) {
|
||||
if (isAscending)
|
||||
{
|
||||
return source.OrderBy(sortByItem);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
return source.OrderByDescending(sortByItem);
|
||||
}
|
||||
}
|
||||
@@ -397,15 +433,18 @@ namespace xDataService.Extensions {
|
||||
bool isAscending,
|
||||
IDictionary<string, Expression<Func<T, object>>> columnsMap = null
|
||||
)
|
||||
where T : class {
|
||||
where T : class
|
||||
{
|
||||
//
|
||||
if (columnsMap == null) {
|
||||
if (columnsMap == null)
|
||||
{
|
||||
//
|
||||
columnsMap = source.FirstOrDefault()
|
||||
.GetDefaultColumnsMap();
|
||||
|
||||
//
|
||||
if (columnsMap == null) {
|
||||
if (columnsMap == null)
|
||||
{
|
||||
return source;
|
||||
}
|
||||
}
|
||||
@@ -415,15 +454,19 @@ namespace xDataService.Extensions {
|
||||
var sortByItem = columnsMap.Keys
|
||||
.FirstOrDefault(p =>
|
||||
p.ToNormalString() == sortBy.ToNormalString());
|
||||
if (sortByItem.IsNullOrEmpty ()) {
|
||||
if (sortByItem.IsNullOrEmpty())
|
||||
{
|
||||
return source;
|
||||
}
|
||||
|
||||
//
|
||||
// Apply Sorting ...
|
||||
if (isAscending) {
|
||||
if (isAscending)
|
||||
{
|
||||
return source.OrderBy(sortByItem);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
return source.OrderByDescending(sortByItem);
|
||||
}
|
||||
}
|
||||
@@ -440,17 +483,20 @@ namespace xDataService.Extensions {
|
||||
this IQueryable<T> source,
|
||||
int? page,
|
||||
int? pageSize
|
||||
) {
|
||||
)
|
||||
{
|
||||
//
|
||||
if (!page.HasValue ||
|
||||
page <= 0) {
|
||||
page <= 0)
|
||||
{
|
||||
page = 1;
|
||||
}
|
||||
|
||||
//
|
||||
if (!pageSize.HasValue ||
|
||||
pageSize <= 0 ||
|
||||
pageSize > XDataServiceConstants.MAX_AVAILABLE_PAGE_SIZE) {
|
||||
pageSize > XDataServiceConstants.MAX_AVAILABLE_PAGE_SIZE)
|
||||
{
|
||||
pageSize = XDataServiceConstants.DEFAULT_PAGE_SIZE;
|
||||
}
|
||||
|
||||
@@ -476,17 +522,20 @@ namespace xDataService.Extensions {
|
||||
this IEnumerable<T> source,
|
||||
int? page,
|
||||
int? pageSize
|
||||
) {
|
||||
)
|
||||
{
|
||||
//
|
||||
if (!page.HasValue ||
|
||||
page <= 0) {
|
||||
page <= 0)
|
||||
{
|
||||
page = 1;
|
||||
}
|
||||
|
||||
//
|
||||
if (!pageSize.HasValue ||
|
||||
pageSize <= 0 ||
|
||||
pageSize > XDataServiceConstants.MAX_AVAILABLE_PAGE_SIZE) {
|
||||
pageSize > XDataServiceConstants.MAX_AVAILABLE_PAGE_SIZE)
|
||||
{
|
||||
pageSize = XDataServiceConstants.DEFAULT_PAGE_SIZE;
|
||||
}
|
||||
|
||||
@@ -499,5 +548,64 @@ namespace xDataService.Extensions {
|
||||
.Skip((pageVal - 1) * 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