diff --git a/ErsatzTV.Application/Libraries/Commands/DeleteLocalLibraryHandler.cs b/ErsatzTV.Application/Libraries/Commands/DeleteLocalLibraryHandler.cs index 1cd04e186..c057be0e3 100644 --- a/ErsatzTV.Application/Libraries/Commands/DeleteLocalLibraryHandler.cs +++ b/ErsatzTV.Application/Libraries/Commands/DeleteLocalLibraryHandler.cs @@ -14,9 +14,7 @@ public class DeleteLocalLibraryHandler : LocalLibraryHandlerBase, private readonly IDbContextFactory _dbContextFactory; private readonly ISearchIndex _searchIndex; - public DeleteLocalLibraryHandler( - IDbContextFactory dbContextFactory, - ISearchIndex searchIndex) + public DeleteLocalLibraryHandler(IDbContextFactory dbContextFactory, ISearchIndex searchIndex) { _dbContextFactory = dbContextFactory; _searchIndex = searchIndex; @@ -34,15 +32,40 @@ public class DeleteLocalLibraryHandler : LocalLibraryHandlerBase, private async Task DoDeletion(TvContext dbContext, LocalLibrary localLibrary) { List ids = await dbContext.Connection.QueryAsync( - @"SELECT MediaItem.Id FROM MediaItem - INNER JOIN LibraryPath LP on MediaItem.LibraryPathId = LP.Id - WHERE LP.LibraryId = @LibraryId", + """ + SELECT MediaItem.Id FROM MediaItem + INNER JOIN LibraryPath LP on MediaItem.LibraryPathId = LP.Id + WHERE LP.LibraryId = @LibraryId + """, new { LibraryId = localLibrary.Id }) .Map(result => result.ToList()); await _searchIndex.RemoveItems(ids); _searchIndex.Commit(); + await dbContext.Connection.ExecuteAsync( + """ + DELETE FROM MediaItem WHERE Id IN + ( + SELECT MI.Id FROM MediaItem MI + INNER JOIN LibraryPath LP ON MI.LibraryPathId = LP.Id + WHERE LP.LibraryId = @LibraryId + ) + """, + new { LibraryId = localLibrary.Id }); + + // delete all library folders (children first) + IOrderedQueryable orderedFolders = dbContext.LibraryFolders + .Filter(lf => lf.LibraryPath.LibraryId == localLibrary.Id) + .OrderByDescending(lp => lp.Path.Length); + + foreach (LibraryFolder folder in orderedFolders) + { + await dbContext.Connection.ExecuteAsync( + "DELETE FROM LibraryFolder WHERE Id = @LibraryFolderId", + new { LibraryFolderId = folder.Id }); + } + dbContext.LocalLibraries.Remove(localLibrary); await dbContext.SaveChangesAsync(); diff --git a/ErsatzTV.Application/Libraries/Commands/UpdateLocalLibraryHandler.cs b/ErsatzTV.Application/Libraries/Commands/UpdateLocalLibraryHandler.cs index 85005014b..342537af4 100644 --- a/ErsatzTV.Application/Libraries/Commands/UpdateLocalLibraryHandler.cs +++ b/ErsatzTV.Application/Libraries/Commands/UpdateLocalLibraryHandler.cs @@ -1,4 +1,5 @@ using System.Threading.Channels; +using Dapper; using ErsatzTV.Application.MediaSources; using ErsatzTV.Core; using ErsatzTV.Core.Domain; @@ -54,16 +55,36 @@ public class UpdateLocalLibraryHandler : LocalLibraryHandlerBase, var toRemoveIds = toRemove.Map(lp => lp.Id).ToList(); - List itemsToRemove = await dbContext.MediaItems - .Filter(mi => toRemoveIds.Contains(mi.LibraryPathId)) - .Map(mi => mi.Id) - .ToListAsync(); + await dbContext.Connection.ExecuteAsync( + "DELETE FROM MediaItem WHERE LibraryPathId IN @Ids", + new { Ids = toRemoveIds }); + + // delete all library folders (children first) + IOrderedQueryable orderedFolders = dbContext.LibraryFolders + .Filter(lf => toRemoveIds.Contains(lf.LibraryPathId)) + .OrderByDescending(lp => lp.Path.Length); + + foreach (LibraryFolder folder in orderedFolders) + { + await dbContext.Connection.ExecuteAsync( + "DELETE FROM LibraryFolder WHERE Id = @LibraryFolderId", + new { LibraryFolderId = folder.Id }); + } + + await dbContext.LibraryPaths + .Filter(lp => toRemoveIds.Contains(lp.Id)) + .ExecuteDeleteAsync(); - existing.Paths.RemoveAll(toRemove.Contains); existing.Paths.AddRange(toAdd); if (await dbContext.SaveChangesAsync() > 0) { + List itemsToRemove = await dbContext.MediaItems + .AsNoTracking() + .Filter(mi => toRemoveIds.Contains(mi.LibraryPathId)) + .Map(mi => mi.Id) + .ToListAsync(); + await _searchIndex.RemoveItems(itemsToRemove); _searchIndex.Commit(); }