diff --git a/Kyoo.Common/Controllers/IRepository.cs b/Kyoo.Common/Controllers/IRepository.cs index caef30a6..22f47a16 100644 --- a/Kyoo.Common/Controllers/IRepository.cs +++ b/Kyoo.Common/Controllers/IRepository.cs @@ -1,18 +1,56 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Linq.Expressions; using System.Threading.Tasks; using JetBrains.Annotations; using Kyoo.Models; namespace Kyoo.Controllers { + public struct Pagination + { + public int Count; + public int AfterID; + } + + public readonly struct Sort + { + public string Key { get; } + public bool Descendant { get; } + + public Sort(string 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 { Task Get(int id); Task Get(string slug); Task> Search(string query); - Task> GetAll(); + + Task> GetAll(Expression> where = null, + Sort sort = default, + Pagination page = default); + + Task> GetAll(Expression> where = null, + Expression> sort = default, + Pagination page = default) => GetAll(where, new Sort(sort), page); + Task Create([NotNull] T obj); Task CreateIfNotExists([NotNull] T obj); Task Edit([NotNull] T edited, bool resetOld); diff --git a/Kyoo.Common/Models/WatchItem.cs b/Kyoo.Common/Models/WatchItem.cs index 83b987f6..141b139c 100644 --- a/Kyoo.Common/Models/WatchItem.cs +++ b/Kyoo.Common/Models/WatchItem.cs @@ -101,7 +101,7 @@ namespace Kyoo.Models if (EpisodeNumber >= episode.Season.Episodes.Count()) { NextEpisode = episode.Show.Seasons - .FirstOrDefault(x => x.SeasonNumber == SeasonNumber - 1)?.Episodes + .FirstOrDefault(x => x.SeasonNumber == SeasonNumber + 1)?.Episodes .FirstOrDefault(x => x.EpisodeNumber == 1); } else diff --git a/Kyoo.Common/Utility.cs b/Kyoo.Common/Utility.cs index a852c6f8..23313077 100644 --- a/Kyoo.Common/Utility.cs +++ b/Kyoo.Common/Utility.cs @@ -3,6 +3,7 @@ using System.Collections; using System.Collections.Generic; using System.Globalization; using System.Linq; +using System.Linq.Expressions; using System.Reflection; using System.Text; using System.Text.RegularExpressions; @@ -224,5 +225,16 @@ namespace Kyoo yield return ret; } } + + public static string GetMemberName(Expression> key) + { + if (key == null) + throw new ArgumentNullException(nameof(key)); + + if (!(key.Body is MemberExpression member)) + throw new ArgumentException("Key should be a member of the object."); + + return member.Member.Name; + } } } \ No newline at end of file diff --git a/Kyoo/Controllers/LibraryManager.cs b/Kyoo/Controllers/LibraryManager.cs index 9b1a6590..8092f5c6 100644 --- a/Kyoo/Controllers/LibraryManager.cs +++ b/Kyoo/Controllers/LibraryManager.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq.Expressions; using System.Threading.Tasks; using Kyoo.Models; @@ -363,7 +364,7 @@ namespace Kyoo.Controllers return _shows.Delete(show); } - public Task DeleteSeason(Season season) + public Task DeleteSeason(Season season, IShowRepository repo) { return _seasons.Delete(season); } diff --git a/Kyoo/Controllers/Repositories/CollectionRepository.cs b/Kyoo/Controllers/Repositories/CollectionRepository.cs index 776cc597..d183d3d6 100644 --- a/Kyoo/Controllers/Repositories/CollectionRepository.cs +++ b/Kyoo/Controllers/Repositories/CollectionRepository.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Linq.Expressions; using System.Threading.Tasks; using Kyoo.Models; using Kyoo.Models.Exceptions; @@ -46,7 +47,9 @@ namespace Kyoo.Controllers .ToListAsync(); } - public async Task> GetAll() + public async Task> GetAll(Expression> where = null, + Sort sort = default, + Pagination page = default) { return await _database.Collections.ToListAsync(); }