Starting the GetAll of the collection repository

This commit is contained in:
Zoe Roux 2020-06-28 21:56:05 +02:00
parent e21f5d44a8
commit 3eaf4c005a
2 changed files with 25 additions and 19 deletions

View File

@ -8,33 +8,28 @@ using Kyoo.Models;
namespace Kyoo.Controllers namespace Kyoo.Controllers
{ {
public struct Pagination public readonly struct Pagination
{ {
public int Count; public int Count { get; }
public int AfterID; public int AfterID { get; }
public Pagination(int count, int afterID = 0)
{
Count = count;
AfterID = afterID;
}
} }
public readonly struct Sort<T> public readonly struct Sort<T>
{ {
public string Key { get; } public Expression<Func<T, object>> Key { get; }
public bool Descendant { get; } public bool Descendant { get; }
public Sort(string key, bool descendant = false) public Sort(Expression<Func<T, object>> key, bool descendant = false)
{ {
Key = key; Key = key;
Descendant = descendant; Descendant = descendant;
} }
public Sort(Expression<Func<T, object>> key)
{
Key = Utility.GetMemberName(key);
Descendant = false;
}
public static implicit operator Sort<T>([NotNull] Expression<Func<T, object>> key)
{
return new Sort<T>(Utility.GetMemberName(key));
}
} }
public interface IRepository<T> : IDisposable, IAsyncDisposable public interface IRepository<T> : IDisposable, IAsyncDisposable
@ -49,7 +44,8 @@ namespace Kyoo.Controllers
Task<ICollection<T>> GetAll(Expression<Func<T, bool>> where = null, Task<ICollection<T>> GetAll(Expression<Func<T, bool>> where = null,
Expression<Func<T, object>> sort = default, Expression<Func<T, object>> sort = default,
Pagination page = default) => GetAll(where, new Sort<T>(sort), page); Pagination page = default
) => GetAll(where, new Sort<T>(sort), page);
Task<int> Create([NotNull] T obj); Task<int> Create([NotNull] T obj);
Task<int> CreateIfNotExists([NotNull] T obj); Task<int> CreateIfNotExists([NotNull] T obj);

View File

@ -51,7 +51,17 @@ namespace Kyoo.Controllers
Sort<Collection> sort = default, Sort<Collection> sort = default,
Pagination page = default) Pagination page = default)
{ {
return await _database.Collections.ToListAsync(); IQueryable<Collection> query = _database.Collections;
if (where != null)
query = query.Where(where);
Expression<Func<Collection, object>> 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<int> Create(Collection obj) public async Task<int> Create(Collection obj)