diff --git a/Kyoo.Common/Controllers/Implementations/LibraryManager.cs b/Kyoo.Common/Controllers/Implementations/LibraryManager.cs index 640ed956..306e9bd7 100644 --- a/Kyoo.Common/Controllers/Implementations/LibraryManager.cs +++ b/Kyoo.Common/Controllers/Implementations/LibraryManager.cs @@ -260,47 +260,49 @@ namespace Kyoo.Controllers await Load(obj as IResource, member); return obj; } - + public Task Load(IResource obj, string member) { return (obj, member) switch { (Library l, nameof(Library.Providers)) => ProviderRepository - .GetAll(Utility.ResourceEquals(obj)) + .GetAll(x => x.Libraries.Any(y => y.ID == obj.ID || y.Slug == obj.Slug)) .Then(x => l.Providers = x), - (Library l, nameof(Library.Shows)) => ShowRepository.GetAll(Utility.ResourceEquals(obj)) + (Library l, nameof(Library.Shows)) => ShowRepository + .GetAll(x => x.Libraries.Any(y => y.ID == obj.ID || y.Slug == obj.Slug)) .Then(x => l.Shows = x), (Library l, nameof(Library.Collections)) => CollectionRepository - .GetAll(Utility.ResourceEquals(obj)) + .GetAll(x => x.Libraries.Any(y => y.ID == obj.ID || y.Slug == obj.Slug)) .Then(x => l.Collections = x), (Collection c, nameof(Library.Shows)) => ShowRepository - .GetAll(Utility.ResourceEquals(obj)) + .GetAll(x => x.Collections.Any(y => y.ID == obj.ID || y.Slug == obj.Slug)) .Then(x => c.Shows = x), (Collection c, nameof(Collection.Libraries)) => LibraryRepository - .GetAll(Utility.ResourceEquals(obj)) + .GetAll(x => x.Collections.Any(y => y.ID == obj.ID || y.Slug == obj.Slug)) .Then(x => c.Libraries = x), (Show s, nameof(Show.ExternalIDs)) => ProviderRepository .GetMetadataID(x => x.ShowID == obj.ID) .Then(x => s.ExternalIDs = x), (Show s, nameof(Show.Genres)) => GenreRepository - .GetAll(Utility.ResourceEquals(obj)) + .GetAll(x => x.Shows.Any(y => y.ID == obj.ID || y.Slug == obj.Slug)) .Then(x => s.Genres = x), (Show s, nameof(Show.People)) => ( PeopleRepository.GetFromShow((dynamic)(obj.ID > 0 ? obj.ID : obj.Slug)) - as Task>).Then(x => s.People = x), + as Task>) + .Then(x => s.People = x), (Show s, nameof(Show.Seasons)) => SeasonRepository - .GetAll(Utility.ResourceEquals(obj)) + .GetAll(x => x.Show.ID == obj.ID || x.Show.Slug == obj.Slug) .Then(x => s.Seasons = x), (Show s, nameof(Show.Episodes)) => EpisodeRepository - .GetAll(Utility.ResourceEquals(obj)) + .GetAll(x => x.Show.ID == obj.ID || x.Show.Slug == obj.Slug) .Then(x => s.Episodes = x), (Show s, nameof(Show.Libraries)) => LibraryRepository - .GetAll(Utility.ResourceEquals(obj)) + .GetAll(x => x.Shows.Any(y => y.ID == obj.ID || y.Slug == obj.Slug)) .Then(x => s.Libraries = x), (Show s, nameof(Show.Collections)) => CollectionRepository - .GetAll(Utility.ResourceEquals(obj)) + .GetAll(x => x.Shows.Any(y => y.ID == obj.ID || y.Slug == obj.Slug)) .Then(x => s.Collections = x), // TODO Studio loading does not work. (Show s, nameof(Show.Studio)) => StudioRepository @@ -311,33 +313,37 @@ namespace Kyoo.Controllers .GetMetadataID(x => x.SeasonID == obj.ID) .Then(x => s.ExternalIDs = x), (Season s, nameof(Season.Episodes)) => EpisodeRepository - .GetAll(Utility.ResourceEquals(obj)) + .GetAll(x => x.Season.ID == obj.ID || x.Season.Slug == obj.Slug) .Then(x => s.Episodes = x), (Season s, nameof(Season.Show)) => ShowRepository - .Get(Utility.ResourceEquals(obj)).Then(x => s.Show = x), + .Get(x => x.Seasons + .Any(y => y.ID == obj.ID || y.ShowID == s.ShowID && y.SeasonNumber == s.SeasonNumber)) + .Then(x => s.Show = x), + // TODO maybe add slug support for episodes (Episode e, nameof(Episode.ExternalIDs)) => ProviderRepository .GetMetadataID(x => x.EpisodeID == obj.ID) .Then(x => e.ExternalIDs = x), (Episode e, nameof(Episode.Tracks)) => TrackRepository - .GetAll(Utility.ResourceEquals(obj)) + .GetAll(x => x.Episode.ID == obj.ID) .Then(x => e.Tracks = x), (Episode e, nameof(Episode.Show)) => ShowRepository - .Get(Utility.ResourceEquals(obj)).Then(x => e.Show = x), + .Get(x => x.Episodes.Any(y => y.ID == obj.ID)) + .Then(x => e.Show = x), (Episode e, nameof(Episode.Season)) => SeasonRepository - .Get(Utility.ResourceEquals(obj)) + .Get(x => x.Episodes.Any(y => y.ID == e.ID)) .Then(x => e.Season = x), (Track t, nameof(Track.Episode)) => EpisodeRepository - .Get(Utility.ResourceEquals(obj)) + .Get(x => x.Tracks.Any(y => y.ID == obj.ID)) .Then(x => t.Episode = x), (Genre g, nameof(Genre.Shows)) => ShowRepository - .GetAll(Utility.ResourceEquals(obj)) + .GetAll(x => x.Genres.Any(y => y.ID == obj.ID || y.Slug == obj.Slug)) .Then(x => g.Shows = x), (Studio s, nameof(Studio.Shows)) => ShowRepository - .GetAll(Utility.ResourceEquals(obj)) + .GetAll(x => x.Studio.ID == obj.ID || x.Studio.Slug == obj.Slug) .Then(x => s.Shows = x), (People p, nameof(People.ExternalIDs)) => ProviderRepository diff --git a/Kyoo.Common/Models/Resources/Library.cs b/Kyoo.Common/Models/Resources/Library.cs index 1556753b..54f9ac89 100644 --- a/Kyoo.Common/Models/Resources/Library.cs +++ b/Kyoo.Common/Models/Resources/Library.cs @@ -11,7 +11,7 @@ namespace Kyoo.Models public string Name { get; set; } public string[] Paths { get; set; } - [EditableRelation] public virtual ICollection Providers { get; set; } + [EditableRelation] [LoadableRelation] public virtual ICollection Providers { get; set; } [LoadableRelation] public virtual ICollection Shows { get; set; } [LoadableRelation] public virtual ICollection Collections { get; set; } diff --git a/Kyoo.Common/Models/Resources/ProviderID.cs b/Kyoo.Common/Models/Resources/ProviderID.cs index 411bf976..bb37b395 100644 --- a/Kyoo.Common/Models/Resources/ProviderID.cs +++ b/Kyoo.Common/Models/Resources/ProviderID.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using Kyoo.Models.Attributes; namespace Kyoo.Models @@ -8,6 +9,8 @@ namespace Kyoo.Models public string Slug { get; set; } public string Name { get; set; } public string Logo { get; set; } + + [LoadableRelation] public ICollection Libraries { get; set; } public ProviderID() { } diff --git a/Kyoo.CommonAPI/ResourceViewAttribute.cs b/Kyoo.CommonAPI/ResourceViewAttribute.cs index 565c1e4b..7a60da55 100644 --- a/Kyoo.CommonAPI/ResourceViewAttribute.cs +++ b/Kyoo.CommonAPI/ResourceViewAttribute.cs @@ -24,9 +24,9 @@ namespace Kyoo.CommonApi where.Remove(key); } - string[] fields = string.Join(',', context.HttpContext.Request.Query["fields"]).Split(','); - - context.HttpContext.Items["fields"] = fields; + string[] fields = context.HttpContext.Request.Query["fields"] + .SelectMany(x => x.Split(',')) + .ToArray(); if (context.ActionDescriptor is ControllerActionDescriptor descriptor) { Type type = descriptor.MethodInfo.ReturnType; @@ -37,17 +37,24 @@ namespace Kyoo.CommonApi PropertyInfo[] properties = type.GetProperties() .Where(x => x.GetCustomAttribute() != null) .ToArray(); - foreach (string field in fields) - { - if (properties.Any(y => string.Equals(y.Name,field, StringComparison.InvariantCultureIgnoreCase))) - continue; - context.Result = new BadRequestObjectResult(new + fields = fields.Select(x => { - Error = $"{field} does not exist on {type.Name}." - }); + string property = properties + .FirstOrDefault(y => string.Equals(x, y.Name, StringComparison.InvariantCultureIgnoreCase)) + ?.Name; + if (property != null) + return property; + context.Result = new BadRequestObjectResult(new + { + Error = $"{x} does not exist on {type.Name}." + }); + return null; + }) + .ToArray(); + if (context.Result != null) return; - } } + context.HttpContext.Items["fields"] = fields; base.OnActionExecuting(context); } diff --git a/Kyoo/Models/DatabaseMigrations/Internal/20210216202218_Initial.Designer.cs b/Kyoo/Models/DatabaseMigrations/Internal/20210228210101_Initial.Designer.cs similarity index 99% rename from Kyoo/Models/DatabaseMigrations/Internal/20210216202218_Initial.Designer.cs rename to Kyoo/Models/DatabaseMigrations/Internal/20210228210101_Initial.Designer.cs index ece1dce0..ccd23f6a 100644 --- a/Kyoo/Models/DatabaseMigrations/Internal/20210216202218_Initial.Designer.cs +++ b/Kyoo/Models/DatabaseMigrations/Internal/20210228210101_Initial.Designer.cs @@ -7,10 +7,10 @@ using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; -namespace Kyoo.Kyoo.Models.DatabaseMigrations.Internal +namespace Kyoo.Models.DatabaseMigrations.Internal { [DbContext(typeof(DatabaseContext))] - [Migration("20210216202218_Initial")] + [Migration("20210228210101_Initial")] partial class Initial { protected override void BuildTargetModel(ModelBuilder modelBuilder) diff --git a/Kyoo/Models/DatabaseMigrations/Internal/20210216202218_Initial.cs b/Kyoo/Models/DatabaseMigrations/Internal/20210228210101_Initial.cs similarity index 99% rename from Kyoo/Models/DatabaseMigrations/Internal/20210216202218_Initial.cs rename to Kyoo/Models/DatabaseMigrations/Internal/20210228210101_Initial.cs index e15d9e45..8a57b1cf 100644 --- a/Kyoo/Models/DatabaseMigrations/Internal/20210216202218_Initial.cs +++ b/Kyoo/Models/DatabaseMigrations/Internal/20210228210101_Initial.cs @@ -2,7 +2,7 @@ using Microsoft.EntityFrameworkCore.Migrations; using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; -namespace Kyoo.Kyoo.Models.DatabaseMigrations.Internal +namespace Kyoo.Models.DatabaseMigrations.Internal { public partial class Initial : Migration { diff --git a/Kyoo/Models/DatabaseMigrations/Internal/DatabaseContextModelSnapshot.cs b/Kyoo/Models/DatabaseMigrations/Internal/DatabaseContextModelSnapshot.cs index c34b0309..6a2bf0b1 100644 --- a/Kyoo/Models/DatabaseMigrations/Internal/DatabaseContextModelSnapshot.cs +++ b/Kyoo/Models/DatabaseMigrations/Internal/DatabaseContextModelSnapshot.cs @@ -6,7 +6,7 @@ using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; -namespace Kyoo.Kyoo.Models.DatabaseMigrations.Internal +namespace Kyoo.Models.DatabaseMigrations.Internal { [DbContext(typeof(DatabaseContext))] partial class DatabaseContextModelSnapshot : ModelSnapshot