From 00fdc272e92db5dc8dc97fc66dced325ea5169e2 Mon Sep 17 00:00:00 2001 From: Jason Dove Date: Sun, 21 Mar 2021 15:23:53 +0000 Subject: [PATCH] remove plex items from index after sign out (#94) --- .../Plex/Commands/SignOutOfPlexHandler.cs | 12 +++++++++--- .../Repositories/IMediaSourceRepository.cs | 2 +- .../Data/Repositories/MediaSourceRepository.cs | 8 ++++++-- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/ErsatzTV.Application/Plex/Commands/SignOutOfPlexHandler.cs b/ErsatzTV.Application/Plex/Commands/SignOutOfPlexHandler.cs index f6ac7411c..00d072b29 100644 --- a/ErsatzTV.Application/Plex/Commands/SignOutOfPlexHandler.cs +++ b/ErsatzTV.Application/Plex/Commands/SignOutOfPlexHandler.cs @@ -1,9 +1,11 @@ -using System.Threading; +using System.Collections.Generic; +using System.Threading; using System.Threading.Tasks; using ErsatzTV.Core; using ErsatzTV.Core.Interfaces.Locking; using ErsatzTV.Core.Interfaces.Plex; using ErsatzTV.Core.Interfaces.Repositories; +using ErsatzTV.Core.Interfaces.Search; using LanguageExt; namespace ErsatzTV.Application.Plex.Commands @@ -13,20 +15,24 @@ namespace ErsatzTV.Application.Plex.Commands private readonly IEntityLocker _entityLocker; private readonly IMediaSourceRepository _mediaSourceRepository; private readonly IPlexSecretStore _plexSecretStore; + private readonly ISearchIndex _searchIndex; public SignOutOfPlexHandler( IMediaSourceRepository mediaSourceRepository, IPlexSecretStore plexSecretStore, - IEntityLocker entityLocker) + IEntityLocker entityLocker, + ISearchIndex searchIndex) { _mediaSourceRepository = mediaSourceRepository; _plexSecretStore = plexSecretStore; _entityLocker = entityLocker; + _searchIndex = searchIndex; } public async Task> Handle(SignOutOfPlex request, CancellationToken cancellationToken) { - await _mediaSourceRepository.DeleteAllPlex(); + List ids = await _mediaSourceRepository.DeleteAllPlex(); + await _searchIndex.RemoveItems(ids); await _plexSecretStore.DeleteAll(); _entityLocker.UnlockPlex(); diff --git a/ErsatzTV.Core/Interfaces/Repositories/IMediaSourceRepository.cs b/ErsatzTV.Core/Interfaces/Repositories/IMediaSourceRepository.cs index c92c71815..34de2470a 100644 --- a/ErsatzTV.Core/Interfaces/Repositories/IMediaSourceRepository.cs +++ b/ErsatzTV.Core/Interfaces/Repositories/IMediaSourceRepository.cs @@ -35,7 +35,7 @@ namespace ErsatzTV.Core.Interfaces.Repositories Task Update(PlexLibrary plexMediaSourceLibrary); Task Delete(int mediaSourceId); - Task DeleteAllPlex(); + Task> DeleteAllPlex(); Task> DisablePlexLibrarySync(List libraryIds); Task EnablePlexLibrarySync(IEnumerable libraryIds); } diff --git a/ErsatzTV.Infrastructure/Data/Repositories/MediaSourceRepository.cs b/ErsatzTV.Infrastructure/Data/Repositories/MediaSourceRepository.cs index c4fc2b731..be0d7f3b7 100644 --- a/ErsatzTV.Infrastructure/Data/Repositories/MediaSourceRepository.cs +++ b/ErsatzTV.Infrastructure/Data/Repositories/MediaSourceRepository.cs @@ -257,7 +257,7 @@ namespace ErsatzTV.Infrastructure.Data.Repositories await context.SaveChangesAsync(); } - public async Task DeleteAllPlex() + public async Task> DeleteAllPlex() { await using TvContext context = _dbContextFactory.CreateDbContext(); @@ -267,8 +267,12 @@ namespace ErsatzTV.Infrastructure.Data.Repositories List allPlexLibraries = await context.PlexLibraries.ToListAsync(); context.PlexLibraries.RemoveRange(allPlexLibraries); + List movieIds = await context.PlexMovies.Map(pm => pm.Id).ToListAsync(); + List showIds = await context.PlexShows.Map(ps => ps.Id).ToListAsync(); + await context.SaveChangesAsync(); - return Unit.Default; + + return movieIds.Append(showIds).ToList(); } public async Task> DisablePlexLibrarySync(List libraryIds)