Skip to content

Commit

Permalink
Merge pull request #11 from RicardoZambon/features/filters
Browse files Browse the repository at this point in the history
feat: Review filters extension methods
  • Loading branch information
RicardoZambon authored Apr 23, 2024
2 parents 8ec9d6b + 4fad065 commit 5e56632
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 12 deletions.
10 changes: 5 additions & 5 deletions ZWebAPI/ExtensionMethods/QueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public static IQueryable<TEntity> TryFilter<TEntity>(this IQueryable<TEntity> qu

if (GetProperty(parameter, propertyName) is MemberExpression property)
{
ConstantExpression value = Expression.Constant(propertyValue);
UnaryExpression value = Expression.Convert(Expression.Constant(propertyValue), property.Type);

return BuildLambda<TEntity>(Expression.Equal(property, value), parameter);
}
Expand All @@ -176,7 +176,7 @@ public static IQueryable<TEntity> TryFilter<TEntity>(this IQueryable<TEntity> qu

if (GetProperty(parameter, propertyName) is MemberExpression property)
{
ConstantExpression value = Expression.Constant(propertyValue);
UnaryExpression value = Expression.Convert(Expression.Constant(propertyValue), property.Type);

return BuildLambda<TEntity>(Expression.GreaterThan(property, value), parameter);
}
Expand All @@ -189,7 +189,7 @@ public static IQueryable<TEntity> TryFilter<TEntity>(this IQueryable<TEntity> qu

if (GetProperty(parameter, propertyName) is MemberExpression property)
{
ConstantExpression value = Expression.Constant(propertyValue);
UnaryExpression value = Expression.Convert(Expression.Constant(propertyValue), property.Type);

return BuildLambda<TEntity>(Expression.GreaterThanOrEqual(property, value), parameter);
}
Expand All @@ -205,7 +205,7 @@ public static IQueryable<TEntity> TryFilter<TEntity>(this IQueryable<TEntity> qu

if (GetProperty(parameter, propertyName) is MemberExpression property)
{
ConstantExpression value = Expression.Constant(propertyValue);
UnaryExpression value = Expression.Convert(Expression.Constant(propertyValue), property.Type);

return BuildLambda<TEntity>(Expression.LessThan(property, value), parameter);
}
Expand All @@ -218,7 +218,7 @@ public static IQueryable<TEntity> TryFilter<TEntity>(this IQueryable<TEntity> qu

if (GetProperty(parameter, propertyName) is MemberExpression property)
{
ConstantExpression value = Expression.Constant(propertyValue);
UnaryExpression value = Expression.Convert(Expression.Constant(propertyValue), property.Type);

return BuildLambda<TEntity>(Expression.LessThanOrEqual(property, value), parameter);
}
Expand Down
72 changes: 65 additions & 7 deletions ZWebAPI/ExtensionMethods/SummaryParametersExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal static class SummaryParametersExtensions
/// <returns>
/// <c>true</c> if the specified summary parameters has filters; otherwise, <c>false</c>.
/// </returns>
internal static bool HasFilters(this ISummaryParameters parameters)
public static bool HasFilters(this ISummaryParameters parameters)
{
return parameters?.Filters?.Any() ?? false;
}
Expand All @@ -28,9 +28,9 @@ internal static bool HasFilters(this ISummaryParameters parameters)
/// <returns>
/// <c>true</c> if the specified summary parameter has the property filter; otherwise, <c>false</c>.
/// </returns>
internal static bool HasFilter(this ISummaryParameters parameters, string property)
public static bool HasFilter(this ISummaryParameters parameters, string property)
{
return parameters?.Filters?.Keys.Any(x => string.Equals(x, property, StringComparison.OrdinalIgnoreCase)) ?? false;
return parameters?.Filters?.Keys?.Any(x => string.Equals(x, property, StringComparison.OrdinalIgnoreCase)) ?? false;
}

/// <summary>
Expand All @@ -40,7 +40,8 @@ internal static bool HasFilter(this ISummaryParameters parameters, string proper
/// <param name="parameters">The parameters.</param>
/// <param name="property">The property.</param>
/// <returns>The filter value.</returns>
internal static TResult? GetFilterValue<TResult>(this ISummaryParameters parameters, string property) where TResult : struct
public static TResult? GetFilterValue<TResult>(this ISummaryParameters parameters, string property)
where TResult : struct
{
if (parameters.GetFilterValue(property, typeof(TResult)) is object result)
{
Expand All @@ -56,7 +57,7 @@ internal static bool HasFilter(this ISummaryParameters parameters, string proper
/// <param name="property">The property.</param>
/// <param name="resultType">Type of the result.</param>
/// <returns>The filter value.</returns>
internal static object? GetFilterValue(this ISummaryParameters parameters, string property, Type resultType)
public static object? GetFilterValue(this ISummaryParameters parameters, string property, Type resultType)
{
KeyValuePair<string, object>? filter = parameters.Filters?.FirstOrDefault(x => string.Equals(x.Key, property, StringComparison.OrdinalIgnoreCase));

Expand All @@ -70,15 +71,58 @@ internal static bool HasFilter(this ISummaryParameters parameters, string proper
case JsonValueKind.Null:
return null;
case JsonValueKind.Number:
return jsonValue.GetInt64();
if (resultType == typeof(byte))
{
return jsonValue.GetByte();
}
else if (resultType == typeof(decimal))
{
return jsonValue.GetDecimal();
}
else if (resultType == typeof(double))
{
return jsonValue.GetDouble();
}
else if (resultType == typeof(float))
{
return jsonValue.GetSingle();
}
else if (resultType == typeof(long))
{
return jsonValue.GetInt64();
}
else if (resultType == typeof(sbyte))
{
return jsonValue.GetSByte();
}
else if (resultType == typeof(short))
{
return jsonValue.GetInt16();
}
else if (resultType == typeof(uint))
{
return jsonValue.GetUInt32();
}
else if (resultType == typeof(ulong))
{
return jsonValue.GetUInt64();
}
else if (resultType == typeof(ushort))
{
return jsonValue.GetUInt16();
}
return jsonValue.GetInt32();

case JsonValueKind.String:
return jsonValue.GetString();
return jsonValue.Deserialize(resultType);

default:
object rawValue = jsonValue.GetRawText();
if (rawValue is null)
{
return null;
}

if (resultType.IsEnum)
{
return Enum.Parse(resultType, rawValue.ToString() ?? string.Empty);
Expand All @@ -88,5 +132,19 @@ internal static bool HasFilter(this ISummaryParameters parameters, string proper
}
return null;
}

/// <summary>
/// Updates the filter value.
/// </summary>
/// <param name="parameters">The parameters.</param>
/// <param name="property">The property.</param>
/// <param name="value">The value.</param>
public static void UpdateFilterValue(this ISummaryParameters parameters, string property, object value)
{
if (parameters?.Filters?.Keys?.FirstOrDefault(x => string.Equals(x, property, StringComparison.OrdinalIgnoreCase)) is string key)
{
parameters.Filters[key] = JsonDocument.Parse(JsonSerializer.Serialize(value)).RootElement;
}
}
}
}

0 comments on commit 5e56632

Please sign in to comment.