Initial commit - Auth
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Auth.Core.Utility;
|
||||
|
||||
public static class FilterExtension
|
||||
{
|
||||
public static IQueryable<T> ApplyFilter<T, TFilter>(this IQueryable<T> query, TFilter filter)
|
||||
where TFilter : class
|
||||
{
|
||||
if (filter == null)
|
||||
return query;
|
||||
|
||||
var parameter = Expression.Parameter(typeof(T), "x");
|
||||
Expression finalExpression = null;
|
||||
|
||||
var filterProperties = typeof(TFilter)
|
||||
.GetProperties()
|
||||
.Where(p => p.GetValue(filter) != null);
|
||||
|
||||
foreach (var filterProperty in filterProperties)
|
||||
{
|
||||
var filterValue = filterProperty.GetValue(filter);
|
||||
if (filterValue == null)
|
||||
continue;
|
||||
|
||||
var propertyName = filterProperty.Name;
|
||||
PropertyInfo entityProperty;
|
||||
|
||||
Expression propertyExpression = null;
|
||||
Expression comparison = null;
|
||||
|
||||
// Handle special suffix cases
|
||||
if (propertyName.EndsWith("_Start") || propertyName.EndsWith("_Min"))
|
||||
{
|
||||
entityProperty = typeof(T).GetProperty(
|
||||
propertyName.Substring(0, propertyName.LastIndexOf('_'))
|
||||
);
|
||||
if (entityProperty != null)
|
||||
{
|
||||
propertyExpression = Expression.Property(parameter, entityProperty);
|
||||
comparison = Expression.GreaterThanOrEqual(
|
||||
propertyExpression,
|
||||
Expression.Constant(filterValue, entityProperty.PropertyType)
|
||||
);
|
||||
}
|
||||
}
|
||||
else if (propertyName.EndsWith("_End") || propertyName.EndsWith("_Max"))
|
||||
{
|
||||
entityProperty = typeof(T).GetProperty(
|
||||
propertyName.Substring(0, propertyName.LastIndexOf('_'))
|
||||
);
|
||||
if (entityProperty != null)
|
||||
{
|
||||
propertyExpression = Expression.Property(parameter, entityProperty);
|
||||
comparison = Expression.LessThanOrEqual(
|
||||
propertyExpression,
|
||||
Expression.Constant(filterValue, entityProperty.PropertyType)
|
||||
);
|
||||
}
|
||||
}
|
||||
else if (propertyName.EndsWith("Name", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
entityProperty = typeof(T).GetProperty(propertyName);
|
||||
if (entityProperty != null && entityProperty.PropertyType == typeof(string))
|
||||
{
|
||||
propertyExpression = Expression.Property(parameter, entityProperty);
|
||||
|
||||
// For string Contains operation
|
||||
var containsMethod = typeof(string).GetMethod(
|
||||
"Contains",
|
||||
new[] { typeof(string) }
|
||||
);
|
||||
var filterValueString = filterValue.ToString();
|
||||
|
||||
comparison = Expression.Call(
|
||||
propertyExpression,
|
||||
containsMethod,
|
||||
Expression.Constant(filterValueString, typeof(string))
|
||||
);
|
||||
}
|
||||
else if (entityProperty != null)
|
||||
{
|
||||
// Fall back to equality comparison if it's not a string
|
||||
propertyExpression = Expression.Property(parameter, entityProperty);
|
||||
comparison = Expression.Equal(
|
||||
propertyExpression,
|
||||
Expression.Constant(filterValue, entityProperty.PropertyType)
|
||||
);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
entityProperty = typeof(T).GetProperty(propertyName);
|
||||
if (entityProperty != null)
|
||||
{
|
||||
propertyExpression = Expression.Property(parameter, entityProperty);
|
||||
comparison = Expression.Equal(
|
||||
propertyExpression,
|
||||
Expression.Constant(filterValue, entityProperty.PropertyType)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (comparison != null)
|
||||
{
|
||||
finalExpression =
|
||||
finalExpression == null
|
||||
? comparison
|
||||
: Expression.AndAlso(finalExpression, comparison);
|
||||
}
|
||||
}
|
||||
|
||||
if (finalExpression != null)
|
||||
{
|
||||
var lambda = Expression.Lambda<Func<T, bool>>(finalExpression, parameter);
|
||||
query = query.Where(lambda);
|
||||
}
|
||||
|
||||
return query;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Auth.Core.Utility;
|
||||
|
||||
public static class IEnumerableExtensions
|
||||
{
|
||||
public static IEnumerable<List<T>> Batch<T>(this IEnumerable<T> source, int batchSize)
|
||||
{
|
||||
var batch = new List<T>(batchSize);
|
||||
foreach (var item in source)
|
||||
{
|
||||
batch.Add(item);
|
||||
if (batch.Count == batchSize)
|
||||
{
|
||||
yield return batch;
|
||||
batch = new List<T>(batchSize);
|
||||
}
|
||||
}
|
||||
if (batch.Count > 0)
|
||||
{
|
||||
yield return batch;
|
||||
}
|
||||
}
|
||||
}
|
||||
Executable
+19
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Auth.Contracts.DTOs;
|
||||
using Auth.Contracts;
|
||||
|
||||
namespace Auth.Core.Utility;
|
||||
|
||||
public static class Pagination<T>
|
||||
where T : class
|
||||
{
|
||||
public static IQueryable<T> Paginate(IQueryable<T> query, Pagination pagination)
|
||||
{
|
||||
return query
|
||||
.Skip((pagination.CurrentPage.Value - 1) * pagination.PageSize.Value)
|
||||
.Take(pagination.PageSize.Value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Generic.Contracts.Generics;
|
||||
|
||||
namespace Auth.Core.Utility;
|
||||
|
||||
public static class PaginationExtension
|
||||
{
|
||||
public static IQueryable<T> Paginate<T>(this IQueryable<T> query, Pagination pagination)
|
||||
{
|
||||
if (pagination.CurrentPage.HasValue && pagination.PageSize.HasValue)
|
||||
return query
|
||||
.Skip((pagination.CurrentPage.Value - 1) * pagination.PageSize.Value)
|
||||
.Take(pagination.PageSize.Value);
|
||||
else
|
||||
return query;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Auth.Core.Utility;
|
||||
|
||||
public static class QueryableExtensions
|
||||
{
|
||||
public static IQueryable<T> ApplySorting<T>(this IQueryable<T> source, SortModel sortBy)
|
||||
{
|
||||
// Parameter for lambda expression, e.g., "p" in "p => p.Property"
|
||||
var parameter = Expression.Parameter(typeof(T), "p");
|
||||
|
||||
// Access the property on the parameter, e.g., "p.Property"
|
||||
var property = Expression.Property(parameter, sortBy.SortBy);
|
||||
|
||||
// Cast property access to an object (boxing value types)
|
||||
var converted = Expression.Convert(property, typeof(object));
|
||||
|
||||
// Create the lambda expression, e.g., "p => (object)p.Property"
|
||||
var keySelector = Expression.Lambda<Func<T, object>>(converted, parameter);
|
||||
|
||||
// Apply OrderBy or OrderByDescending based on the ascending flag
|
||||
return sortBy.SortDirection == SortDir.Ascending
|
||||
? source.OrderBy(keySelector)
|
||||
: source.OrderByDescending(keySelector);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user