From 90a7d6654efcdd053b3d9e6b848b10f986ba62ce Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Fri, 8 May 2020 02:21:59 +0200 Subject: [PATCH] Updating the database scheme --- Kyoo.Common/Models/Collection.cs | 7 +- Kyoo.Common/Models/CollectionLink.cs | 10 +++ Kyoo/Controllers/LibraryManager.cs | 46 +++-------- Kyoo/Models/DatabaseContext.cs | 12 +-- ....cs => 20200507185533_Initial.Designer.cs} | 36 ++++++-- ...1_Initial.cs => 20200507185533_Initial.cs} | 79 +++++++++++------- .../Internal/DatabaseContextModelSnapshot.cs | 34 ++++++-- Kyoo/Tasks/Crawler.cs | 82 +++++++++++-------- Kyoo/Views/API/PeopleAPI.cs | 3 +- 9 files changed, 188 insertions(+), 121 deletions(-) rename Kyoo/Models/DatabaseMigrations/Internal/{20200426223831_Initial.Designer.cs => 20200507185533_Initial.Designer.cs} (95%) rename Kyoo/Models/DatabaseMigrations/Internal/{20200426223831_Initial.cs => 20200507185533_Initial.cs} (94%) diff --git a/Kyoo.Common/Models/Collection.cs b/Kyoo.Common/Models/Collection.cs index 928992ce..830d0150 100644 --- a/Kyoo.Common/Models/Collection.cs +++ b/Kyoo.Common/Models/Collection.cs @@ -12,7 +12,12 @@ namespace Kyoo.Models public string Poster { get; set; } public string Overview { get; set; } [JsonIgnore] public string ImgPrimary { get; set; } - public virtual IEnumerable Shows { get; set; } + [JsonIgnore] public virtual IEnumerable Links { get; set; } + public virtual IEnumerable Shows + { + get => Links.Select(x => x.Show); + set => Links = value.Select(x => new CollectionLink(this, x)); + } public Collection() { } diff --git a/Kyoo.Common/Models/CollectionLink.cs b/Kyoo.Common/Models/CollectionLink.cs index b4baf772..24fb6403 100644 --- a/Kyoo.Common/Models/CollectionLink.cs +++ b/Kyoo.Common/Models/CollectionLink.cs @@ -7,5 +7,15 @@ namespace Kyoo.Models public virtual Collection Collection { get; set; } public long ShowID { get; set; } public virtual Show Show { get; set; } + + public CollectionLink() { } + + public CollectionLink(Collection collection, Show show) + { + Collection = collection; + CollectionID = collection.ID; + Show = show; + ShowID = show.ID; + } } } \ No newline at end of file diff --git a/Kyoo/Controllers/LibraryManager.cs b/Kyoo/Controllers/LibraryManager.cs index f04fda4c..69499273 100644 --- a/Kyoo/Controllers/LibraryManager.cs +++ b/Kyoo/Controllers/LibraryManager.cs @@ -430,25 +430,14 @@ namespace Kyoo.Controllers return x; }).ToList(); - show.People = show.People?.Select(x => - { - x.People = Validate(x.People); - x.PeopleID = x.People.ID; - return x; - }).ToList(); - - show.Seasons = show.Seasons?.Select(x => - { - return _database.Seasons.FirstOrDefault(y => y.ShowID == x.ShowID - && y.SeasonNumber == x.SeasonNumber) ?? Validate(x); - }).ToList(); - show.Episodes = show.Episodes?.Select(x => - { - return _database.Episodes.FirstOrDefault(y => y.ShowID == x.ShowID - && y.SeasonNumber == x.SeasonNumber - && y.EpisodeNumber == x.EpisodeNumber) ?? Validate(x); - }).ToList(); - + show.People = show.People?.GroupBy(x => x.Slug) + .Select(x => x.First()) + .Select(x => + { + x.People = Validate(x.People); + x.PeopleID = x.People.ID; + return x; + }).ToList(); show.ExternalIDs = Validate(show.ExternalIDs); return show; } @@ -457,13 +446,8 @@ namespace Kyoo.Controllers { if (season == null) return null; - - season.Episodes = season.Episodes?.Select(x => - { - return _database.Episodes.FirstOrDefault(y => y.ShowID == x.ShowID - && y.SeasonNumber == x.SeasonNumber - && y.EpisodeNumber == x.EpisodeNumber) ?? Validate(x); - }).ToList(); + + season.Show = Validate(season.Show); season.ExternalIDs = Validate(season.ExternalIDs); return season; } @@ -472,13 +456,9 @@ namespace Kyoo.Controllers { if (episode == null) return null; - - Season old = _database.Seasons.FirstOrDefault(x => x.ShowID == episode.ShowID - && x.SeasonNumber == episode.SeasonNumber); - if (old != null) - episode.Season = old; - else - episode.Season.ExternalIDs = Validate(episode.Season.ExternalIDs); + + episode.Show = GetShow(episode.Show.Slug) ?? Validate(episode.Show); + episode.Season = GetSeason(episode.Show.Slug, episode.SeasonNumber) ?? Validate(episode.Season); episode.ExternalIDs = Validate(episode.ExternalIDs); return episode; } diff --git a/Kyoo/Models/DatabaseContext.cs b/Kyoo/Models/DatabaseContext.cs index 991f358a..dac4d29b 100644 --- a/Kyoo/Models/DatabaseContext.cs +++ b/Kyoo/Models/DatabaseContext.cs @@ -117,17 +117,17 @@ namespace Kyoo modelBuilder.Entity() .Ignore(x => x.Genres); + + modelBuilder.Entity() + .Ignore(x => x.Shows); modelBuilder.Entity() - .Ignore(x => x.Slug); - modelBuilder.Entity() - .Ignore(x => x.Name); - modelBuilder.Entity() + .Ignore(x => x.Slug) + .Ignore(x => x.Name) .Ignore(x => x.ExternalIDs); modelBuilder.Entity() - .Ignore(x => x.Name); - modelBuilder.Entity() + .Ignore(x => x.Name) .Ignore(x => x.Logo); diff --git a/Kyoo/Models/DatabaseMigrations/Internal/20200426223831_Initial.Designer.cs b/Kyoo/Models/DatabaseMigrations/Internal/20200507185533_Initial.Designer.cs similarity index 95% rename from Kyoo/Models/DatabaseMigrations/Internal/20200426223831_Initial.Designer.cs rename to Kyoo/Models/DatabaseMigrations/Internal/20200507185533_Initial.Designer.cs index 36ce01db..c9475964 100644 --- a/Kyoo/Models/DatabaseMigrations/Internal/20200426223831_Initial.Designer.cs +++ b/Kyoo/Models/DatabaseMigrations/Internal/20200507185533_Initial.Designer.cs @@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace Kyoo.Models.DatabaseMigrations.Internal { [DbContext(typeof(DatabaseContext))] - [Migration("20200426223831_Initial")] + [Migration("20200507185533_Initial")] partial class Initial { protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -27,6 +27,9 @@ namespace Kyoo.Models.DatabaseMigrations.Internal b.Property("ImgPrimary") .HasColumnType("TEXT"); + b.Property("LibraryID") + .HasColumnType("INTEGER"); + b.Property("Name") .HasColumnType("TEXT"); @@ -41,6 +44,8 @@ namespace Kyoo.Models.DatabaseMigrations.Internal b.HasKey("ID"); + b.HasIndex("LibraryID"); + b.HasIndex("Slug") .IsUnique(); @@ -271,10 +276,7 @@ namespace Kyoo.Models.DatabaseMigrations.Internal .ValueGeneratedOnAdd() .HasColumnType("INTEGER"); - b.Property("PeopleID") - .HasColumnType("TEXT"); - - b.Property("PeopleID1") + b.Property("PeopleID") .HasColumnType("INTEGER"); b.Property("Role") @@ -288,7 +290,7 @@ namespace Kyoo.Models.DatabaseMigrations.Internal b.HasKey("ID"); - b.HasIndex("PeopleID1"); + b.HasIndex("PeopleID"); b.HasIndex("ShowID"); @@ -385,6 +387,9 @@ namespace Kyoo.Models.DatabaseMigrations.Internal b.Property("IsMovie") .HasColumnType("INTEGER"); + b.Property("LibraryID") + .HasColumnType("INTEGER"); + b.Property("Logo") .HasColumnType("TEXT"); @@ -417,6 +422,8 @@ namespace Kyoo.Models.DatabaseMigrations.Internal b.HasKey("ID"); + b.HasIndex("LibraryID"); + b.HasIndex("Slug") .IsUnique(); @@ -485,10 +492,17 @@ namespace Kyoo.Models.DatabaseMigrations.Internal b.ToTable("Tracks"); }); + modelBuilder.Entity("Kyoo.Models.Collection", b => + { + b.HasOne("Kyoo.Models.Library", null) + .WithMany("Collections") + .HasForeignKey("LibraryID"); + }); + modelBuilder.Entity("Kyoo.Models.CollectionLink", b => { b.HasOne("Kyoo.Models.Collection", "Collection") - .WithMany() + .WithMany("Links") .HasForeignKey("CollectionID"); b.HasOne("Kyoo.Models.Show", "Show") @@ -572,7 +586,9 @@ namespace Kyoo.Models.DatabaseMigrations.Internal { b.HasOne("Kyoo.Models.People", "People") .WithMany("Roles") - .HasForeignKey("PeopleID1"); + .HasForeignKey("PeopleID") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); b.HasOne("Kyoo.Models.Show", "Show") .WithMany("People") @@ -605,6 +621,10 @@ namespace Kyoo.Models.DatabaseMigrations.Internal modelBuilder.Entity("Kyoo.Models.Show", b => { + b.HasOne("Kyoo.Models.Library", null) + .WithMany("Shows") + .HasForeignKey("LibraryID"); + b.HasOne("Kyoo.Models.Studio", "Studio") .WithMany() .HasForeignKey("StudioID"); diff --git a/Kyoo/Models/DatabaseMigrations/Internal/20200426223831_Initial.cs b/Kyoo/Models/DatabaseMigrations/Internal/20200507185533_Initial.cs similarity index 94% rename from Kyoo/Models/DatabaseMigrations/Internal/20200426223831_Initial.cs rename to Kyoo/Models/DatabaseMigrations/Internal/20200507185533_Initial.cs index 3ae1e6b9..bea865a2 100644 --- a/Kyoo/Models/DatabaseMigrations/Internal/20200426223831_Initial.cs +++ b/Kyoo/Models/DatabaseMigrations/Internal/20200507185533_Initial.cs @@ -7,23 +7,6 @@ namespace Kyoo.Models.DatabaseMigrations.Internal { protected override void Up(MigrationBuilder migrationBuilder) { - migrationBuilder.CreateTable( - name: "Collections", - columns: table => new - { - ID = table.Column(nullable: false) - .Annotation("Sqlite:Autoincrement", true), - Slug = table.Column(nullable: true), - Name = table.Column(nullable: true), - Poster = table.Column(nullable: true), - Overview = table.Column(nullable: true), - ImgPrimary = table.Column(nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_Collections", x => x.ID); - }); - migrationBuilder.CreateTable( name: "Genres", columns: table => new @@ -96,6 +79,30 @@ namespace Kyoo.Models.DatabaseMigrations.Internal table.PrimaryKey("PK_Studios", x => x.ID); }); + migrationBuilder.CreateTable( + name: "Collections", + columns: table => new + { + ID = table.Column(nullable: false) + .Annotation("Sqlite:Autoincrement", true), + Slug = table.Column(nullable: true), + Name = table.Column(nullable: true), + Poster = table.Column(nullable: true), + Overview = table.Column(nullable: true), + ImgPrimary = table.Column(nullable: true), + LibraryID = table.Column(nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Collections", x => x.ID); + table.ForeignKey( + name: "FK_Collections_Libraries_LibraryID", + column: x => x.LibraryID, + principalTable: "Libraries", + principalColumn: "ID", + onDelete: ReferentialAction.Restrict); + }); + migrationBuilder.CreateTable( name: "ProviderLinks", columns: table => new @@ -141,11 +148,18 @@ namespace Kyoo.Models.DatabaseMigrations.Internal Logo = table.Column(nullable: true), Backdrop = table.Column(nullable: true), IsMovie = table.Column(nullable: false), - StudioID = table.Column(nullable: true) + StudioID = table.Column(nullable: true), + LibraryID = table.Column(nullable: true) }, constraints: table => { table.PrimaryKey("PK_Shows", x => x.ID); + table.ForeignKey( + name: "FK_Shows_Libraries_LibraryID", + column: x => x.LibraryID, + principalTable: "Libraries", + principalColumn: "ID", + onDelete: ReferentialAction.Restrict); table.ForeignKey( name: "FK_Shows_Studios_StudioID", column: x => x.StudioID, @@ -243,8 +257,7 @@ namespace Kyoo.Models.DatabaseMigrations.Internal { ID = table.Column(nullable: false) .Annotation("Sqlite:Autoincrement", true), - PeopleID = table.Column(nullable: true), - PeopleID1 = table.Column(nullable: true), + PeopleID = table.Column(nullable: false), ShowID = table.Column(nullable: false), Role = table.Column(nullable: true), Type = table.Column(nullable: true) @@ -253,11 +266,11 @@ namespace Kyoo.Models.DatabaseMigrations.Internal { table.PrimaryKey("PK_PeopleLinks", x => x.ID); table.ForeignKey( - name: "FK_PeopleLinks_Peoples_PeopleID1", - column: x => x.PeopleID1, + name: "FK_PeopleLinks_Peoples_PeopleID", + column: x => x.PeopleID, principalTable: "Peoples", principalColumn: "ID", - onDelete: ReferentialAction.Restrict); + onDelete: ReferentialAction.Cascade); table.ForeignKey( name: "FK_PeopleLinks_Shows_ShowID", column: x => x.ShowID, @@ -411,6 +424,11 @@ namespace Kyoo.Models.DatabaseMigrations.Internal table: "CollectionLinks", column: "ShowID"); + migrationBuilder.CreateIndex( + name: "IX_Collections_LibraryID", + table: "Collections", + column: "LibraryID"); + migrationBuilder.CreateIndex( name: "IX_Collections_Slug", table: "Collections", @@ -485,9 +503,9 @@ namespace Kyoo.Models.DatabaseMigrations.Internal column: "ShowID"); migrationBuilder.CreateIndex( - name: "IX_PeopleLinks_PeopleID1", + name: "IX_PeopleLinks_PeopleID", table: "PeopleLinks", - column: "PeopleID1"); + column: "PeopleID"); migrationBuilder.CreateIndex( name: "IX_PeopleLinks_ShowID", @@ -521,6 +539,11 @@ namespace Kyoo.Models.DatabaseMigrations.Internal table: "Seasons", column: "ShowID"); + migrationBuilder.CreateIndex( + name: "IX_Shows_LibraryID", + table: "Shows", + column: "LibraryID"); + migrationBuilder.CreateIndex( name: "IX_Shows_Slug", table: "Shows", @@ -576,9 +599,6 @@ namespace Kyoo.Models.DatabaseMigrations.Internal migrationBuilder.DropTable( name: "Peoples"); - migrationBuilder.DropTable( - name: "Libraries"); - migrationBuilder.DropTable( name: "Providers"); @@ -591,6 +611,9 @@ namespace Kyoo.Models.DatabaseMigrations.Internal migrationBuilder.DropTable( name: "Shows"); + migrationBuilder.DropTable( + name: "Libraries"); + migrationBuilder.DropTable( name: "Studios"); } diff --git a/Kyoo/Models/DatabaseMigrations/Internal/DatabaseContextModelSnapshot.cs b/Kyoo/Models/DatabaseMigrations/Internal/DatabaseContextModelSnapshot.cs index 75c41270..2eddf479 100644 --- a/Kyoo/Models/DatabaseMigrations/Internal/DatabaseContextModelSnapshot.cs +++ b/Kyoo/Models/DatabaseMigrations/Internal/DatabaseContextModelSnapshot.cs @@ -25,6 +25,9 @@ namespace Kyoo.Models.DatabaseMigrations.Internal b.Property("ImgPrimary") .HasColumnType("TEXT"); + b.Property("LibraryID") + .HasColumnType("INTEGER"); + b.Property("Name") .HasColumnType("TEXT"); @@ -39,6 +42,8 @@ namespace Kyoo.Models.DatabaseMigrations.Internal b.HasKey("ID"); + b.HasIndex("LibraryID"); + b.HasIndex("Slug") .IsUnique(); @@ -269,10 +274,7 @@ namespace Kyoo.Models.DatabaseMigrations.Internal .ValueGeneratedOnAdd() .HasColumnType("INTEGER"); - b.Property("PeopleID") - .HasColumnType("TEXT"); - - b.Property("PeopleID1") + b.Property("PeopleID") .HasColumnType("INTEGER"); b.Property("Role") @@ -286,7 +288,7 @@ namespace Kyoo.Models.DatabaseMigrations.Internal b.HasKey("ID"); - b.HasIndex("PeopleID1"); + b.HasIndex("PeopleID"); b.HasIndex("ShowID"); @@ -383,6 +385,9 @@ namespace Kyoo.Models.DatabaseMigrations.Internal b.Property("IsMovie") .HasColumnType("INTEGER"); + b.Property("LibraryID") + .HasColumnType("INTEGER"); + b.Property("Logo") .HasColumnType("TEXT"); @@ -415,6 +420,8 @@ namespace Kyoo.Models.DatabaseMigrations.Internal b.HasKey("ID"); + b.HasIndex("LibraryID"); + b.HasIndex("Slug") .IsUnique(); @@ -483,10 +490,17 @@ namespace Kyoo.Models.DatabaseMigrations.Internal b.ToTable("Tracks"); }); + modelBuilder.Entity("Kyoo.Models.Collection", b => + { + b.HasOne("Kyoo.Models.Library", null) + .WithMany("Collections") + .HasForeignKey("LibraryID"); + }); + modelBuilder.Entity("Kyoo.Models.CollectionLink", b => { b.HasOne("Kyoo.Models.Collection", "Collection") - .WithMany() + .WithMany("Links") .HasForeignKey("CollectionID"); b.HasOne("Kyoo.Models.Show", "Show") @@ -570,7 +584,9 @@ namespace Kyoo.Models.DatabaseMigrations.Internal { b.HasOne("Kyoo.Models.People", "People") .WithMany("Roles") - .HasForeignKey("PeopleID1"); + .HasForeignKey("PeopleID") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); b.HasOne("Kyoo.Models.Show", "Show") .WithMany("People") @@ -603,6 +619,10 @@ namespace Kyoo.Models.DatabaseMigrations.Internal modelBuilder.Entity("Kyoo.Models.Show", b => { + b.HasOne("Kyoo.Models.Library", null) + .WithMany("Shows") + .HasForeignKey("LibraryID"); + b.HasOne("Kyoo.Models.Studio", "Studio") .WithMany() .HasForeignKey("StudioID"); diff --git a/Kyoo/Tasks/Crawler.cs b/Kyoo/Tasks/Crawler.cs index 4f6b734d..1ffdb8c6 100644 --- a/Kyoo/Tasks/Crawler.cs +++ b/Kyoo/Tasks/Crawler.cs @@ -8,6 +8,7 @@ using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using Kyoo.Models.Watch; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; namespace Kyoo.Controllers @@ -21,7 +22,7 @@ namespace Kyoo.Controllers public bool RunOnStartup => true; public int Priority => 0; - private ILibraryManager _libraryManager; + private IServiceProvider _serviceProvider; private IThumbnailsManager _thumbnailsManager; private IProviderManager _metadataProvider; private ITranscoder _transcoder; @@ -29,7 +30,9 @@ namespace Kyoo.Controllers public IEnumerable GetPossibleParameters() { - return _libraryManager.GetLibraries().Select(x => x.Slug); + using IServiceScope serviceScope = _serviceProvider.CreateScope(); + ILibraryManager libraryManager = serviceScope.ServiceProvider.GetService(); + return libraryManager.GetLibraries().Select(x => x.Slug); } public int? Progress() @@ -40,30 +43,30 @@ namespace Kyoo.Controllers public async Task Run(IServiceProvider serviceProvider, CancellationToken cancellationToken, string argument = null) { - // TODO Should use more scopes of the library manager (one per episodes to register). - using IServiceScope serviceScope = serviceProvider.CreateScope(); - _libraryManager = serviceScope.ServiceProvider.GetService(); - _thumbnailsManager = serviceScope.ServiceProvider.GetService(); - _metadataProvider = serviceScope.ServiceProvider.GetService(); - _transcoder = serviceScope.ServiceProvider.GetService(); - _config = serviceScope.ServiceProvider.GetService(); + _serviceProvider = serviceProvider; + _thumbnailsManager = serviceProvider.GetService(); + _metadataProvider = serviceProvider.GetService(); + _transcoder = serviceProvider.GetService(); + _config = serviceProvider.GetService(); try { - IEnumerable episodes = _libraryManager.GetEpisodes(); + using IServiceScope serviceScope = _serviceProvider.CreateScope(); + ILibraryManager libraryManager = serviceScope.ServiceProvider.GetService(); + IEnumerable episodes = libraryManager.GetEpisodes(); IEnumerable libraries = argument == null - ? _libraryManager.GetLibraries() - : new [] {_libraryManager.GetLibrary(argument)}; + ? libraryManager.GetLibraries() + : new [] {libraryManager.GetLibrary(argument)}; foreach (Episode episode in episodes) { if (!File.Exists(episode.Path)) - _libraryManager.RemoveEpisode(episode); + libraryManager.RemoveEpisode(episode); } - await _libraryManager.SaveChanges(); + await libraryManager.SaveChanges(); foreach (Library library in libraries) - await Scan(library, cancellationToken); + await Scan(library, libraryManager, cancellationToken); } catch (Exception ex) { @@ -72,7 +75,7 @@ namespace Kyoo.Controllers Console.WriteLine("Scan finished!"); } - private async Task Scan(Library library, CancellationToken cancellationToken) + private async Task Scan(Library library, ILibraryManager libraryManager, CancellationToken cancellationToken) { Console.WriteLine($"Scanning library {library.Name} at {string.Join(", ", library.Paths)}."); foreach (string path in library.Paths) @@ -106,11 +109,11 @@ namespace Kyoo.Controllers } await Task.WhenAll(files.Select(file => { - if (!IsVideo(file) || _libraryManager.GetEpisodes().Any(x => x.Path == file)) - return null; + if (!IsVideo(file) || libraryManager.GetEpisodes().Any(x => x.Path == file)) + return Task.CompletedTask; string relativePath = file.Substring(path.Length); return RegisterFile(file, relativePath, library, cancellationToken); - }).Where(x => x != null)); + })); } } @@ -119,6 +122,10 @@ namespace Kyoo.Controllers if (token.IsCancellationRequested) return; + using IServiceScope serviceScope = _serviceProvider.CreateScope(); + ILibraryManager libraryManager = serviceScope.ServiceProvider.GetService(); + ((DbSet)libraryManager.GetLibraries()).Attach(library); + Console.WriteLine($"Registering episode at: {path}"); string patern = _config.GetValue("regex"); Regex regex = new Regex(patern, RegexOptions.IgnoreCase); @@ -131,33 +138,36 @@ namespace Kyoo.Controllers long episodeNumber = long.TryParse(match.Groups["Episode"].Value, out tmp) ? tmp : -1; long absoluteNumber = long.TryParse(match.Groups["Absolute"].Value, out tmp) ? tmp : -1; - Collection collection = await GetCollection(collectionName, library); + Collection collection = await GetCollection(libraryManager, collectionName, library); bool isMovie = seasonNumber == -1 && episodeNumber == -1 && absoluteNumber == -1; - Show show = await GetShow(showName, showPath, isMovie, library); + Show show = await GetShow(libraryManager, showName, showPath, isMovie, library); if (isMovie) - _libraryManager.Register(await GetMovie(show, path)); + libraryManager.Register(await GetMovie(show, path)); else { - Season season = await GetSeason(show, seasonNumber, library); - Episode episode = await GetEpisode(show, season, episodeNumber, absoluteNumber, path, library); - _libraryManager.Register(episode); + Season season = await GetSeason(libraryManager, show, seasonNumber, library); + Episode episode = await GetEpisode(libraryManager, show, season, episodeNumber, absoluteNumber, path, library); + libraryManager.Register(episode); } if (collection != null) - _libraryManager.Register(collection); - _libraryManager.RegisterShowLinks(library, collection, show); - await _libraryManager.SaveChanges(); + libraryManager.Register(collection); + libraryManager.RegisterShowLinks(library, collection, show); + await libraryManager.SaveChanges(); } - private async Task GetCollection(string collectionName, Library library) + private async Task GetCollection(ILibraryManager libraryManager, string collectionName, Library library) { if (string.IsNullOrEmpty(collectionName)) return await Task.FromResult(null); - return _libraryManager.GetCollection(Utility.ToSlug(collectionName)) ?? await _metadataProvider.GetCollectionFromName(collectionName, library); + Collection name = libraryManager.GetCollection(Utility.ToSlug(collectionName)); + if (name != null) + return name; + return await _metadataProvider.GetCollectionFromName(collectionName, library); } - private async Task GetShow(string showTitle, string showPath, bool isMovie, Library library) + private async Task GetShow(ILibraryManager libraryManager, string showTitle, string showPath, bool isMovie, Library library) { - Show show = _libraryManager.GetShowByPath(showPath); + Show show = libraryManager.GetShowByPath(showPath); if (show != null) return show; show = await _metadataProvider.SearchShow(showTitle, isMovie, library); @@ -170,11 +180,11 @@ namespace Kyoo.Controllers return show; } - private async Task GetSeason(Show show, long seasonNumber, Library library) + private async Task GetSeason(ILibraryManager libraryManager, Show show, long seasonNumber, Library library) { if (seasonNumber == -1) return default; - Season season = _libraryManager.GetSeason(show.Slug, seasonNumber); + Season season = libraryManager.GetSeason(show.Slug, seasonNumber); if (season == null) { season = await _metadataProvider.GetSeason(show, seasonNumber, library); @@ -184,11 +194,11 @@ namespace Kyoo.Controllers return season; } - private async Task GetEpisode(Show show, Season season, long episodeNumber, long absoluteNumber, string episodePath, Library library) + private async Task GetEpisode(ILibraryManager libraryManager, Show show, Season season, long episodeNumber, long absoluteNumber, string episodePath, Library library) { Episode episode = await _metadataProvider.GetEpisode(show, episodePath, season?.SeasonNumber ?? -1, episodeNumber, absoluteNumber, library); if (season == null) - season = await GetSeason(show, episode.SeasonNumber, library); + season = await GetSeason(libraryManager, show, episode.SeasonNumber, library); episode.Season = season; if (season == null) { diff --git a/Kyoo/Views/API/PeopleAPI.cs b/Kyoo/Views/API/PeopleAPI.cs index 25ef79eb..ca21eb0b 100644 --- a/Kyoo/Views/API/PeopleAPI.cs +++ b/Kyoo/Views/API/PeopleAPI.cs @@ -25,12 +25,11 @@ namespace Kyoo.Api if (people == null) return NotFound(); - Collection collection = new Collection(people.Slug, people.Name, null, null) + return new Collection(people.Slug, people.Name, null, null) { Shows = people.Roles.Select(x => x.Show), Poster = "peopleimg/" + people.Slug }; - return collection; } } } \ No newline at end of file