Fix episodes comparison

This commit is contained in:
Zoe Roux 2023-11-29 16:14:48 +01:00
parent e0b2e8e937
commit bd84989454
3 changed files with 27 additions and 9 deletions

View File

@ -152,8 +152,17 @@ public abstract record Filter<T> : Filter
private static Parser<object> _GetValueParser(Type type)
{
Type? nullable = Nullable.GetUnderlyingType(type);
if (nullable != null)
{
return
from value in _GetValueParser(nullable)
select value;
}
if (type == typeof(int))
return Parse.Number.Select(x => int.Parse(x) as object);
if (type == typeof(float))
{
return

View File

@ -137,15 +137,24 @@ namespace Kyoo.Core.Controllers
BinaryExpression StringCompatibleExpression(
Func<Expression, Expression, BinaryExpression> operand,
Expression left,
Expression right)
string property,
object value)
{
var left = Expression.Property(x, property);
var right = Expression.Constant(value, ((PropertyInfo)left.Member).PropertyType);
if (left.Type != typeof(string))
return operand(left, right);
MethodCallExpression call = Expression.Call(typeof(string), "Compare", null, left, right);
return operand(call, Expression.Constant(0));
}
Expression Exp(Func<Expression, Expression, BinaryExpression> operand, string property, object? value)
{
var prop = Expression.Property(x, property);
var val = Expression.Constant(value, ((PropertyInfo)prop.Member).PropertyType);
return operand(prop, val);
}
Expression Parse(Filter<T> f)
{
return f switch
@ -153,12 +162,12 @@ namespace Kyoo.Core.Controllers
Filter<T>.And(var first, var second) => Expression.AndAlso(Parse(first), Parse(second)),
Filter<T>.Or(var first, var second) => Expression.OrElse(Parse(first), Parse(second)),
Filter<T>.Not(var inner) => Expression.Not(Parse(inner)),
Filter<T>.Eq(var property, var value) => Expression.Equal(Expression.Property(x, property), Expression.Constant(value)),
Filter<T>.Ne(var property, var value) => Expression.NotEqual(Expression.Property(x, property), Expression.Constant(value)),
Filter<T>.Gt(var property, var value) => StringCompatibleExpression(Expression.GreaterThan, Expression.Property(x, property), Expression.Constant(value)),
Filter<T>.Ge(var property, var value) => StringCompatibleExpression(Expression.GreaterThanOrEqual, Expression.Property(x, property), Expression.Constant(value)),
Filter<T>.Lt(var property, var value) => StringCompatibleExpression(Expression.LessThan, Expression.Property(x, property), Expression.Constant(value)),
Filter<T>.Le(var property, var value) => StringCompatibleExpression(Expression.LessThanOrEqual, Expression.Property(x, property), Expression.Constant(value)),
Filter<T>.Eq(var property, var value) => Exp(Expression.Equal, property, value),
Filter<T>.Ne(var property, var value) => Exp(Expression.NotEqual, property, value),
Filter<T>.Gt(var property, var value) => StringCompatibleExpression(Expression.GreaterThan, property, value),
Filter<T>.Ge(var property, var value) => StringCompatibleExpression(Expression.GreaterThanOrEqual, property, value),
Filter<T>.Lt(var property, var value) => StringCompatibleExpression(Expression.LessThan, property, value),
Filter<T>.Le(var property, var value) => StringCompatibleExpression(Expression.LessThanOrEqual, property, value),
Filter<T>.Has(var property, var value) => Expression.Call(typeof(Enumerable), "Contains", new[] { value.GetType() }, Expression.Property(x, property), Expression.Constant(value)),
Filter<T>.CmpRandom(var op, var seed, var refId) => CmpRandomHandler(op, seed, refId),
Filter<T>.Lambda(var lambda) => ExpressionArgumentReplacer.ReplaceParams(lambda.Body, lambda.Parameters, x),

View File

@ -54,7 +54,7 @@ public class RepositoryHelper
{
sort ??= new Sort<T>.Default();
IEnumerable<SortIndicator> GetSortsBy(Sort<T> sort)
static IEnumerable<SortIndicator> GetSortsBy(Sort<T> sort)
{
return sort switch
{