Browse Source

delet items removed from plex (#77)

* delete items removed from plex

* fix tests
pull/78/head v0.0.18-prealpha
Jason Dove 5 years ago committed by GitHub
parent
commit
a01888792a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      ErsatzTV.Core.Tests/Fakes/FakeTelevisionRepository.cs
  2. 1
      ErsatzTV.Core/Interfaces/Repositories/IMovieRepository.cs
  3. 3
      ErsatzTV.Core/Interfaces/Repositories/ITelevisionRepository.cs
  4. 4
      ErsatzTV.Core/Plex/PlexMovieLibraryScanner.cs
  5. 12
      ErsatzTV.Core/Plex/PlexTelevisionLibraryScanner.cs
  6. 9
      ErsatzTV.Infrastructure/Data/Repositories/MovieRepository.cs
  7. 29
      ErsatzTV.Infrastructure/Data/Repositories/TelevisionRepository.cs

9
ErsatzTV.Core.Tests/Fakes/FakeTelevisionRepository.cs

@ -77,5 +77,14 @@ namespace ErsatzTV.Core.Tests.Fakes @@ -77,5 +77,14 @@ namespace ErsatzTV.Core.Tests.Fakes
throw new NotSupportedException();
public Task<Unit> AddGenre(ShowMetadata metadata, Genre genre) => throw new NotSupportedException();
public Task<Unit> RemoveMissingPlexShows(PlexLibrary library, List<string> showKeys) =>
throw new NotSupportedException();
public Task<Unit> RemoveMissingPlexSeasons(string showKey, List<string> seasonKeys) =>
throw new NotSupportedException();
public Task<Unit> RemoveMissingPlexEpisodes(string seasonKey, List<string> episodeKeys) =>
throw new NotSupportedException();
}
}

1
ErsatzTV.Core/Interfaces/Repositories/IMovieRepository.cs

@ -17,5 +17,6 @@ namespace ErsatzTV.Core.Interfaces.Repositories @@ -17,5 +17,6 @@ namespace ErsatzTV.Core.Interfaces.Repositories
Task<IEnumerable<string>> FindMoviePaths(LibraryPath libraryPath);
Task<Unit> DeleteByPath(LibraryPath libraryPath, string path);
Task<Unit> AddGenre(MovieMetadata metadata, Genre genre);
Task<Unit> RemoveMissingPlexMovies(PlexLibrary library, List<string> movieKeys);
}
}

3
ErsatzTV.Core/Interfaces/Repositories/ITelevisionRepository.cs

@ -36,5 +36,8 @@ namespace ErsatzTV.Core.Interfaces.Repositories @@ -36,5 +36,8 @@ namespace ErsatzTV.Core.Interfaces.Repositories
Task<Either<BaseError, PlexSeason>> GetOrAddPlexSeason(PlexLibrary library, PlexSeason item);
Task<Either<BaseError, PlexEpisode>> GetOrAddPlexEpisode(PlexLibrary library, PlexEpisode item);
Task<Unit> AddGenre(ShowMetadata metadata, Genre genre);
Task<Unit> RemoveMissingPlexShows(PlexLibrary library, List<string> showKeys);
Task<Unit> RemoveMissingPlexSeasons(string showKey, List<string> seasonKeys);
Task<Unit> RemoveMissingPlexEpisodes(string seasonKey, List<string> episodeKeys);
}
}

4
ErsatzTV.Core/Plex/PlexMovieLibraryScanner.cs

