mirror of
https://github.com/Radarr/Radarr.git
synced 2024-10-05 15:47:20 +02:00
Don't call AllMovies twice for each AddMovie validation
This commit is contained in:
parent
98f0769c5d
commit
4550ef13a4
@ -48,7 +48,7 @@ private void GivenMovies(params Movie[] movies)
|
||||
{
|
||||
Mocker.GetMock<IMovieService>()
|
||||
.Setup(v => v.AllMoviePaths())
|
||||
.Returns(movies.Select(x => x.Path).ToList());
|
||||
.Returns(movies.ToDictionary(x => x.Id, x => x.Path));
|
||||
}
|
||||
|
||||
private void GivenExistingFolder(string folder)
|
||||
@ -73,7 +73,7 @@ public void should_check_diskspace_for_movies_folders()
|
||||
[Test]
|
||||
public void should_check_diskspace_for_same_root_folder_only_once()
|
||||
{
|
||||
GivenMovies(new Movie { Path = _moviesFolder }, new Movie { Path = _moviesFolder2 });
|
||||
GivenMovies(new Movie { Id = 1, Path = _moviesFolder }, new Movie { Id = 2, Path = _moviesFolder2 });
|
||||
|
||||
GivenExistingFolder(_moviesFolder);
|
||||
GivenExistingFolder(_moviesFolder2);
|
||||
|
@ -30,7 +30,7 @@ private void GivenMissingRootFolder()
|
||||
|
||||
Mocker.GetMock<IMovieService>()
|
||||
.Setup(s => s.AllMoviePaths())
|
||||
.Returns(movies.Select(x => x.Path).ToList());
|
||||
.Returns(movies.ToDictionary(x => x.Id, x => x.Path));
|
||||
|
||||
Mocker.GetMock<IDiskProvider>()
|
||||
.Setup(s => s.GetParentFolder(movies.First().Path))
|
||||
@ -46,7 +46,7 @@ public void should_not_return_error_when_no_movie()
|
||||
{
|
||||
Mocker.GetMock<IMovieService>()
|
||||
.Setup(s => s.AllMoviePaths())
|
||||
.Returns(new List<string>());
|
||||
.Returns(new Dictionary<int, string>());
|
||||
|
||||
Subject.Check().ShouldBeOk();
|
||||
}
|
||||
|
@ -29,17 +29,21 @@ public void Setup()
|
||||
};
|
||||
|
||||
Mocker.GetMock<IMovieService>()
|
||||
.Setup(s => s.GetAllMovies())
|
||||
.Returns(_movies);
|
||||
.Setup(s => s.AllMovieTitleSlugs())
|
||||
.Returns(_movies.ToDictionary(m => m.Id, m => m.TitleSlug));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_not_be_valid_if_there_is_an_existing_movie_with_the_same_title_slug()
|
||||
{
|
||||
Mocker.GetMock<IMovieService>()
|
||||
.Setup(s => s.GetMovie(_movies.First().Id))
|
||||
.Returns(_movies.First());
|
||||
|
||||
var movie = Builder<Movie>.CreateNew()
|
||||
.With(s => s.Id = 100)
|
||||
.With(s => s.TitleSlug = _movies.First().TitleSlug)
|
||||
.Build();
|
||||
.With(s => s.Id = 100)
|
||||
.With(s => s.TitleSlug = _movies.First().TitleSlug)
|
||||
.Build();
|
||||
|
||||
_validator.Validate(movie).IsValid.Should().BeFalse();
|
||||
}
|
||||
|
@ -47,7 +47,7 @@ public void should_be_able_to_add_root_dir(string path)
|
||||
{
|
||||
Mocker.GetMock<IMovieRepository>()
|
||||
.Setup(s => s.AllMoviePaths())
|
||||
.Returns(new List<string>());
|
||||
.Returns(new Dictionary<int, string>());
|
||||
|
||||
var root = new RootFolder { Path = path.AsOsAgnostic() };
|
||||
|
||||
@ -140,7 +140,7 @@ public void should_get_root_folder_with_subfolders_excluding_special_sub_folders
|
||||
|
||||
Mocker.GetMock<IMovieRepository>()
|
||||
.Setup(s => s.AllMoviePaths())
|
||||
.Returns(new List<string>());
|
||||
.Returns(new Dictionary<int, string>());
|
||||
|
||||
Mocker.GetMock<IDiskProvider>()
|
||||
.Setup(s => s.GetDirectories(rootFolder.Path))
|
||||
|
@ -43,8 +43,8 @@ public List<DiskSpace> GetFreeSpace()
|
||||
private IEnumerable<string> GetMoviesRootPaths()
|
||||
{
|
||||
return _movieService.AllMoviePaths()
|
||||
.Where(s => _diskProvider.FolderExists(s))
|
||||
.Select(s => _diskProvider.GetPathRoot(s))
|
||||
.Where(s => _diskProvider.FolderExists(s.Value))
|
||||
.Select(s => _diskProvider.GetPathRoot(s.Value))
|
||||
.Distinct();
|
||||
}
|
||||
|
||||
|
@ -22,7 +22,7 @@ public override HealthCheck Check()
|
||||
{
|
||||
// Not best for optimization but due to possible symlinks and junctions, we get mounts based on series path so internals can handle mount resolution.
|
||||
var mounts = _movieService.AllMoviePaths()
|
||||
.Select(p => _diskProvider.GetMount(p))
|
||||
.Select(p => _diskProvider.GetMount(p.Value))
|
||||
.Where(m => m != null && m.MountOptions != null && m.MountOptions.IsReadOnly)
|
||||
.DistinctBy(m => m.RootDirectory)
|
||||
.ToList();
|
||||
|
@ -29,7 +29,7 @@ public RootFolderCheck(IMovieService movieService, IDiskProvider diskProvider, I
|
||||
public override HealthCheck Check()
|
||||
{
|
||||
var rootFolders = _movieService.AllMoviePaths()
|
||||
.Select(s => _rootFolderService.GetBestRootFolderPath(s))
|
||||
.Select(s => _rootFolderService.GetBestRootFolderPath(s.Value))
|
||||
.Distinct();
|
||||
|
||||
var missingRootFolders = rootFolders.Where(s => !_diskProvider.FolderExists(s))
|
||||
|
@ -27,7 +27,8 @@ public interface IMovieRepository : IBasicRepository<Movie>
|
||||
void SetFileId(int fileId, int movieId);
|
||||
PagingSpec<Movie> MoviesWhereCutoffUnmet(PagingSpec<Movie> pagingSpec, List<QualitiesBelowCutoff> qualitiesBelowCutoff);
|
||||
Movie FindByPath(string path);
|
||||
List<string> AllMoviePaths();
|
||||
Dictionary<int, string> AllMoviePaths();
|
||||
Dictionary<int, string> AllMovieTitleSlugs();
|
||||
List<int> AllMovieTmdbIds();
|
||||
Dictionary<int, List<int>> AllMovieTags();
|
||||
List<int> GetRecommendations();
|
||||
@ -275,11 +276,21 @@ public Movie FindByPath(string path)
|
||||
return Query(x => x.Path == path).FirstOrDefault();
|
||||
}
|
||||
|
||||
public List<string> AllMoviePaths()
|
||||
public Dictionary<int, string> AllMoviePaths()
|
||||
{
|
||||
using (var conn = _database.OpenConnection())
|
||||
{
|
||||
return conn.Query<string>("SELECT Path FROM Movies").ToList();
|
||||
var strSql = "SELECT Id AS [Key], Path AS [Value] FROM Movies";
|
||||
return conn.Query<KeyValuePair<int, string>>(strSql).ToDictionary(x => x.Key, x => x.Value);
|
||||
}
|
||||
}
|
||||
|
||||
public Dictionary<int, string> AllMovieTitleSlugs()
|
||||
{
|
||||
using (var conn = _database.OpenConnection())
|
||||
{
|
||||
var strSql = "SELECT Id AS [Key], TitleSlug AS [Value] FROM Movies";
|
||||
return conn.Query<KeyValuePair<int, string>>(strSql).ToDictionary(x => x.Key, x => x.Value);
|
||||
}
|
||||
}
|
||||
|
||||
@ -295,9 +306,8 @@ public Dictionary<int, List<int>> AllMovieTags()
|
||||
{
|
||||
using (var conn = _database.OpenConnection())
|
||||
{
|
||||
string strSql = "SELECT Id AS [Key], Tags AS [Value] FROM Movies";
|
||||
var tags = conn.Query<KeyValuePair<int, List<int>>>(strSql).ToDictionary(x => x.Key, x => x.Value);
|
||||
return tags;
|
||||
var strSql = "SELECT Id AS [Key], Tags AS [Value] FROM Movies";
|
||||
return conn.Query<KeyValuePair<int, List<int>>>(strSql).ToDictionary(x => x.Key, x => x.Value);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -31,8 +31,9 @@ public interface IMovieService
|
||||
List<Movie> FindByTitleCandidates(string title, out string roman, out string arabic);
|
||||
Movie FindByTitleSlug(string slug);
|
||||
Movie FindByPath(string path);
|
||||
List<string> AllMoviePaths();
|
||||
Dictionary<int, string> AllMoviePaths();
|
||||
List<int> AllMovieTmdbIds();
|
||||
Dictionary<int, string> AllMovieTitleSlugs();
|
||||
bool MovieExists(Movie movie);
|
||||
List<Movie> GetMoviesByFileId(int fileId);
|
||||
List<Movie> GetMoviesBetweenDates(DateTime start, DateTime end, bool includeUnmonitored);
|
||||
@ -189,11 +190,16 @@ public Movie FindByPath(string path)
|
||||
return _movieRepository.FindByPath(path);
|
||||
}
|
||||
|
||||
public List<string> AllMoviePaths()
|
||||
public Dictionary<int, string> AllMoviePaths()
|
||||
{
|
||||
return _movieRepository.AllMoviePaths();
|
||||
}
|
||||
|
||||
public Dictionary<int, string> AllMovieTitleSlugs()
|
||||
{
|
||||
return _movieRepository.AllMovieTitleSlugs();
|
||||
}
|
||||
|
||||
public List<int> AllMovieTmdbIds()
|
||||
{
|
||||
return _movieRepository.AllMovieTmdbIds();
|
||||
|
@ -1,3 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using FluentValidation.Validators;
|
||||
using NzbDrone.Common.Extensions;
|
||||
@ -25,16 +26,18 @@ protected override bool IsValid(PropertyValidatorContext context)
|
||||
var instanceId = (int)instance.Id;
|
||||
var slug = context.PropertyValue.ToString();
|
||||
|
||||
var conflictingMovie = _movieService.GetAllMovies()
|
||||
.FirstOrDefault(s => s.TitleSlug.IsNotNullOrWhiteSpace() &&
|
||||
s.TitleSlug.Equals(context.PropertyValue.ToString()) &&
|
||||
s.Id != instanceId);
|
||||
var conflictingId = _movieService.AllMovieTitleSlugs()
|
||||
.FirstOrDefault(s => s.Value.IsNotNullOrWhiteSpace() &&
|
||||
s.Value.Equals(context.PropertyValue.ToString()) &&
|
||||
s.Key != instanceId);
|
||||
|
||||
if (conflictingMovie == null)
|
||||
if (conflictingId.Equals(default(KeyValuePair<int, string>)))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
var conflictingMovie = _movieService.GetMovie(conflictingId.Key);
|
||||
|
||||
context.MessageFormatter.AppendArgument("slug", slug);
|
||||
context.MessageFormatter.AppendArgument("movieTitle", conflictingMovie.Title);
|
||||
|
||||
|
@ -67,7 +67,7 @@ public List<RootFolder> AllWithUnmappedFolders()
|
||||
{
|
||||
var rootFolders = _rootFolderRepository.All().ToList();
|
||||
|
||||
var moviePaths = _movieRepository.AllMoviePaths().ToList();
|
||||
var moviePaths = _movieRepository.AllMoviePaths();
|
||||
|
||||
rootFolders.ForEach(folder =>
|
||||
{
|
||||
@ -116,7 +116,7 @@ public RootFolder Add(RootFolder rootFolder)
|
||||
|
||||
_rootFolderRepository.Insert(rootFolder);
|
||||
|
||||
var moviePaths = _movieRepository.AllMoviePaths().ToList();
|
||||
var moviePaths = _movieRepository.AllMoviePaths();
|
||||
|
||||
GetDetails(rootFolder, moviePaths, true);
|
||||
|
||||
@ -128,7 +128,7 @@ public void Remove(int id)
|
||||
_rootFolderRepository.Delete(id);
|
||||
}
|
||||
|
||||
private List<UnmappedFolder> GetUnmappedFolders(string path, List<string> moviePaths)
|
||||
private List<UnmappedFolder> GetUnmappedFolders(string path, Dictionary<int, string> moviePaths)
|
||||
{
|
||||
_logger.Debug("Generating list of unmapped folders");
|
||||
|
||||
@ -146,7 +146,7 @@ private List<UnmappedFolder> GetUnmappedFolders(string path, List<string> movieP
|
||||
}
|
||||
|
||||
var possibleMovieFolders = _diskProvider.GetDirectories(path).ToList();
|
||||
var unmappedFolders = possibleMovieFolders.Except(moviePaths.Select(s => s), PathEqualityComparer.Instance).ToList();
|
||||
var unmappedFolders = possibleMovieFolders.Except(moviePaths.Select(s => s.Value), PathEqualityComparer.Instance).ToList();
|
||||
|
||||
foreach (string unmappedFolder in unmappedFolders)
|
||||
{
|
||||
@ -167,7 +167,7 @@ private List<UnmappedFolder> GetUnmappedFolders(string path, List<string> movieP
|
||||
public RootFolder Get(int id, bool timeout)
|
||||
{
|
||||
var rootFolder = _rootFolderRepository.Get(id);
|
||||
var moviePaths = _movieRepository.AllMoviePaths().ToList();
|
||||
var moviePaths = _movieRepository.AllMoviePaths();
|
||||
|
||||
GetDetails(rootFolder, moviePaths, timeout);
|
||||
|
||||
@ -188,7 +188,7 @@ public string GetBestRootFolderPath(string path)
|
||||
return possibleRootFolder.Path;
|
||||
}
|
||||
|
||||
private void GetDetails(RootFolder rootFolder, List<string> moviePaths, bool timeout)
|
||||
private void GetDetails(RootFolder rootFolder, Dictionary<int, string> moviePaths, bool timeout)
|
||||
{
|
||||
Task.Run(() =>
|
||||
{
|
||||
|
@ -22,7 +22,7 @@ protected override bool IsValid(PropertyValidatorContext context)
|
||||
return true;
|
||||
}
|
||||
|
||||
return !_movieService.AllMoviePaths().Any(s => context.PropertyValue.ToString().IsParentPath(s));
|
||||
return !_movieService.AllMoviePaths().Any(s => context.PropertyValue.ToString().IsParentPath(s.Value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
using System.Linq;
|
||||
using FluentValidation.Validators;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Core.Movies;
|
||||
@ -26,7 +27,7 @@ protected override bool IsValid(PropertyValidatorContext context)
|
||||
|
||||
context.MessageFormatter.AppendArgument("moviePath", context.PropertyValue.ToString());
|
||||
|
||||
return !_moviesService.GetAllMovies().Exists(s => s.Path.PathEquals(context.PropertyValue.ToString()) && s.Id != instanceId);
|
||||
return !_moviesService.AllMoviePaths().Any(s => s.Value.PathEquals(context.PropertyValue.ToString()) && s.Key != instanceId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user