diff --git a/Kyoo.Common/Models/Link.cs b/Kyoo.Common/Models/Link.cs index cacc7c64..f504b5a0 100644 --- a/Kyoo.Common/Models/Link.cs +++ b/Kyoo.Common/Models/Link.cs @@ -8,7 +8,13 @@ namespace Kyoo.Models public int FirstID { get; set; } public int SecondID { get; set; } - public Link() {} + public Link() {} + + public Link(int firstID, int secondID) + { + FirstID = firstID; + SecondID = secondID; + } public Link(IResource first, IResource second) { @@ -39,7 +45,7 @@ namespace Kyoo.Models { get { - return x => new {LibraryID = x.FirstID, ProviderID = x.SecondID}; + return x => new {First = x.FirstID, Second = x.SecondID}; } } } @@ -63,11 +69,15 @@ namespace Kyoo.Models Second = second; } + public Link(int firstID, int secondID) + : base(firstID, secondID) + { } + public new static Expression, object>> PrimaryKey { get { - return x => new {LibraryID = x.FirstID, ProviderID = x.SecondID}; + return x => new {First = x.FirstID, Second = x.SecondID}; } } } diff --git a/Kyoo/Controllers/Repositories/ShowRepository.cs b/Kyoo/Controllers/Repositories/ShowRepository.cs index be04ffc9..f07e4f5a 100644 --- a/Kyoo/Controllers/Repositories/ShowRepository.cs +++ b/Kyoo/Controllers/Repositories/ShowRepository.cs @@ -4,7 +4,6 @@ using System.Linq; using System.Linq.Expressions; using System.Threading.Tasks; using Kyoo.Models; -using Kyoo.Models.Exceptions; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; @@ -148,31 +147,23 @@ namespace Kyoo.Controllers public async Task AddShowLink(int showID, int? libraryID, int? collectionID) { - Show show = await Get(showID); if (collectionID != null) { - Collection collection = _database.GetTemporaryObject(new Collection {ID = collectionID.Value}); - - show.Collections ??= new List(); - show.Collections.Add(collection); + await _database.Links() + .AddAsync(new Link(collectionID.Value, showID)); await _database.SaveIfNoDuplicates(); if (libraryID != null) { - Library library = await _database.Libraries.FirstOrDefaultAsync(x => x.ID == libraryID.Value); - if (library == null) - throw new ItemNotFound($"No library found with the ID {libraryID.Value}"); - library.Collections ??= new List(); - library.Collections.Add(collection); + await _database.Links() + .AddAsync(new Link(libraryID.Value, collectionID.Value)); await _database.SaveIfNoDuplicates(); } } if (libraryID != null) { - Library library = _database.GetTemporaryObject(new Library {ID = libraryID.Value}); - - show.Libraries ??= new List(); - show.Libraries.Add(library); + await _database.Links() + .AddAsync(new Link(libraryID.Value, showID)); await _database.SaveIfNoDuplicates(); } } diff --git a/Kyoo/Models/DatabaseContext.cs b/Kyoo/Models/DatabaseContext.cs index 25271455..435b01f7 100644 --- a/Kyoo/Models/DatabaseContext.cs +++ b/Kyoo/Models/DatabaseContext.cs @@ -30,8 +30,14 @@ namespace Kyoo public DbSet Providers { get; set; } public DbSet MetadataIds { get; set; } - // TODO Many to many with UsingEntity for this. public DbSet PeopleRoles { get; set; } + + public DbSet> Links() + where T1 : class, IResource + where T2 : class, IResource + { + return Set>(); + } public DatabaseContext() @@ -115,7 +121,7 @@ namespace Kyoo .HasOne(x => x.Second) .WithMany(x => x.CollectionLinks), y => y.HasKey(Link.PrimaryKey)); - + modelBuilder.Entity() .HasMany(x => x.Shows) .WithMany(x => x.Genres) diff --git a/Kyoo/Views/WebClient b/Kyoo/Views/WebClient index f36ac1bb..a8a576bd 160000 --- a/Kyoo/Views/WebClient +++ b/Kyoo/Views/WebClient @@ -1 +1 @@ -Subproject commit f36ac1bb9bf60329a7f04ba76730f43ded7b0d9d +Subproject commit a8a576bdd9e0c69fbae8e5e4f392c112fe3af8a2