@ -45,7 +45,6 @@ namespace ErsatzTV.Core.Plex @@ -45,7 +45,6 @@ namespace ErsatzTV.Core.Plex
{
foreach (PlexMovie incoming in movieEntries)
{
// TODO: optimize dbcontext use here, do we need tracking? can we make partial updates with dapper?
// TODO: figure out how to rebuild playlists
Either<BaseError, PlexMovie> maybeMovie = await _movieRepository
.GetOrAdd(plexMediaSourceLibrary, incoming)
@ -59,6 +58,9 @@ namespace ErsatzTV.Core.Plex @@ -59,6 +58,9 @@ namespace ErsatzTV.Core.Plex
incoming.Key,
error.Value));
}
var movieKeys = movieEntries.Map(s => s.Key).ToList();
await _movieRepository.RemoveMissingPlexMovies(plexMediaSourceLibrary, movieKeys);
},
error =>
{

12
ErsatzTV.Core/Plex/PlexTelevisionLibraryScanner.cs

@ -45,7 +45,6 @@ namespace ErsatzTV.Core.Plex @@ -45,7 +45,6 @@ namespace ErsatzTV.Core.Plex
{
foreach (PlexShow incoming in showEntries)
{
// TODO: optimize dbcontext use here, do we need tracking? can we make partial updates with dapper?
// TODO: figure out how to rebuild playlists
Either<BaseError, PlexShow> maybeShow = await _televisionRepository
.GetOrAddPlexShow(plexMediaSourceLibrary, incoming)
@ -64,7 +63,8 @@ namespace ErsatzTV.Core.Plex @@ -64,7 +63,8 @@ namespace ErsatzTV.Core.Plex
});
}
// TODO: delete removed shows
var showKeys = showEntries.Map(s => s.Key).ToList();
await _televisionRepository.RemoveMissingPlexShows(plexMediaSourceLibrary, showKeys);
return Unit.Default;
},
@ -141,7 +141,6 @@ namespace ErsatzTV.Core.Plex @@ -141,7 +141,6 @@ namespace ErsatzTV.Core.Plex
{
incoming.ShowId = show.Id;
// TODO: optimize dbcontext use here, do we need tracking? can we make partial updates with dapper?
// TODO: figure out how to rebuild playlists
Either<BaseError, PlexSeason> maybeSeason = await _televisionRepository
.GetOrAddPlexSeason(plexMediaSourceLibrary, incoming)
@ -159,7 +158,8 @@ namespace ErsatzTV.Core.Plex @@ -159,7 +158,8 @@ namespace ErsatzTV.Core.Plex
});
}
// TODO: delete removed seasons
var seasonKeys = seasonEntries.Map(s => s.Key).ToList();
await _televisionRepository.RemoveMissingPlexSeasons(show.Key, seasonKeys);
return Unit.Default;
},
@ -206,7 +206,6 @@ namespace ErsatzTV.Core.Plex @@ -206,7 +206,6 @@ namespace ErsatzTV.Core.Plex
{
incoming.SeasonId = season.Id;
// TODO: optimize dbcontext use here, do we need tracking? can we make partial updates with dapper?
// TODO: figure out how to rebuild playlists
Either<BaseError, PlexEpisode> maybeEpisode = await _televisionRepository
.GetOrAddPlexEpisode(plexMediaSourceLibrary, incoming)
@ -220,7 +219,8 @@ namespace ErsatzTV.Core.Plex @@ -220,7 +219,8 @@ namespace ErsatzTV.Core.Plex
error.Value));
}
// TODO: delete removed episodes
var episodeKeys = episodeEntries.Map(s => s.Key).ToList();
await _televisionRepository.RemoveMissingPlexEpisodes(season.Key, episodeKeys);
return Unit.Default;
},

9
ErsatzTV.Infrastructure/Data/Repositories/MovieRepository.cs

@ -147,6 +147,15 @@ namespace ErsatzTV.Infrastructure.Data.Repositories @@ -147,6 +147,15 @@ namespace ErsatzTV.Infrastructure.Data.Repositories
"INSERT INTO Genre (Name, MovieMetadataId) VALUES (@Name, @MetadataId)",
new { genre.Name, MetadataId = metadata.Id }).ToUnit();
public Task<Unit> RemoveMissingPlexMovies(PlexLibrary library, List<string> movieKeys) =>
_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
WHERE lp.LibraryId = @LibraryId AND pm.Key not in @Keys)",
new { LibraryId = library.Id, Keys = movieKeys }).ToUnit();
private async Task<Either<BaseError, Movie>> AddMovie(int libraryPathId, string path)
{
try

29
ErsatzTV.Infrastructure/Data/Repositories/TelevisionRepository.cs

@ -364,6 +364,35 @@ namespace ErsatzTV.Infrastructure.Data.Repositories @@ -364,6 +364,35 @@ namespace ErsatzTV.Infrastructure.Data.Repositories
"INSERT INTO Genre (Name, SeasonMetadataId) VALUES (@Name, @MetadataId)",
new { genre.Name, MetadataId = metadata.Id }).ToUnit();
public Task<Unit> RemoveMissingPlexShows(PlexLibrary library, List<string> showKeys) =>
_dbConnection.ExecuteAsync(
@"DELETE FROM MediaItem WHERE Id IN
(SELECT m.Id FROM MediaItem m
INNER JOIN PlexShow ps ON ps.Id = m.Id
INNER JOIN LibraryPath lp ON lp.Id = m.LibraryPathId
WHERE lp.LibraryId = @LibraryId AND ps.Key not in @Keys)",
new { LibraryId = library.Id, Keys = showKeys }).ToUnit();
public Task<Unit> RemoveMissingPlexSeasons(string showKey, List<string> seasonKeys) =>
_dbConnection.ExecuteAsync(
@"DELETE FROM MediaItem WHERE Id IN
(SELECT m.Id FROM MediaItem m
INNER JOIN Season s ON m.Id = s.Id
INNER JOIN PlexSeason ps ON ps.Id = m.Id
INNER JOIN PlexShow P on P.Id = s.ShowId
WHERE P.Key = @ShowKey AND ps.Key not in @Keys)",
new { ShowKey = showKey, Keys = seasonKeys }).ToUnit();
public Task<Unit> RemoveMissingPlexEpisodes(string seasonKey, List<string> episodeKeys) =>
_dbConnection.ExecuteAsync(
@"DELETE FROM MediaItem WHERE Id IN
(SELECT m.Id FROM MediaItem m
INNER JOIN Episode e ON m.Id = e.Id
INNER JOIN PlexEpisode pe ON pe.Id = m.Id
INNER JOIN PlexSeason P on P.Id = e.SeasonId
WHERE P.Key = @SeasonKey AND pe.Key not in @Keys)",
new { SeasonKey = seasonKey, Keys = episodeKeys }).ToUnit();
public async Task<List<Episode>> GetShowItems(int showId)
{
IEnumerable<int> ids = await _dbConnection.QueryAsync<int>(

Loading…
Cancel
Save