diff --git a/Kyoo.Common/Controllers/ILibraryManager.cs b/Kyoo.Common/Controllers/ILibraryManager.cs index 2cdbaf50..cd763d84 100644 --- a/Kyoo.Common/Controllers/ILibraryManager.cs +++ b/Kyoo.Common/Controllers/ILibraryManager.cs @@ -65,6 +65,10 @@ namespace Kyoo.Controllers where T : class, IResource where T2 : class; + Task Load([NotNull] T obj, Expression>> member) + where T : class, IResource + where T2 : class; + // Library Items relations Task> GetItemsFromLibrary(int id, Expression> where = null, diff --git a/Kyoo.Common/Controllers/Implementations/ALibraryManager.cs b/Kyoo.Common/Controllers/Implementations/ALibraryManager.cs index 8b276616..d2988241 100644 --- a/Kyoo.Common/Controllers/Implementations/ALibraryManager.cs +++ b/Kyoo.Common/Controllers/Implementations/ALibraryManager.cs @@ -239,9 +239,17 @@ namespace Kyoo.Controllers where T : class, IResource where T2 : class { - return Task.CompletedTask; + // TODO figure out why setting this method as abstract prevent the app from loading this assembly. + throw new NotImplementedException(); } + public virtual Task Load(T obj, Expression>> member) + where T : class, IResource + where T2 : class + { + throw new NotImplementedException(); + } + public Task> GetLibraries(Expression> where = null, Sort sort = default, Pagination page = default) diff --git a/Kyoo.CommonAPI/LibraryManager.cs b/Kyoo.CommonAPI/LibraryManager.cs index a6657be8..5bc2559d 100644 --- a/Kyoo.CommonAPI/LibraryManager.cs +++ b/Kyoo.CommonAPI/LibraryManager.cs @@ -1,10 +1,8 @@ using System; -using System.Collections; using System.Collections.Generic; using System.Linq.Expressions; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.ChangeTracking; namespace Kyoo.Controllers { @@ -39,22 +37,22 @@ namespace Kyoo.Controllers _database = database; } + public override Task Load(T obj, Expression>> member) + { + if (obj == null) + throw new ArgumentNullException(nameof(obj)); + if (!Utility.IsPropertyExpression(member) || member == null) + throw new ArgumentException($"{nameof(member)} is not a property."); + return _database.Entry(obj).Collection(member).LoadAsync(); + } + public override Task Load(T obj, Expression> member) { if (obj == null) throw new ArgumentNullException(nameof(obj)); if (!Utility.IsPropertyExpression(member) || member == null) throw new ArgumentException($"{nameof(member)} is not a property."); - - EntityEntry entry = _database.Entry(obj); - - if (!typeof(IEnumerable).IsAssignableFrom(typeof(T2))) - return entry.Reference(member).LoadAsync(); - - // TODO This is totally the wrong thing. We should run entry.Collection(collectionMember).LoadAsync() - // TODO where collectionMember would be member with T2 replaced by it's inner type (IEnumerable) - Type collectionType = Utility.GetGenericDefinition(typeof(T2), typeof(IEnumerable<>)); - return Utility.RunGenericMethod(entry, "Collection", collectionType, member).LoadAsync(); + return _database.Entry(obj).Reference(member).LoadAsync(); } } } \ No newline at end of file