diff --git a/ErsatzTV.Application/Plex/Commands/UpdatePlexLibraryPreferencesHandler.cs b/ErsatzTV.Application/Plex/Commands/UpdatePlexLibraryPreferencesHandler.cs index 831811b35..ee81b4962 100644 --- a/ErsatzTV.Application/Plex/Commands/UpdatePlexLibraryPreferencesHandler.cs +++ b/ErsatzTV.Application/Plex/Commands/UpdatePlexLibraryPreferencesHandler.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; using System.Threading; using System.Threading.Tasks; using ErsatzTV.Core; @@ -20,7 +21,7 @@ namespace ErsatzTV.Application.Plex.Commands UpdatePlexLibraryPreferences request, CancellationToken cancellationToken) { - IEnumerable toDisable = request.Preferences.Filter(p => p.ShouldSyncItems == false).Map(p => p.Id); + var toDisable = request.Preferences.Filter(p => p.ShouldSyncItems == false).Map(p => p.Id).ToList(); await _mediaSourceRepository.DisablePlexLibrarySync(toDisable); IEnumerable toEnable = request.Preferences.Filter(p => p.ShouldSyncItems).Map(p => p.Id); diff --git a/ErsatzTV.Core/Interfaces/Repositories/IMediaSourceRepository.cs b/ErsatzTV.Core/Interfaces/Repositories/IMediaSourceRepository.cs index 596a6f67b..74f52762a 100644 --- a/ErsatzTV.Core/Interfaces/Repositories/IMediaSourceRepository.cs +++ b/ErsatzTV.Core/Interfaces/Repositories/IMediaSourceRepository.cs @@ -23,7 +23,7 @@ namespace ErsatzTV.Core.Interfaces.Repositories Task Update(PlexMediaSource plexMediaSource); Task Update(PlexLibrary plexMediaSourceLibrary); Task Delete(int id); - Task DisablePlexLibrarySync(IEnumerable libraryIds); + 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 fe8d51b98..fc21f44dd 100644 --- a/ErsatzTV.Infrastructure/Data/Repositories/MediaSourceRepository.cs +++ b/ErsatzTV.Infrastructure/Data/Repositories/MediaSourceRepository.cs @@ -168,14 +168,29 @@ namespace ErsatzTV.Infrastructure.Data.Repositories await context.SaveChangesAsync(); } - public Task DisablePlexLibrarySync(IEnumerable libraryIds) => - _dbConnection.ExecuteAsync( - "UPDATE PlexLibrary SET ShouldSyncItems = 0 WHERE Id in @ids", + public async Task DisablePlexLibrarySync(List libraryIds) + { + await _dbConnection.ExecuteAsync( + "UPDATE PlexLibrary SET ShouldSyncItems = 0 WHERE Id IN @ids", + new { ids = libraryIds }); + + await _dbConnection.ExecuteAsync( + "UPDATE Library SET LastScan = null WHERE Id IN @ids", new { ids = libraryIds }); + await _dbConnection.ExecuteAsync( + @"DELETE FROM MediaItem WHERE Id IN + (SELECT m.Id FROM MediaItem m + INNER JOIN PlexMovie pm ON pm.Id = m.Id + INNER JOIN LibraryPath lp ON lp.Id = m.LibraryPathId + INNER JOIN Library l ON l.Id = lp.LibraryId + WHERE l.Id IN @ids)", + new { ids = libraryIds }); + } + public Task EnablePlexLibrarySync(IEnumerable libraryIds) => _dbConnection.ExecuteAsync( - "UPDATE PlexLibrary SET ShouldSyncItems = 1 WHERE Id in @ids", + "UPDATE PlexLibrary SET ShouldSyncItems = 1 WHERE Id IN @ids", new { ids = libraryIds }); } } diff --git a/ErsatzTV/Pages/PlexLibrariesEditor.razor b/ErsatzTV/Pages/PlexLibrariesEditor.razor index ce40519b0..a89b77d68 100644 --- a/ErsatzTV/Pages/PlexLibrariesEditor.razor +++ b/ErsatzTV/Pages/PlexLibrariesEditor.razor @@ -6,6 +6,7 @@ @inject NavigationManager NavigationManager @inject ILogger Logger @inject ISnackbar Snackbar +@inject ChannelWriter Channel @@ -86,13 +87,22 @@ Seq errorMessages = await Mediator.Send(request).Map(e => e.LeftToSeq()); - errorMessages.HeadOrNone().Match( + await errorMessages.HeadOrNone().Match( error => { Snackbar.Add($"Unexpected error saving plex libraries: {error.Value}", Severity.Error); Logger.LogError("Unexpected error saving plex libraries: {Error}", error.Value); + return Task.CompletedTask; }, - () => NavigationManager.NavigateTo("/media/plex")); + async () => + { + foreach (int id in _libraries.Filter(l => l.ShouldSyncItems).Map(l => l.Id)) + { + await Channel.WriteAsync(new SynchronizePlexLibraryByIdIfNeeded(id)); + } + + NavigationManager.NavigateTo("/media/plex"); + }); } } \ No newline at end of file