Fixing the sort by

This commit is contained in:
Zoe Roux 2020-07-06 20:38:34 +02:00
parent 4357d599e4
commit 63a85124b7

View File

@ -31,7 +31,10 @@ namespace Kyoo.Controllers
Key = key; Key = key;
Descendant = descendant; Descendant = descendant;
if (!(Key.Body is MemberExpression)) if (Key.Body is MemberExpression ||
Key.Body.NodeType == ExpressionType.Convert && ((UnaryExpression)Key.Body).Operand is MemberExpression)
return;
throw new ArgumentException("The given sort key is not valid."); throw new ArgumentException("The given sort key is not valid.");
} }
@ -48,12 +51,16 @@ namespace Kyoo.Controllers
string order = sortBy.Contains(':') ? sortBy.Substring(sortBy.IndexOf(':') + 1) : null; string order = sortBy.Contains(':') ? sortBy.Substring(sortBy.IndexOf(':') + 1) : null;
ParameterExpression param = Expression.Parameter(typeof(T), "x"); ParameterExpression param = Expression.Parameter(typeof(T), "x");
Key = Expression.Lambda<Func<T, object>>(Expression.Property(param, key), param); MemberExpression property = Expression.Property(param, key);
Key = property.Type.IsValueType
? Expression.Lambda<Func<T, object>>(Expression.Convert(property, typeof(object)), param)
: Expression.Lambda<Func<T, object>>(property, param);
Descendant = order switch Descendant = order switch
{ {
"desc" => true, "desc" => true,
"asc" => false, "asc" => false,
"" => false, null => false,
_ => throw new ArgumentException($"The sort order, if set, should be :asc or :desc but it was :{order}.") _ => throw new ArgumentException($"The sort order, if set, should be :asc or :desc but it was :{order}.")
}; };
} }