Fixing the load

This commit is contained in:
Zoe Roux 2021-02-28 22:30:55 +01:00
parent 7e8ab3b173
commit 00bf834a29
7 changed files with 52 additions and 36 deletions

View File

@ -266,41 +266,43 @@ namespace Kyoo.Controllers
return (obj, member) switch return (obj, member) switch
{ {
(Library l, nameof(Library.Providers)) => ProviderRepository (Library l, nameof(Library.Providers)) => ProviderRepository
.GetAll(Utility.ResourceEquals<ProviderID>(obj)) .GetAll(x => x.Libraries.Any(y => y.ID == obj.ID || y.Slug == obj.Slug))
.Then(x => l.Providers = x), .Then(x => l.Providers = x),
(Library l, nameof(Library.Shows)) => ShowRepository.GetAll(Utility.ResourceEquals<Show>(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), .Then(x => l.Shows = x),
(Library l, nameof(Library.Collections)) => CollectionRepository (Library l, nameof(Library.Collections)) => CollectionRepository
.GetAll(Utility.ResourceEquals<Collection>(obj)) .GetAll(x => x.Libraries.Any(y => y.ID == obj.ID || y.Slug == obj.Slug))
.Then(x => l.Collections = x), .Then(x => l.Collections = x),
(Collection c, nameof(Library.Shows)) => ShowRepository (Collection c, nameof(Library.Shows)) => ShowRepository
.GetAll(Utility.ResourceEquals<Show>(obj)) .GetAll(x => x.Collections.Any(y => y.ID == obj.ID || y.Slug == obj.Slug))
.Then(x => c.Shows = x), .Then(x => c.Shows = x),
(Collection c, nameof(Collection.Libraries)) => LibraryRepository (Collection c, nameof(Collection.Libraries)) => LibraryRepository
.GetAll(Utility.ResourceEquals<Library>(obj)) .GetAll(x => x.Collections.Any(y => y.ID == obj.ID || y.Slug == obj.Slug))
.Then(x => c.Libraries = x), .Then(x => c.Libraries = x),
(Show s, nameof(Show.ExternalIDs)) => ProviderRepository (Show s, nameof(Show.ExternalIDs)) => ProviderRepository
.GetMetadataID(x => x.ShowID == obj.ID) .GetMetadataID(x => x.ShowID == obj.ID)
.Then(x => s.ExternalIDs = x), .Then(x => s.ExternalIDs = x),
(Show s, nameof(Show.Genres)) => GenreRepository (Show s, nameof(Show.Genres)) => GenreRepository
.GetAll(Utility.ResourceEquals<Genre>(obj)) .GetAll(x => x.Shows.Any(y => y.ID == obj.ID || y.Slug == obj.Slug))
.Then(x => s.Genres = x), .Then(x => s.Genres = x),
(Show s, nameof(Show.People)) => ( (Show s, nameof(Show.People)) => (
PeopleRepository.GetFromShow((dynamic)(obj.ID > 0 ? obj.ID : obj.Slug)) PeopleRepository.GetFromShow((dynamic)(obj.ID > 0 ? obj.ID : obj.Slug))
as Task<ICollection<PeopleRole>>).Then(x => s.People = x), as Task<ICollection<PeopleRole>>)
.Then(x => s.People = x),
(Show s, nameof(Show.Seasons)) => SeasonRepository (Show s, nameof(Show.Seasons)) => SeasonRepository
.GetAll(Utility.ResourceEquals<Season>(obj)) .GetAll(x => x.Show.ID == obj.ID || x.Show.Slug == obj.Slug)
.Then(x => s.Seasons = x), .Then(x => s.Seasons = x),
(Show s, nameof(Show.Episodes)) => EpisodeRepository (Show s, nameof(Show.Episodes)) => EpisodeRepository
.GetAll(Utility.ResourceEquals<Episode>(obj)) .GetAll(x => x.Show.ID == obj.ID || x.Show.Slug == obj.Slug)
.Then(x => s.Episodes = x), .Then(x => s.Episodes = x),
(Show s, nameof(Show.Libraries)) => LibraryRepository (Show s, nameof(Show.Libraries)) => LibraryRepository
.GetAll(Utility.ResourceEquals<Library>(obj)) .GetAll(x => x.Shows.Any(y => y.ID == obj.ID || y.Slug == obj.Slug))
.Then(x => s.Libraries = x), .Then(x => s.Libraries = x),
(Show s, nameof(Show.Collections)) => CollectionRepository (Show s, nameof(Show.Collections)) => CollectionRepository
.GetAll(Utility.ResourceEquals<Collection>(obj)) .GetAll(x => x.Shows.Any(y => y.ID == obj.ID || y.Slug == obj.Slug))
.Then(x => s.Collections = x), .Then(x => s.Collections = x),
// TODO Studio loading does not work. // TODO Studio loading does not work.
(Show s, nameof(Show.Studio)) => StudioRepository (Show s, nameof(Show.Studio)) => StudioRepository
@ -311,33 +313,37 @@ namespace Kyoo.Controllers
.GetMetadataID(x => x.SeasonID == obj.ID) .GetMetadataID(x => x.SeasonID == obj.ID)
.Then(x => s.ExternalIDs = x), .Then(x => s.ExternalIDs = x),
(Season s, nameof(Season.Episodes)) => EpisodeRepository (Season s, nameof(Season.Episodes)) => EpisodeRepository
.GetAll(Utility.ResourceEquals<Episode>(obj)) .GetAll(x => x.Season.ID == obj.ID || x.Season.Slug == obj.Slug)
.Then(x => s.Episodes = x), .Then(x => s.Episodes = x),
(Season s, nameof(Season.Show)) => ShowRepository (Season s, nameof(Season.Show)) => ShowRepository
.Get(Utility.ResourceEquals<Show>(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 (Episode e, nameof(Episode.ExternalIDs)) => ProviderRepository
.GetMetadataID(x => x.EpisodeID == obj.ID) .GetMetadataID(x => x.EpisodeID == obj.ID)
.Then(x => e.ExternalIDs = x), .Then(x => e.ExternalIDs = x),
(Episode e, nameof(Episode.Tracks)) => TrackRepository (Episode e, nameof(Episode.Tracks)) => TrackRepository
.GetAll(Utility.ResourceEquals<Track>(obj)) .GetAll(x => x.Episode.ID == obj.ID)
.Then(x => e.Tracks = x), .Then(x => e.Tracks = x),
(Episode e, nameof(Episode.Show)) => ShowRepository (Episode e, nameof(Episode.Show)) => ShowRepository
.Get(Utility.ResourceEquals<Show>(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 (Episode e, nameof(Episode.Season)) => SeasonRepository
.Get(Utility.ResourceEquals<Season>(obj)) .Get(x => x.Episodes.Any(y => y.ID == e.ID))
.Then(x => e.Season = x), .Then(x => e.Season = x),
(Track t, nameof(Track.Episode)) => EpisodeRepository (Track t, nameof(Track.Episode)) => EpisodeRepository
.Get(Utility.ResourceEquals<Episode>(obj)) .Get(x => x.Tracks.Any(y => y.ID == obj.ID))
.Then(x => t.Episode = x), .Then(x => t.Episode = x),
(Genre g, nameof(Genre.Shows)) => ShowRepository (Genre g, nameof(Genre.Shows)) => ShowRepository
.GetAll(Utility.ResourceEquals<Show>(obj)) .GetAll(x => x.Genres.Any(y => y.ID == obj.ID || y.Slug == obj.Slug))
.Then(x => g.Shows = x), .Then(x => g.Shows = x),
(Studio s, nameof(Studio.Shows)) => ShowRepository (Studio s, nameof(Studio.Shows)) => ShowRepository
.GetAll(Utility.ResourceEquals<Show>(obj)) .GetAll(x => x.Studio.ID == obj.ID || x.Studio.Slug == obj.Slug)
.Then(x => s.Shows = x), .Then(x => s.Shows = x),
(People p, nameof(People.ExternalIDs)) => ProviderRepository (People p, nameof(People.ExternalIDs)) => ProviderRepository

View File

@ -11,7 +11,7 @@ namespace Kyoo.Models
public string Name { get; set; } public string Name { get; set; }
public string[] Paths { get; set; } public string[] Paths { get; set; }
[EditableRelation] public virtual ICollection<ProviderID> Providers { get; set; } [EditableRelation] [LoadableRelation] public virtual ICollection<ProviderID> Providers { get; set; }
[LoadableRelation] public virtual ICollection<Show> Shows { get; set; } [LoadableRelation] public virtual ICollection<Show> Shows { get; set; }
[LoadableRelation] public virtual ICollection<Collection> Collections { get; set; } [LoadableRelation] public virtual ICollection<Collection> Collections { get; set; }

View File

@ -1,3 +1,4 @@
using System.Collections.Generic;
using Kyoo.Models.Attributes; using Kyoo.Models.Attributes;
namespace Kyoo.Models namespace Kyoo.Models
@ -9,6 +10,8 @@ namespace Kyoo.Models
public string Name { get; set; } public string Name { get; set; }
public string Logo { get; set; } public string Logo { get; set; }
[LoadableRelation] public ICollection<Library> Libraries { get; set; }
public ProviderID() { } public ProviderID() { }
public ProviderID(string name, string logo) public ProviderID(string name, string logo)

View File

@ -24,9 +24,9 @@ namespace Kyoo.CommonApi
where.Remove(key); where.Remove(key);
} }
string[] fields = string.Join(',', context.HttpContext.Request.Query["fields"]).Split(','); string[] fields = context.HttpContext.Request.Query["fields"]
.SelectMany(x => x.Split(','))
context.HttpContext.Items["fields"] = fields; .ToArray();
if (context.ActionDescriptor is ControllerActionDescriptor descriptor) if (context.ActionDescriptor is ControllerActionDescriptor descriptor)
{ {
Type type = descriptor.MethodInfo.ReturnType; Type type = descriptor.MethodInfo.ReturnType;
@ -37,17 +37,24 @@ namespace Kyoo.CommonApi
PropertyInfo[] properties = type.GetProperties() PropertyInfo[] properties = type.GetProperties()
.Where(x => x.GetCustomAttribute<LoadableRelationAttribute>() != null) .Where(x => x.GetCustomAttribute<LoadableRelationAttribute>() != null)
.ToArray(); .ToArray();
foreach (string field in fields) fields = fields.Select(x =>
{
if (properties.Any(y => string.Equals(y.Name,field, StringComparison.InvariantCultureIgnoreCase)))
continue;
context.Result = new BadRequestObjectResult(new
{ {
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; return;
}
} }
context.HttpContext.Items["fields"] = fields;
base.OnActionExecuting(context); base.OnActionExecuting(context);
} }

View File

@ -7,10 +7,10 @@ using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace Kyoo.Kyoo.Models.DatabaseMigrations.Internal namespace Kyoo.Models.DatabaseMigrations.Internal
{ {
[DbContext(typeof(DatabaseContext))] [DbContext(typeof(DatabaseContext))]
[Migration("20210216202218_Initial")] [Migration("20210228210101_Initial")]
partial class Initial partial class Initial
{ {
protected override void BuildTargetModel(ModelBuilder modelBuilder) protected override void BuildTargetModel(ModelBuilder modelBuilder)

View File

@ -2,7 +2,7 @@
using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace Kyoo.Kyoo.Models.DatabaseMigrations.Internal namespace Kyoo.Models.DatabaseMigrations.Internal
{ {
public partial class Initial : Migration public partial class Initial : Migration
{ {

View File

@ -6,7 +6,7 @@ using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace Kyoo.Kyoo.Models.DatabaseMigrations.Internal namespace Kyoo.Models.DatabaseMigrations.Internal
{ {
[DbContext(typeof(DatabaseContext))] [DbContext(typeof(DatabaseContext))]
partial class DatabaseContextModelSnapshot : ModelSnapshot partial class DatabaseContextModelSnapshot : ModelSnapshot