(refactor): Paged response code review

This commit is contained in:
Maksym Sadovnychyy 2024-10-17 19:32:13 +02:00
parent d8eacdd32a
commit 346b891a5f
2 changed files with 60 additions and 66 deletions

View File

@ -8,7 +8,7 @@
<!-- NuGet package metadata --> <!-- NuGet package metadata -->
<PackageId>MaksIT.Core</PackageId> <PackageId>MaksIT.Core</PackageId>
<Version>1.1.1</Version> <Version>1.1.2</Version>
<Authors>Maksym Sadovnychyy</Authors> <Authors>Maksym Sadovnychyy</Authors>
<Company>MAKS-IT</Company> <Company>MAKS-IT</Company>
<Product>MaksIT.Core</Product> <Product>MaksIT.Core</Product>

View File

@ -1,65 +1,57 @@
using System.Linq.Expressions; using System.Linq.Expressions;
using MaksIT.Core.Extensions;
using MaksIT.Core.Abstractions.Webapi; using MaksIT.Core.Abstractions.Webapi;
namespace MaksIT.Core.Webapi.Models; namespace MaksIT.Core.Webapi.Models;
public class PagedRequest : RequestModelBase { public class PagedRequest : RequestModelBase {
public int PageSize { get; set; } = 100; public int PageSize { get; set; } = 100;
public int PageNumber { get; set; } = 1; public int PageNumber { get; set; } = 1;
// Global filter applicable to any collection
public string? Filters { get; set; } public string? Filters { get; set; }
// Specific filters for different collections
public Dictionary<string, string>? CollectionFilters { get; set; } public Dictionary<string, string>? CollectionFilters { get; set; }
// Optional sorting
public string? SortBy { get; set; } public string? SortBy { get; set; }
public bool IsAscending { get; set; } = true; public bool IsAscending { get; set; } = true;
// Method to build an expression for a specific collection, combining global and collection-specific filters public Expression<Func<T, bool>>? BuildCollectionFilterExpression<T>(string collectionName) {
public Expression<Func<T, bool>> BuildCollectionFilterExpression<T>(string collectionName) { Expression<Func<T, bool>>? globalFilterExpression = null;
var expressions = new List<Expression>(); Expression<Func<T, bool>>? collectionFilterExpression = null;
var parameter = Expression.Parameter(typeof(T), "x");
// Add global filters if available
if (!string.IsNullOrEmpty(Filters)) { if (!string.IsNullOrEmpty(Filters)) {
var globalFilterExpression = BuildFilterExpression<T>(Filters, parameter); globalFilterExpression = BuildFilterExpression<T>(Filters);
expressions.Add(globalFilterExpression);
} }
// Add collection-specific filters if available if (CollectionFilters != null && CollectionFilters.TryGetValue(collectionName, out var collectionFilter) && !string.IsNullOrEmpty(collectionFilter)) {
if (CollectionFilters != null && CollectionFilters.ContainsKey(collectionName) && !string.IsNullOrEmpty(CollectionFilters[collectionName])) { collectionFilterExpression = BuildFilterExpression<T>(collectionFilter);
var collectionFilterExpression = BuildFilterExpression<T>(CollectionFilters[collectionName], parameter);
expressions.Add(collectionFilterExpression);
} }
// Combine the expressions using AND (you can extend to OR logic if needed) if (globalFilterExpression == null && collectionFilterExpression == null) {
Expression combinedExpression; return null;
if (expressions.Any()) {
combinedExpression = expressions.Aggregate(Expression.AndAlso);
}
else {
// Default to 'true' if no filters are provided
combinedExpression = Expression.Constant(true);
} }
return Expression.Lambda<Func<T, bool>>(combinedExpression, parameter); if (globalFilterExpression != null && collectionFilterExpression != null) {
return globalFilterExpression.CombineWith(collectionFilterExpression);
}
return globalFilterExpression ?? collectionFilterExpression;
} }
// Method to build a LINQ expression from a single filter string (global or collection-specific) public Expression<Func<T, bool>>? BuildFilterExpression<T>(string filter) {
public Expression<Func<T, bool>> BuildFilterExpression<T>(string filter, ParameterExpression? parameter = null) { if (string.IsNullOrEmpty(filter)) {
if (string.IsNullOrEmpty(filter)) return null;
return x => true; // Default to 'true' if no filters are provided }
parameter ??= Expression.Parameter(typeof(T), "x"); var parameter = Expression.Parameter(typeof(T), "x");
var expressions = new List<Expression>(); var expressions = new List<Expression>();
// Parse the filters string (this logic can be extended to support more complex filtering)
var filters = filter.Split(new[] { "AND", "OR" }, StringSplitOptions.None); var filters = filter.Split(new[] { "AND", "OR" }, StringSplitOptions.None);
foreach (var subFilter in filters) { foreach (var subFilter in filters) {
Expression? expression = null;
if (subFilter.Contains('=')) { if (subFilter.Contains('=')) {
var parts = subFilter.Split('='); var parts = subFilter.Split('=');
var propertyName = parts[0].Trim(); var propertyName = parts[0].Trim();
@ -68,51 +60,53 @@ public class PagedRequest : RequestModelBase {
var property = Expression.Property(parameter, propertyName); var property = Expression.Property(parameter, propertyName);
var constant = Expression.Constant(ConvertValue(property.Type, value)); var constant = Expression.Constant(ConvertValue(property.Type, value));
expressions.Add(Expression.Equal(property, constant)); expression = Expression.Equal(property, constant);
} }
else if (subFilter.Contains('>') || subFilter.Contains('<')) { else if (subFilter.Contains('>') || subFilter.Contains('<')) {
var comparisonType = subFilter.Contains(">=") ? ">=" : expression = BuildComparisonExpression(subFilter, parameter);
subFilter.Contains("<=") ? "<=" : }
subFilter.Contains(">") ? ">" : "<";
var parts = subFilter.Split(new[] { '>', '<', '=', ' ' }, StringSplitOptions.RemoveEmptyEntries);
var propertyName = parts[0].Trim();
var value = parts[1].Trim().Replace("'", "");
var property = Expression.Property(parameter, propertyName); if (expression != null) {
var constant = Expression.Constant(ConvertValue(property.Type, value)); expressions.Add(expression);
switch (comparisonType) {
case ">":
expressions.Add(Expression.GreaterThan(property, constant));
break;
case "<":
expressions.Add(Expression.LessThan(property, constant));
break;
case ">=":
expressions.Add(Expression.GreaterThanOrEqual(property, constant));
break;
case "<=":
expressions.Add(Expression.LessThanOrEqual(property, constant));
break;
}
} }
} }
// Combine the expressions using AND (you can extend to support OR) if (!expressions.Any()) {
return null;
}
var combinedExpression = expressions.Aggregate(Expression.AndAlso); var combinedExpression = expressions.Aggregate(Expression.AndAlso);
return Expression.Lambda<Func<T, bool>>(combinedExpression, parameter); return Expression.Lambda<Func<T, bool>>(combinedExpression, parameter);
} }
// Helper method to convert the value to the correct type private static Expression? BuildComparisonExpression(string subFilter, ParameterExpression parameter) {
private static object ConvertValue(Type type, string value) { var comparisonType = subFilter.Contains(">=") ? ">=" :
if (type == typeof(int)) subFilter.Contains("<=") ? "<=" :
return int.Parse(value); subFilter.Contains(">") ? ">" : "<";
if (type == typeof(bool))
return bool.Parse(value);
if (type == typeof(DateTime))
return DateTime.Parse(value);
return value; // Default to string var parts = subFilter.Split(new[] { '>', '<', '=', ' ' }, StringSplitOptions.RemoveEmptyEntries);
var propertyName = parts[0].Trim();
var value = parts[1].Trim().Replace("'", "");
var property = Expression.Property(parameter, propertyName);
var constant = Expression.Constant(ConvertValue(property.Type, value));
return comparisonType switch {
">" => Expression.GreaterThan(property, constant),
"<" => Expression.LessThan(property, constant),
">=" => Expression.GreaterThanOrEqual(property, constant),
"<=" => Expression.LessThanOrEqual(property, constant),
_ => null
};
}
private static object ConvertValue(Type type, string value) {
return type switch {
var t when t == typeof(int) => int.Parse(value),
var t when t == typeof(bool) => bool.Parse(value),
var t when t == typeof(DateTime) => DateTime.Parse(value),
_ => value
};
} }
} }