mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-07-09 03:04:20 -04:00
Allowing equals & not equals of resources items
This commit is contained in:
parent
dacfbcd3b2
commit
5b7740f2d9
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using Kyoo.Models;
|
||||||
|
|
||||||
namespace Kyoo.CommonApi
|
namespace Kyoo.CommonApi
|
||||||
{
|
{
|
||||||
@ -50,7 +51,7 @@ namespace Kyoo.CommonApi
|
|||||||
MemberExpression propertyExpr = Expression.Property(param, property);
|
MemberExpression propertyExpr = Expression.Property(param, property);
|
||||||
|
|
||||||
ConstantExpression valueExpr = null;
|
ConstantExpression valueExpr = null;
|
||||||
if (operand != "ctn")
|
if (operand != "ctn" && !typeof(IResource).IsAssignableFrom(propertyExpr.Type))
|
||||||
{
|
{
|
||||||
Type propertyType = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;
|
Type propertyType = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;
|
||||||
object val = string.IsNullOrEmpty(value) || value.Equals("null", StringComparison.OrdinalIgnoreCase)
|
object val = string.IsNullOrEmpty(value) || value.Equals("null", StringComparison.OrdinalIgnoreCase)
|
||||||
@ -61,7 +62,9 @@ namespace Kyoo.CommonApi
|
|||||||
|
|
||||||
Expression condition = operand switch
|
Expression condition = operand switch
|
||||||
{
|
{
|
||||||
"eq" => Expression.Equal(propertyExpr, valueExpr!),
|
"eq" when valueExpr == null => ResourceEqual(propertyExpr, value),
|
||||||
|
"not" when valueExpr == null => ResourceEqual(propertyExpr, value, true),
|
||||||
|
"eq" => Expression.Equal(propertyExpr, valueExpr),
|
||||||
"not" => Expression.NotEqual(propertyExpr, valueExpr!),
|
"not" => Expression.NotEqual(propertyExpr, valueExpr!),
|
||||||
"lt" => StringCompatibleExpression(Expression.LessThan, propertyExpr, valueExpr),
|
"lt" => StringCompatibleExpression(Expression.LessThan, propertyExpr, valueExpr),
|
||||||
"lte" => StringCompatibleExpression(Expression.LessThanOrEqual, propertyExpr, valueExpr),
|
"lte" => StringCompatibleExpression(Expression.LessThanOrEqual, propertyExpr, valueExpr),
|
||||||
@ -81,6 +84,26 @@ namespace Kyoo.CommonApi
|
|||||||
return Expression.Lambda<Func<T, bool>>(expression, param);
|
return Expression.Lambda<Func<T, bool>>(expression, param);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Expression ResourceEqual(Expression parameter, string value, bool notEqual = false)
|
||||||
|
{
|
||||||
|
MemberExpression field;
|
||||||
|
ConstantExpression valueConst;
|
||||||
|
if (int.TryParse(value, out int id))
|
||||||
|
{
|
||||||
|
field = Expression.Property(parameter, "ID");
|
||||||
|
valueConst = Expression.Constant(id);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
field = Expression.Property(parameter, "Slug");
|
||||||
|
valueConst = Expression.Constant(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (notEqual)
|
||||||
|
return Expression.NotEqual(field, valueConst);
|
||||||
|
return Expression.Equal(field, valueConst);
|
||||||
|
}
|
||||||
|
|
||||||
private static Expression ContainsResourceExpression(MemberExpression xProperty, string value)
|
private static Expression ContainsResourceExpression(MemberExpression xProperty, string value)
|
||||||
{
|
{
|
||||||
// x => x.PROPERTY.Any(y => y.Slug == value)
|
// x => x.PROPERTY.Any(y => y.Slug == value)
|
||||||
@ -88,20 +111,7 @@ namespace Kyoo.CommonApi
|
|||||||
ParameterExpression y = Expression.Parameter(xProperty.Type.GenericTypeArguments.First(), "y");
|
ParameterExpression y = Expression.Parameter(xProperty.Type.GenericTypeArguments.First(), "y");
|
||||||
foreach (string val in value.Split(','))
|
foreach (string val in value.Split(','))
|
||||||
{
|
{
|
||||||
MemberExpression yProperty;
|
LambdaExpression lambda = Expression.Lambda(ResourceEqual(y, val), y);
|
||||||
ConstantExpression yValue;
|
|
||||||
if (int.TryParse(val, out int id))
|
|
||||||
{
|
|
||||||
yProperty = Expression.Property(y, "ID");
|
|
||||||
yValue = Expression.Constant(id);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
yProperty = Expression.Property(y, "Slug");
|
|
||||||
yValue = Expression.Constant(val);
|
|
||||||
}
|
|
||||||
|
|
||||||
LambdaExpression lambda = Expression.Lambda(Expression.Equal(yProperty, yValue), y);
|
|
||||||
Expression iteration = Expression.Call(typeof(Enumerable), "Any", xProperty.Type.GenericTypeArguments,
|
Expression iteration = Expression.Call(typeof(Enumerable), "Any", xProperty.Type.GenericTypeArguments,
|
||||||
xProperty, lambda);
|
xProperty, lambda);
|
||||||
|
|
||||||
|
@ -1 +1 @@
|
|||||||
Subproject commit 2e44a86c4b9004012c10d259ae90925adcd66aa7
|
Subproject commit 7c117aaed04d693b12baf6b0061f77de084d56be
|
Loading…
x
Reference in New Issue
Block a user