Solving a bug with libraries links

This commit is contained in:
Zoe Roux 2020-02-08 19:09:15 +01:00
parent d307308fb4
commit b9b00192bc
3 changed files with 16 additions and 4 deletions

View File

@ -1,3 +1,6 @@
using System;
#nullable enable
namespace Kyoo.Models
{
public class LibraryLink

View File

@ -345,12 +345,12 @@ namespace Kyoo.Controllers
public void RegisterShowLinks(Library library, Collection collection, Show show)
{
if (collection != null)
if (collection != null)
{
_database.LibraryLinks.Add(new LibraryLink {LibraryID = library.ID, CollectionID = collection.ID});
_database.CollectionLinks.Add(new CollectionLink { CollectionID = collection.ID, ShowID = show.ID});
_database.LibraryLinks.AddIfNotExist(new LibraryLink {LibraryID = library.ID, CollectionID = collection.ID}, x => x.LibraryID == library.ID && x.CollectionID == collection.ID && x.ShowID == null);
_database.CollectionLinks.AddIfNotExist(new CollectionLink { CollectionID = collection.ID, ShowID = show.ID}, x => x.CollectionID == collection.ID && x.ShowID == show.ID);
}
_database.LibraryLinks.Add(new LibraryLink {LibraryID = library.ID, ShowID = show.ID});
_database.LibraryLinks.AddIfNotExist(new LibraryLink {LibraryID = library.ID, ShowID = show.ID}, x => x.LibraryID == library.ID && x.CollectionID == null && x.ShowID == show.ID);
_database.SaveChanges();
}

View File

@ -50,4 +50,13 @@ namespace Kyoo
.ValueGeneratedNever();
}
}
}
public static class DbSetExtension
{
public static EntityEntry<T> AddIfNotExist<T>(this DbSet<T> db, T entity, Func<T, bool> predicate) where T : class
{
bool exists = db.Any(predicate);
return exists ? null : db.Add(entity);
}
}