diff --git a/Kyoo.Common/Controllers/IRepository.cs b/Kyoo.Common/Controllers/IRepository.cs index 22f47a16..e401bca3 100644 --- a/Kyoo.Common/Controllers/IRepository.cs +++ b/Kyoo.Common/Controllers/IRepository.cs @@ -8,33 +8,28 @@ using Kyoo.Models; namespace Kyoo.Controllers { - public struct Pagination + public readonly struct Pagination { - public int Count; - public int AfterID; + public int Count { get; } + public int AfterID { get; } + + public Pagination(int count, int afterID = 0) + { + Count = count; + AfterID = afterID; + } } public readonly struct Sort { - public string Key { get; } + public Expression> Key { get; } public bool Descendant { get; } - - public Sort(string key, bool descendant = false) + + public Sort(Expression> key, bool descendant = false) { Key = key; Descendant = descendant; } - - public Sort(Expression> key) - { - Key = Utility.GetMemberName(key); - Descendant = false; - } - - public static implicit operator Sort([NotNull] Expression> key) - { - return new Sort(Utility.GetMemberName(key)); - } } public interface IRepository : IDisposable, IAsyncDisposable @@ -49,7 +44,8 @@ namespace Kyoo.Controllers Task> GetAll(Expression> where = null, Expression> sort = default, - Pagination page = default) => GetAll(where, new Sort(sort), page); + Pagination page = default + ) => GetAll(where, new Sort(sort), page); Task Create([NotNull] T obj); Task CreateIfNotExists([NotNull] T obj); diff --git a/Kyoo/Controllers/Repositories/CollectionRepository.cs b/Kyoo/Controllers/Repositories/CollectionRepository.cs index d183d3d6..e94c34b8 100644 --- a/Kyoo/Controllers/Repositories/CollectionRepository.cs +++ b/Kyoo/Controllers/Repositories/CollectionRepository.cs @@ -51,7 +51,17 @@ namespace Kyoo.Controllers Sort sort = default, Pagination page = default) { - return await _database.Collections.ToListAsync(); + IQueryable query = _database.Collections; + + if (where != null) + query = query.Where(where); + + Expression> sortOrder = sort.Key ?? (x => x.Name); + query = sort.Descendant ? query.OrderByDescending(sortOrder) : query.OrderBy(sortOrder); + + query.Where(x => x.ID ) + + return await query.ToListAsync(); } public async Task Create(Collection obj)