mirror of
https://github.com/Radarr/Radarr
synced 2026-05-09 11:10:23 +02: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.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using NzbDrone.Core.Datastore;
|
using NzbDrone.Core.Datastore;
|
||||||
using NzbDrone.Core.Messaging.Events;
|
using NzbDrone.Core.Messaging.Events;
|
||||||
|
|
||||||
|
|
@ -7,6 +8,7 @@ namespace NzbDrone.Core.ImportLists.ImportListMovies
|
||||||
public interface IImportListMovieRepository : IBasicRepository<ImportListMovie>
|
public interface IImportListMovieRepository : IBasicRepository<ImportListMovie>
|
||||||
{
|
{
|
||||||
List<ImportListMovie> GetAllForLists(List<int> listIds);
|
List<ImportListMovie> GetAllForLists(List<int> listIds);
|
||||||
|
bool ExistsByMetadataId(int metadataId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ImportListMovieRepository : BasicRepository<ImportListMovie>, IImportListMovieRepository
|
public class ImportListMovieRepository : BasicRepository<ImportListMovie>, IImportListMovieRepository
|
||||||
|
|
@ -20,5 +22,12 @@ public List<ImportListMovie> GetAllForLists(List<int> listIds)
|
||||||
{
|
{
|
||||||
return Query(x => listIds.Contains(x.ListId));
|
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 @@ public interface IImportListMovieService
|
||||||
List<ImportListMovie> SyncMoviesForList(List<ImportListMovie> listMovies, int listId);
|
List<ImportListMovie> SyncMoviesForList(List<ImportListMovie> listMovies, int listId);
|
||||||
void RemoveListMovie(ImportListMovie listMovie);
|
void RemoveListMovie(ImportListMovie listMovie);
|
||||||
ImportListMovie GetById(int id);
|
ImportListMovie GetById(int id);
|
||||||
|
bool ExistsByMetadataId(int metadataId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ImportListMovieService : IImportListMovieService, IHandleAsync<ProviderDeletedEvent<IImportList>>
|
public class ImportListMovieService : IImportListMovieService, IHandleAsync<ProviderDeletedEvent<IImportList>>
|
||||||
|
|
@ -79,5 +80,10 @@ public void HandleAsync(ProviderDeletedEvent<IImportList> message)
|
||||||
var moviesOnList = _importListMovieRepository.GetAllForLists(new List<int> { message.ProviderId });
|
var moviesOnList = _importListMovieRepository.GetAllForLists(new List<int> { message.ProviderId });
|
||||||
_importListMovieRepository.DeleteMany(moviesOnList);
|
_importListMovieRepository.DeleteMany(moviesOnList);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool ExistsByMetadataId(int metadataId)
|
||||||
|
{
|
||||||
|
return _importListMovieRepository.ExistsByMetadataId(metadataId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using NzbDrone.Core.ImportLists.ImportListMovies;
|
||||||
|
|
||||||
namespace NzbDrone.Core.Movies
|
namespace NzbDrone.Core.Movies
|
||||||
{
|
{
|
||||||
|
|
@ -11,15 +12,20 @@ public interface IMovieMetadataService
|
||||||
List<MovieMetadata> GetMoviesByCollectionTmdbId(int collectionId);
|
List<MovieMetadata> GetMoviesByCollectionTmdbId(int collectionId);
|
||||||
bool Upsert(MovieMetadata movie);
|
bool Upsert(MovieMetadata movie);
|
||||||
bool UpsertMany(List<MovieMetadata> movies);
|
bool UpsertMany(List<MovieMetadata> movies);
|
||||||
|
void DeleteMany(List<MovieMetadata> movies);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class MovieMetadataService : IMovieMetadataService
|
public class MovieMetadataService : IMovieMetadataService
|
||||||
{
|
{
|
||||||
private readonly IMovieMetadataRepository _movieMetadataRepository;
|
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;
|
_movieMetadataRepository = movieMetadataRepository;
|
||||||
|
_movieService = movieService;
|
||||||
|
_importListMovieService = importListMovieService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MovieMetadata FindByTmdbId(int tmdbId)
|
public MovieMetadata FindByTmdbId(int tmdbId)
|
||||||
|
|
@ -56,5 +62,16 @@ public bool UpsertMany(List<MovieMetadata> movies)
|
||||||
{
|
{
|
||||||
return _movieMetadataRepository.UpsertMany(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;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Dapper;
|
using Dapper;
|
||||||
using NzbDrone.Core.Datastore;
|
using NzbDrone.Core.Datastore;
|
||||||
|
|
@ -30,6 +31,7 @@ public interface IMovieRepository : IBasicRepository<Movie>
|
||||||
List<int> AllMovieTmdbIds();
|
List<int> AllMovieTmdbIds();
|
||||||
Dictionary<int, List<int>> AllMovieTags();
|
Dictionary<int, List<int>> AllMovieTags();
|
||||||
List<int> GetRecommendations();
|
List<int> GetRecommendations();
|
||||||
|
bool ExistsByMetadataId(int metadataId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class MovieRepository : BasicRepository<Movie>, IMovieRepository
|
public class MovieRepository : BasicRepository<Movie>, IMovieRepository
|
||||||
|
|
@ -367,5 +369,12 @@ SELECT CAST(""j"".""value"" AS INT) AS ""Rec"" FROM ""MovieMetadata"" CROSS JOIN
|
||||||
|
|
||||||
return recommendations;
|
return recommendations;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool ExistsByMetadataId(int metadataId)
|
||||||
|
{
|
||||||
|
var movies = Query(x => x.MovieMetadataId == metadataId);
|
||||||
|
|
||||||
|
return movies.Any();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -48,6 +48,7 @@ public interface IMovieService
|
||||||
List<int> GetRecommendedTmdbIds();
|
List<int> GetRecommendedTmdbIds();
|
||||||
bool MoviePathExists(string folder);
|
bool MoviePathExists(string folder);
|
||||||
void RemoveAddOptions(Movie movie);
|
void RemoveAddOptions(Movie movie);
|
||||||
|
bool ExistsByMetadataId(int metadataId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class MovieService : IMovieService, IHandle<MovieFileAddedEvent>,
|
public class MovieService : IMovieService, IHandle<MovieFileAddedEvent>,
|
||||||
|
|
@ -389,6 +390,11 @@ public List<int> GetRecommendedTmdbIds()
|
||||||
return _movieRepository.GetRecommendations();
|
return _movieRepository.GetRecommendations();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool ExistsByMetadataId(int metadataId)
|
||||||
|
{
|
||||||
|
return _movieRepository.ExistsByMetadataId(metadataId);
|
||||||
|
}
|
||||||
|
|
||||||
public void Handle(MovieFileAddedEvent message)
|
public void Handle(MovieFileAddedEvent message)
|
||||||
{
|
{
|
||||||
var movie = message.MovieFile.Movie;
|
var movie = message.MovieFile.Movie;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using NLog;
|
using NLog;
|
||||||
using NzbDrone.Common.Instrumentation.Extensions;
|
using NzbDrone.Common.Instrumentation.Extensions;
|
||||||
|
|
@ -48,6 +49,7 @@ private MovieCollection RefreshCollectionInfo(int collectionId)
|
||||||
_logger.ProgressInfo("Updating info for {0}", collection.Title);
|
_logger.ProgressInfo("Updating info for {0}", collection.Title);
|
||||||
|
|
||||||
MovieCollection collectionInfo;
|
MovieCollection collectionInfo;
|
||||||
|
List<MovieMetadata> movies;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
@ -68,8 +70,27 @@ private MovieCollection RefreshCollectionInfo(int collectionId)
|
||||||
collection.LastInfoSync = DateTime.UtcNow;
|
collection.LastInfoSync = DateTime.UtcNow;
|
||||||
collection.Images = collectionInfo.Images;
|
collection.Images = collectionInfo.Images;
|
||||||
|
|
||||||
collectionInfo.Movies.ForEach(x => x.CollectionTmdbId = collection.TmdbId);
|
movies = collectionInfo.Movies;
|
||||||
_movieMetadataService.UpsertMany(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);
|
_logger.Debug("Finished collection refresh for {0}", collection.Title);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue