diff --git a/back/src/Kyoo.Abstractions/Models/WatchItem.cs b/back/src/Kyoo.Abstractions/Models/WatchItem.cs index c737839c..15d7ddf5 100644 --- a/back/src/Kyoo.Abstractions/Models/WatchItem.cs +++ b/back/src/Kyoo.Abstractions/Models/WatchItem.cs @@ -184,10 +184,16 @@ namespace Kyoo.Abstractions.Models Fonts = await transcoder.ListFonts(ep), PreviousEpisode = ep.Show.IsMovie ? null - : (await library.GetAll(limit: new Pagination(1, ep.ID, true))).FirstOrDefault(), + : (await library.GetAll( + where: x => x.ShowID == ep.ShowID, + limit: new Pagination(1, ep.ID, true) + )).FirstOrDefault(), NextEpisode = ep.Show.IsMovie ? null - : (await library.GetAll(limit: new Pagination(1, ep.ID))).FirstOrDefault(), + : (await library.GetAll( + where: x => x.ShowID == ep.ShowID, + limit: new Pagination(1, ep.ID) + )).FirstOrDefault(), Chapters = await _GetChapters(ep, fs), IsMovie = ep.Show.IsMovie }; diff --git a/back/src/Kyoo.Core/Controllers/Repositories/LocalRepository.cs b/back/src/Kyoo.Core/Controllers/Repositories/LocalRepository.cs index b5d30f66..4f67fb0c 100644 --- a/back/src/Kyoo.Core/Controllers/Repositories/LocalRepository.cs +++ b/back/src/Kyoo.Core/Controllers/Repositories/LocalRepository.cs @@ -168,6 +168,14 @@ namespace Kyoo.Core.Controllers foreach ((string key, bool desc) in sorts) { BinaryExpression compare = null; + PropertyInfo property = typeof(T).GetProperty(key); + + // Comparing a value with null always return false so we short opt < > comparisons with null. + if (property.GetValue(reference) == null) + { + previousSteps.Add(new(key, desc)); + continue; + } // Create all the equality statements for previous sorts. foreach ((string pKey, bool pDesc) in previousSteps) @@ -186,6 +194,16 @@ namespace Kyoo.Core.Controllers MemberExpression xkey = Expression.Property(x, key); MemberExpression rkey = Expression.Property(referenceC, key); BinaryExpression lastCompare = ApiHelper.StringCompatibleExpression(comparer, xkey, rkey); + + // Comparing a value with null always return false for nulls so we must add nulls to the results manually. + // Postgres sorts them after values so we will do the same + // We only add this condition if the collumn type is nullable + if (Nullable.GetUnderlyingType(property.PropertyType) != null) + { + BinaryExpression equalNull = Expression.Equal(xkey, Expression.Constant(null)); + lastCompare = Expression.OrElse(lastCompare, equalNull); + } + compare = compare != null ? Expression.AndAlso(compare, lastCompare) : lastCompare;