fix deleting local libraries and local library paths (#1640)

This commit is contained in:
Jason Dove
2024-03-09 12:12:22 -06:00
committed by GitHub
parent 2119c88c97
commit aded03d962
2 changed files with 55 additions and 11 deletions
@@ -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<int> 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<LibraryFolder> 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<int> itemsToRemove = await dbContext.MediaItems
.AsNoTracking()
.Filter(mi => toRemoveIds.Contains(mi.LibraryPathId))
.Map(mi => mi.Id)
.ToListAsync();
await _searchIndex.RemoveItems(itemsToRemove);
_searchIndex.Commit();
}