private async Task ScanLocalMediaSources(CancellationToken cancellationToken) { using IServiceScope scope = _serviceScopeFactory.CreateScope(); TvContext dbContext = scope.ServiceProvider.GetRequiredService <TvContext>(); List <int> localLibraryIds = await dbContext.LocalMediaSources .SelectMany(ms => ms.Libraries) .Map(l => l.Id) .ToListAsync(cancellationToken); foreach (int libraryId in localLibraryIds) { if (_entityLocker.LockLibrary(libraryId)) { await _channel.WriteAsync( new ScanLocalLibraryIfNeeded(libraryId), cancellationToken); } } }
private async Task <LocalLibraryViewModel> PersistLocalLibrary( TvContext dbContext, LocalLibrary localLibrary) { await dbContext.LocalLibraries.AddAsync(localLibrary); await dbContext.SaveChangesAsync(); if (_entityLocker.LockLibrary(localLibrary.Id)) { await _workerChannel.WriteAsync(new ForceScanLocalLibrary(localLibrary.Id)); } return(ProjectToViewModel(localLibrary)); }
private async Task <LocalLibraryViewModel> UpdateLocalLibrary(TvContext dbContext, Parameters parameters) { (LocalLibrary existing, LocalLibrary incoming) = parameters; existing.Name = incoming.Name; var toAdd = incoming.Paths .Filter(p => existing.Paths.All(ep => NormalizePath(ep.Path) != NormalizePath(p.Path))) .ToList(); var toRemove = existing.Paths .Filter(ep => incoming.Paths.All(p => NormalizePath(p.Path) != NormalizePath(ep.Path))) .ToList(); 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(); existing.Paths.RemoveAll(toRemove.Contains); existing.Paths.AddRange(toAdd); if (await dbContext.SaveChangesAsync() > 0) { await _searchIndex.RemoveItems(itemsToRemove); _searchIndex.Commit(); } if ((toAdd.Count > 0 || toRemove.Count > 0) && _entityLocker.LockLibrary(existing.Id)) { await _workerChannel.WriteAsync(new ForceScanLocalLibrary(existing.Id)); } return(ProjectToViewModel(existing)); }