Browse Source

remove unsynchronized plex movies on save; queue plex library scan on save

pull/34/head
Jason Dove 5 years ago
parent
commit
ba37b6f709
  1. 3
      ErsatzTV.Application/Plex/Commands/UpdatePlexLibraryPreferencesHandler.cs
  2. 2
      ErsatzTV.Core/Interfaces/Repositories/IMediaSourceRepository.cs
  3. 23
      ErsatzTV.Infrastructure/Data/Repositories/MediaSourceRepository.cs
  4. 14
      ErsatzTV/Pages/PlexLibrariesEditor.razor

3
ErsatzTV.Application/Plex/Commands/UpdatePlexLibraryPreferencesHandler.cs

@ -1,4 +1,5 @@ @@ -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 @@ -20,7 +21,7 @@ namespace ErsatzTV.Application.Plex.Commands
UpdatePlexLibraryPreferences request,
CancellationToken cancellationToken)
{
IEnumerable<int> 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<int> toEnable = request.Preferences.Filter(p => p.ShouldSyncItems).Map(p => p.Id);

2
ErsatzTV.Core/Interfaces/Repositories/IMediaSourceRepository.cs

@ -23,7 +23,7 @@ namespace ErsatzTV.Core.Interfaces.Repositories @@ -23,7 +23,7 @@ namespace ErsatzTV.Core.Interfaces.Repositories
Task Update(PlexMediaSource plexMediaSource);
Task Update(PlexLibrary plexMediaSourceLibrary);
Task Delete(int id);
Task DisablePlexLibrarySync(IEnumerable<int> libraryIds);
Task DisablePlexLibrarySync(List<int> libraryIds);
Task EnablePlexLibrarySync(IEnumerable<int> libraryIds);
}
}

23
ErsatzTV.Infrastructure/Data/Repositories/MediaSourceRepository.cs

@ -168,14 +168,29 @@ namespace ErsatzTV.Infrastructure.Data.Repositories @@ -168,14 +168,29 @@ namespace ErsatzTV.Infrastructure.Data.Repositories
await context.SaveChangesAsync();
}
public Task DisablePlexLibrarySync(IEnumerable<int> libraryIds) =>
_dbConnection.ExecuteAsync(
"UPDATE PlexLibrary SET ShouldSyncItems = 0 WHERE Id in @ids",
public async Task DisablePlexLibrarySync(List<int> 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<int> libraryIds) =>
_dbConnection.ExecuteAsync(
"UPDATE PlexLibrary SET ShouldSyncItems = 1 WHERE Id in @ids",
"UPDATE PlexLibrary SET ShouldSyncItems = 1 WHERE Id IN @ids",
new { ids = libraryIds });
}
}

14
ErsatzTV/Pages/PlexLibrariesEditor.razor

@ -6,6 +6,7 @@ @@ -6,6 +6,7 @@
@inject NavigationManager NavigationManager
@inject ILogger<PlexLibrariesEditor> Logger
@inject ISnackbar Snackbar
@inject ChannelWriter<IBackgroundServiceRequest> Channel
<MudContainer MaxWidth="MaxWidth.ExtraLarge">
<MudTable Hover="true" Items="_libraries" Dense="true">
@ -86,13 +87,22 @@ @@ -86,13 +87,22 @@
Seq<BaseError> 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");
});
}
}
Loading…
Cancel
Save