Fixing library links duplicates

This commit is contained in:
Zoe Roux 2020-08-03 06:01:38 +02:00
parent 9c549c593b
commit 8af2cff1a2
5 changed files with 36 additions and 6 deletions

View File

@ -129,15 +129,18 @@ namespace Kyoo.Controllers
if (collectionID != null) if (collectionID != null)
{ {
await _database.CollectionLinks.AddAsync(new CollectionLink {CollectionID = collectionID, ShowID = showID}); await _database.CollectionLinks.AddAsync(new CollectionLink {CollectionID = collectionID, ShowID = showID});
await _database.SaveIfNoDuplicates();
} }
if (libraryID != null) if (libraryID != null)
{ {
await _database.LibraryLinks.AddAsync(new LibraryLink {LibraryID = libraryID.Value, ShowID = showID}); await _database.LibraryLinks.AddAsync(new LibraryLink {LibraryID = libraryID.Value, ShowID = showID});
await _database.SaveIfNoDuplicates();
} }
if (libraryID != null && collectionID != null) if (libraryID != null && collectionID != null)
{ {
await _database.LibraryLinks.AddAsync(new LibraryLink {LibraryID = libraryID.Value, CollectionID = collectionID.Value}); await _database.LibraryLinks.AddAsync(new LibraryLink {LibraryID = libraryID.Value, CollectionID = collectionID.Value});
await _database.SaveIfNoDuplicates();
} }
} }

View File

@ -179,7 +179,10 @@ namespace Kyoo
.HasIndex(x => new {x.ShowID, x.SeasonNumber, x.EpisodeNumber, x.AbsoluteNumber}) .HasIndex(x => new {x.ShowID, x.SeasonNumber, x.EpisodeNumber, x.AbsoluteNumber})
.IsUnique(); .IsUnique();
modelBuilder.Entity<LibraryLink>() modelBuilder.Entity<LibraryLink>()
.HasIndex(x => new {x.LibraryID, x.ShowID, x.CollectionID}) .HasIndex(x => new {x.LibraryID, x.ShowID})
.IsUnique();
modelBuilder.Entity<LibraryLink>()
.HasIndex(x => new {x.LibraryID, x.CollectionID})
.IsUnique(); .IsUnique();
modelBuilder.Entity<CollectionLink>() modelBuilder.Entity<CollectionLink>()
.HasIndex(x => new {x.CollectionID, x.ShowID}) .HasIndex(x => new {x.CollectionID, x.ShowID})
@ -278,6 +281,18 @@ namespace Kyoo
} }
} }
public async Task<int> SaveIfNoDuplicates(CancellationToken cancellationToken = new CancellationToken())
{
try
{
return await SaveChangesAsync(cancellationToken);
}
catch (DuplicatedItemException)
{
return -1;
}
}
public static bool IsDuplicateException(DbUpdateException ex) public static bool IsDuplicateException(DbUpdateException ex)
{ {
return ex.InnerException is PostgresException inner return ex.InnerException is PostgresException inner

View File

@ -10,7 +10,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace Kyoo.Models.DatabaseMigrations.Internal namespace Kyoo.Models.DatabaseMigrations.Internal
{ {
[DbContext(typeof(DatabaseContext))] [DbContext(typeof(DatabaseContext))]
[Migration("20200803005331_Initial")] [Migration("20200803040029_Initial")]
partial class Initial partial class Initial
{ {
protected override void BuildTargetModel(ModelBuilder modelBuilder) protected override void BuildTargetModel(ModelBuilder modelBuilder)
@ -203,7 +203,10 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
b.HasIndex("ShowID"); b.HasIndex("ShowID");
b.HasIndex("LibraryID", "ShowID", "CollectionID") b.HasIndex("LibraryID", "CollectionID")
.IsUnique();
b.HasIndex("LibraryID", "ShowID")
.IsUnique(); .IsUnique();
b.ToTable("LibraryLinks"); b.ToTable("LibraryLinks");

View File

@ -457,9 +457,15 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
column: "ShowID"); column: "ShowID");
migrationBuilder.CreateIndex( migrationBuilder.CreateIndex(
name: "IX_LibraryLinks_LibraryID_ShowID_CollectionID", name: "IX_LibraryLinks_LibraryID_CollectionID",
table: "LibraryLinks", table: "LibraryLinks",
columns: new[] { "LibraryID", "ShowID", "CollectionID" }, columns: new[] { "LibraryID", "CollectionID" },
unique: true);
migrationBuilder.CreateIndex(
name: "IX_LibraryLinks_LibraryID_ShowID",
table: "LibraryLinks",
columns: new[] { "LibraryID", "ShowID" },
unique: true); unique: true);
migrationBuilder.CreateIndex( migrationBuilder.CreateIndex(

View File

@ -201,7 +201,10 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
b.HasIndex("ShowID"); b.HasIndex("ShowID");
b.HasIndex("LibraryID", "ShowID", "CollectionID") b.HasIndex("LibraryID", "CollectionID")
.IsUnique();
b.HasIndex("LibraryID", "ShowID")
.IsUnique(); .IsUnique();
b.ToTable("LibraryLinks"); b.ToTable("LibraryLinks");