Implementing a basic query parser for where, sort & pagination

This commit is contained in:
Zoe Roux
2020-06-30 00:12:06 +02:00
parent 3443e102e9
commit ea625fa45b
4 changed files with 95 additions and 7 deletions
+21
View File
@@ -31,6 +31,27 @@ namespace Kyoo.Controllers
Key = key;
Descendant = descendant;
}
public Sort(string sortBy)
{
if (string.IsNullOrEmpty(sortBy))
{
Key = null;
Descendant = false;
return;
}
string key = sortBy.Contains(':') ? sortBy.Substring(0, sortBy.IndexOf(':')) : sortBy;
string order = sortBy.Contains(':') ? sortBy.Substring(sortBy.IndexOf(':') + 1) : null;
Key = Expression.Lambda<Func<T, object>>(Expression.Property(Expression.Parameter(typeof(T), "x"), key));
Descendant = order switch
{
"desc" => true,
"asc" => false,
_ => throw new ArgumentException($"The sort order, if set, should be :asc or :desc but it was :{order}.")
};
}
}
public interface IRepository<T> : IDisposable, IAsyncDisposable