Fixing collection's link & cleaning library's link

This commit is contained in:
Zoe Roux 2021-03-18 11:40:43 +01:00
parent aeed35bea6
commit fdfd42c7c1
4 changed files with 28 additions and 21 deletions

View File

@ -10,6 +10,12 @@ namespace Kyoo.Models
public Link() {} public Link() {}
public Link(int firstID, int secondID)
{
FirstID = firstID;
SecondID = secondID;
}
public Link(IResource first, IResource second) public Link(IResource first, IResource second)
{ {
FirstID = first.ID; FirstID = first.ID;
@ -39,7 +45,7 @@ namespace Kyoo.Models
{ {
get 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; Second = second;
} }
public Link(int firstID, int secondID)
: base(firstID, secondID)
{ }
public new static Expression<Func<Link<T1, T2>, object>> PrimaryKey public new static Expression<Func<Link<T1, T2>, object>> PrimaryKey
{ {
get get
{ {
return x => new {LibraryID = x.FirstID, ProviderID = x.SecondID}; return x => new {First = x.FirstID, Second = x.SecondID};
} }
} }
} }

View File

@ -4,7 +4,6 @@ using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using Kyoo.Models; using Kyoo.Models;
using Kyoo.Models.Exceptions;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@ -148,31 +147,23 @@ namespace Kyoo.Controllers
public async Task AddShowLink(int showID, int? libraryID, int? collectionID) public async Task AddShowLink(int showID, int? libraryID, int? collectionID)
{ {
Show show = await Get(showID);
if (collectionID != null) if (collectionID != null)
{ {
Collection collection = _database.GetTemporaryObject(new Collection {ID = collectionID.Value}); await _database.Links<Collection, Show>()
.AddAsync(new Link<Collection, Show>(collectionID.Value, showID));
show.Collections ??= new List<Collection>();
show.Collections.Add(collection);
await _database.SaveIfNoDuplicates(); await _database.SaveIfNoDuplicates();
if (libraryID != null) if (libraryID != null)
{ {
Library library = await _database.Libraries.FirstOrDefaultAsync(x => x.ID == libraryID.Value); await _database.Links<Library, Collection>()
if (library == null) .AddAsync(new Link<Library, Collection>(libraryID.Value, collectionID.Value));
throw new ItemNotFound($"No library found with the ID {libraryID.Value}");
library.Collections ??= new List<Collection>();
library.Collections.Add(collection);
await _database.SaveIfNoDuplicates(); await _database.SaveIfNoDuplicates();
} }
} }
if (libraryID != null) if (libraryID != null)
{ {
Library library = _database.GetTemporaryObject(new Library {ID = libraryID.Value}); await _database.Links<Library, Show>()
.AddAsync(new Link<Library, Show>(libraryID.Value, showID));
show.Libraries ??= new List<Library>();
show.Libraries.Add(library);
await _database.SaveIfNoDuplicates(); await _database.SaveIfNoDuplicates();
} }
} }

View File

@ -30,9 +30,15 @@ namespace Kyoo
public DbSet<ProviderID> Providers { get; set; } public DbSet<ProviderID> Providers { get; set; }
public DbSet<MetadataID> MetadataIds { get; set; } public DbSet<MetadataID> MetadataIds { get; set; }
// TODO Many to many with UsingEntity for this.
public DbSet<PeopleRole> PeopleRoles { get; set; } public DbSet<PeopleRole> PeopleRoles { get; set; }
public DbSet<Link<T1, T2>> Links<T1, T2>()
where T1 : class, IResource
where T2 : class, IResource
{
return Set<Link<T1, T2>>();
}
public DatabaseContext() public DatabaseContext()
{ {

@ -1 +1 @@
Subproject commit f36ac1bb9bf60329a7f04ba76730f43ded7b0d9d Subproject commit a8a576bdd9e0c69fbae8e5e4f392c112fe3af8a2