From bd849894540cc6800a639869fc62b84dc83b6f10 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Wed, 29 Nov 2023 16:14:48 +0100 Subject: [PATCH] Fix episodes comparison --- .../Kyoo.Abstractions/Models/Utils/Filter.cs | 9 +++++++ .../Repositories/LocalRepository.cs | 25 +++++++++++++------ .../Repositories/RepositoryHelper.cs | 2 +- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/back/src/Kyoo.Abstractions/Models/Utils/Filter.cs b/back/src/Kyoo.Abstractions/Models/Utils/Filter.cs index a9952a63..9f54e873 100644 --- a/back/src/Kyoo.Abstractions/Models/Utils/Filter.cs +++ b/back/src/Kyoo.Abstractions/Models/Utils/Filter.cs @@ -152,8 +152,17 @@ public abstract record Filter : Filter private static Parser _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 diff --git a/back/src/Kyoo.Core/Controllers/Repositories/LocalRepository.cs b/back/src/Kyoo.Core/Controllers/Repositories/LocalRepository.cs index 187c9ede..46aff561 100644 --- a/back/src/Kyoo.Core/Controllers/Repositories/LocalRepository.cs +++ b/back/src/Kyoo.Core/Controllers/Repositories/LocalRepository.cs @@ -137,15 +137,24 @@ namespace Kyoo.Core.Controllers BinaryExpression StringCompatibleExpression( Func 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 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 f) { return f switch @@ -153,12 +162,12 @@ namespace Kyoo.Core.Controllers Filter.And(var first, var second) => Expression.AndAlso(Parse(first), Parse(second)), Filter.Or(var first, var second) => Expression.OrElse(Parse(first), Parse(second)), Filter.Not(var inner) => Expression.Not(Parse(inner)), - Filter.Eq(var property, var value) => Expression.Equal(Expression.Property(x, property), Expression.Constant(value)), - Filter.Ne(var property, var value) => Expression.NotEqual(Expression.Property(x, property), Expression.Constant(value)), - Filter.Gt(var property, var value) => StringCompatibleExpression(Expression.GreaterThan, Expression.Property(x, property), Expression.Constant(value)), - Filter.Ge(var property, var value) => StringCompatibleExpression(Expression.GreaterThanOrEqual, Expression.Property(x, property), Expression.Constant(value)), - Filter.Lt(var property, var value) => StringCompatibleExpression(Expression.LessThan, Expression.Property(x, property), Expression.Constant(value)), - Filter.Le(var property, var value) => StringCompatibleExpression(Expression.LessThanOrEqual, Expression.Property(x, property), Expression.Constant(value)), + Filter.Eq(var property, var value) => Exp(Expression.Equal, property, value), + Filter.Ne(var property, var value) => Exp(Expression.NotEqual, property, value), + Filter.Gt(var property, var value) => StringCompatibleExpression(Expression.GreaterThan, property, value), + Filter.Ge(var property, var value) => StringCompatibleExpression(Expression.GreaterThanOrEqual, property, value), + Filter.Lt(var property, var value) => StringCompatibleExpression(Expression.LessThan, property, value), + Filter.Le(var property, var value) => StringCompatibleExpression(Expression.LessThanOrEqual, property, value), Filter.Has(var property, var value) => Expression.Call(typeof(Enumerable), "Contains", new[] { value.GetType() }, Expression.Property(x, property), Expression.Constant(value)), Filter.CmpRandom(var op, var seed, var refId) => CmpRandomHandler(op, seed, refId), Filter.Lambda(var lambda) => ExpressionArgumentReplacer.ReplaceParams(lambda.Body, lambda.Parameters, x), diff --git a/back/src/Kyoo.Core/Controllers/Repositories/RepositoryHelper.cs b/back/src/Kyoo.Core/Controllers/Repositories/RepositoryHelper.cs index 3dd867ef..8637b6b9 100644 --- a/back/src/Kyoo.Core/Controllers/Repositories/RepositoryHelper.cs +++ b/back/src/Kyoo.Core/Controllers/Repositories/RepositoryHelper.cs @@ -54,7 +54,7 @@ public class RepositoryHelper { sort ??= new Sort.Default(); - IEnumerable GetSortsBy(Sort sort) + static IEnumerable GetSortsBy(Sort sort) { return sort switch {