mirror of
https://github.com/Radarr/Radarr.git
synced 2025-04-24 06:27:08 -04:00
Fixed: Cleanup MovieMetadata that was removed from a collection
This commit is contained in:
parent
f910a8fde7
commit
eb9eb4ec64
6 changed files with 71 additions and 3 deletions
|
@ -1,4 +1,5 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using NzbDrone.Core.Datastore;
|
||||
using NzbDrone.Core.Messaging.Events;
|
||||
|
||||
|
@ -7,6 +8,7 @@ namespace NzbDrone.Core.ImportLists.ImportListMovies
|
|||
public interface IImportListMovieRepository : IBasicRepository<ImportListMovie>
|
||||
{
|
||||
List<ImportListMovie> GetAllForLists(List<int> listIds);
|
||||
bool ExistsByMetadataId(int metadataId);
|
||||
}
|
||||
|
||||
public class ImportListMovieRepository : BasicRepository<ImportListMovie>, IImportListMovieRepository
|
||||
|
@ -20,5 +22,12 @@ namespace NzbDrone.Core.ImportLists.ImportListMovies
|
|||
{
|
||||
return Query(x => listIds.Contains(x.ListId));
|
||||
}
|
||||
|
||||
public bool ExistsByMetadataId(int metadataId)
|
||||
{
|
||||
var movies = Query(x => x.MovieMetadataId == metadataId);
|
||||
|
||||
return movies.Any();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@ namespace NzbDrone.Core.ImportLists.ImportListMovies
|
|||
List<ImportListMovie> SyncMoviesForList(List<ImportListMovie> listMovies, int listId);
|
||||
void RemoveListMovie(ImportListMovie listMovie);
|
||||
ImportListMovie GetById(int id);
|
||||
bool ExistsByMetadataId(int metadataId);
|
||||
}
|
||||
|
||||
public class ImportListMovieService : IImportListMovieService, IHandleAsync<ProviderDeletedEvent<IImportList>>
|
||||
|
@ -79,5 +80,10 @@ namespace NzbDrone.Core.ImportLists.ImportListMovies
|
|||
var moviesOnList = _importListMovieRepository.GetAllForLists(new List<int> { message.ProviderId });
|
||||
_importListMovieRepository.DeleteMany(moviesOnList);
|
||||
}
|
||||
|
||||
public bool ExistsByMetadataId(int metadataId)
|
||||
{
|
||||
return _importListMovieRepository.ExistsByMetadataId(metadataId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using System.Collections.Generic;
|
||||
using NzbDrone.Core.ImportLists.ImportListMovies;
|
||||
|
||||
namespace NzbDrone.Core.Movies
|
||||
{
|
||||
|
@ -11,15 +12,20 @@ namespace NzbDrone.Core.Movies
|
|||
List<MovieMetadata> GetMoviesByCollectionTmdbId(int collectionId);
|
||||
bool Upsert(MovieMetadata movie);
|
||||
bool UpsertMany(List<MovieMetadata> movies);
|
||||
void DeleteMany(List<MovieMetadata> movies);
|
||||
}
|
||||
|
||||
public class MovieMetadataService : IMovieMetadataService
|
||||
{
|
||||
private readonly IMovieMetadataRepository _movieMetadataRepository;
|
||||
private readonly IMovieService _movieService;
|
||||
private readonly IImportListMovieService _importListMovieService;
|
||||
|
||||
public MovieMetadataService(IMovieMetadataRepository movieMetadataRepository)
|
||||
public MovieMetadataService(IMovieMetadataRepository movieMetadataRepository, IMovieService movieService, IImportListMovieService importListMovieService)
|
||||
{
|
||||
_movieMetadataRepository = movieMetadataRepository;
|
||||
_movieService = movieService;
|
||||
_importListMovieService = importListMovieService;
|
||||
}
|
||||
|
||||
public MovieMetadata FindByTmdbId(int tmdbId)
|
||||
|
@ -56,5 +62,16 @@ namespace NzbDrone.Core.Movies
|
|||
{
|
||||
return _movieMetadataRepository.UpsertMany(movies);
|
||||
}
|
||||
|
||||
public void DeleteMany(List<MovieMetadata> movies)
|
||||
{
|
||||
foreach (var movie in movies)
|
||||
{
|
||||
if (!_importListMovieService.ExistsByMetadataId(movie.Id) && !_movieService.ExistsByMetadataId(movie.Id))
|
||||
{
|
||||
_movieMetadataRepository.Delete(movie);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Dapper;
|
||||
using NzbDrone.Core.Datastore;
|
||||
|
@ -30,6 +31,7 @@ namespace NzbDrone.Core.Movies
|
|||
List<int> AllMovieTmdbIds();
|
||||
Dictionary<int, List<int>> AllMovieTags();
|
||||
List<int> GetRecommendations();
|
||||
bool ExistsByMetadataId(int metadataId);
|
||||
}
|
||||
|
||||
public class MovieRepository : BasicRepository<Movie>, IMovieRepository
|
||||
|
@ -367,5 +369,12 @@ namespace NzbDrone.Core.Movies
|
|||
|
||||
return recommendations;
|
||||
}
|
||||
|
||||
public bool ExistsByMetadataId(int metadataId)
|
||||
{
|
||||
var movies = Query(x => x.MovieMetadataId == metadataId);
|
||||
|
||||
return movies.Any();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -48,6 +48,7 @@ namespace NzbDrone.Core.Movies
|
|||
List<int> GetRecommendedTmdbIds();
|
||||
bool MoviePathExists(string folder);
|
||||
void RemoveAddOptions(Movie movie);
|
||||
bool ExistsByMetadataId(int metadataId);
|
||||
}
|
||||
|
||||
public class MovieService : IMovieService, IHandle<MovieFileAddedEvent>,
|
||||
|
@ -389,6 +390,11 @@ namespace NzbDrone.Core.Movies
|
|||
return _movieRepository.GetRecommendations();
|
||||
}
|
||||
|
||||
public bool ExistsByMetadataId(int metadataId)
|
||||
{
|
||||
return _movieRepository.ExistsByMetadataId(metadataId);
|
||||
}
|
||||
|
||||
public void Handle(MovieFileAddedEvent message)
|
||||
{
|
||||
var movie = message.MovieFile.Movie;
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Instrumentation.Extensions;
|
||||
|
@ -48,6 +49,7 @@ namespace NzbDrone.Core.Movies
|
|||
_logger.ProgressInfo("Updating info for {0}", collection.Title);
|
||||
|
||||
MovieCollection collectionInfo;
|
||||
List<MovieMetadata> movies;
|
||||
|
||||
try
|
||||
{
|
||||
|
@ -68,8 +70,27 @@ namespace NzbDrone.Core.Movies
|
|||
collection.LastInfoSync = DateTime.UtcNow;
|
||||
collection.Images = collectionInfo.Images;
|
||||
|
||||
collectionInfo.Movies.ForEach(x => x.CollectionTmdbId = collection.TmdbId);
|
||||
_movieMetadataService.UpsertMany(collectionInfo.Movies);
|
||||
movies = collectionInfo.Movies;
|
||||
movies.ForEach(x => x.CollectionTmdbId = collection.TmdbId);
|
||||
|
||||
var existingMetaForCollection = _movieMetadataService.GetMoviesByCollectionTmdbId(collection.TmdbId);
|
||||
|
||||
var updateList = new List<MovieMetadata>();
|
||||
|
||||
foreach (var remoteMovie in movies)
|
||||
{
|
||||
var existing = existingMetaForCollection.FirstOrDefault(e => e.TmdbId == remoteMovie.TmdbId);
|
||||
|
||||
if (existingMetaForCollection.Any(x => x.TmdbId == remoteMovie.TmdbId))
|
||||
{
|
||||
existingMetaForCollection.Remove(existing);
|
||||
}
|
||||
|
||||
updateList.Add(remoteMovie);
|
||||
}
|
||||
|
||||
_movieMetadataService.UpsertMany(updateList);
|
||||
_movieMetadataService.DeleteMany(existingMetaForCollection);
|
||||
|
||||
_logger.Debug("Finished collection refresh for {0}", collection.Title);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue