mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-06-02 21:24:20 -04:00
Fixing the load
This commit is contained in:
parent
7e8ab3b173
commit
00bf834a29
@ -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<ProviderID>(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<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),
|
||||
(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),
|
||||
|
||||
(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),
|
||||
(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),
|
||||
|
||||
(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<Genre>(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<ICollection<PeopleRole>>).Then(x => s.People = x),
|
||||
as Task<ICollection<PeopleRole>>)
|
||||
.Then(x => s.People = x),
|
||||
(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),
|
||||
(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),
|
||||
(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),
|
||||
(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),
|
||||
// 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<Episode>(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<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
|
||||
.GetMetadataID(x => x.EpisodeID == obj.ID)
|
||||
.Then(x => e.ExternalIDs = x),
|
||||
(Episode e, nameof(Episode.Tracks)) => TrackRepository
|
||||
.GetAll(Utility.ResourceEquals<Track>(obj))
|
||||
.GetAll(x => x.Episode.ID == obj.ID)
|
||||
.Then(x => e.Tracks = x),
|
||||
(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
|
||||
.Get(Utility.ResourceEquals<Season>(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<Episode>(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<Show>(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<Show>(obj))
|
||||
.GetAll(x => x.Studio.ID == obj.ID || x.Studio.Slug == obj.Slug)
|
||||
.Then(x => s.Shows = x),
|
||||
|
||||
(People p, nameof(People.ExternalIDs)) => ProviderRepository
|
||||
|
@ -11,7 +11,7 @@ namespace Kyoo.Models
|
||||
public string Name { 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<Collection> Collections { get; set; }
|
||||
|
@ -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<Library> Libraries { get; set; }
|
||||
|
||||
public ProviderID() { }
|
||||
|
||||
|
@ -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<LoadableRelationAttribute>() != 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);
|
||||
}
|
||||
|
||||
|
@ -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)
|
@ -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
|
||||
{
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user