mirror of
https://github.com/Radarr/Radarr.git
synced 2024-07-15 00:57:36 +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>()
|
Mocker.GetMock<IMovieService>()
|
||||||
.Setup(v => v.AllMoviePaths())
|
.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)
|
private void GivenExistingFolder(string folder)
|
||||||
@ -73,7 +73,7 @@ public void should_check_diskspace_for_movies_folders()
|
|||||||
[Test]
|
[Test]
|
||||||
public void should_check_diskspace_for_same_root_folder_only_once()
|
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(_moviesFolder);
|
||||||
GivenExistingFolder(_moviesFolder2);
|
GivenExistingFolder(_moviesFolder2);
|
||||||
|
@ -30,7 +30,7 @@ private void GivenMissingRootFolder()
|
|||||||
|
|
||||||
Mocker.GetMock<IMovieService>()
|
Mocker.GetMock<IMovieService>()
|
||||||
.Setup(s => s.AllMoviePaths())
|
.Setup(s => s.AllMoviePaths())
|
||||||
.Returns(movies.Select(x => x.Path).ToList());
|
.Returns(movies.ToDictionary(x => x.Id, x => x.Path));
|
||||||
|
|
||||||
Mocker.GetMock<IDiskProvider>()
|
Mocker.GetMock<IDiskProvider>()
|
||||||
.Setup(s => s.GetParentFolder(movies.First().Path))
|
.Setup(s => s.GetParentFolder(movies.First().Path))
|
||||||
@ -46,7 +46,7 @@ public void should_not_return_error_when_no_movie()
|
|||||||
{
|
{
|
||||||
Mocker.GetMock<IMovieService>()
|
Mocker.GetMock<IMovieService>()
|
||||||
.Setup(s => s.AllMoviePaths())
|
.Setup(s => s.AllMoviePaths())
|
||||||
.Returns(new List<string>());
|
.Returns(new Dictionary<int, string>());
|
||||||
|
|
||||||
Subject.Check().ShouldBeOk();
|
Subject.Check().ShouldBeOk();
|
||||||
}
|
}
|
||||||
|
@ -29,17 +29,21 @@ public void Setup()
|
|||||||
};
|
};
|
||||||
|
|
||||||
Mocker.GetMock<IMovieService>()
|
Mocker.GetMock<IMovieService>()
|
||||||
.Setup(s => s.GetAllMovies())
|
.Setup(s => s.AllMovieTitleSlugs())
|
||||||
.Returns(_movies);
|
.Returns(_movies.ToDictionary(m => m.Id, m => m.TitleSlug));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void should_not_be_valid_if_there_is_an_existing_movie_with_the_same_title_slug()
|
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()
|
var movie = Builder<Movie>.CreateNew()
|
||||||
.With(s => s.Id = 100)
|
.With(s => s.Id = 100)
|
||||||
.With(s => s.TitleSlug = _movies.First().TitleSlug)
|
.With(s => s.TitleSlug = _movies.First().TitleSlug)
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
_validator.Validate(movie).IsValid.Should().BeFalse();
|
_validator.Validate(movie).IsValid.Should().BeFalse();
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,7 @@ public void should_be_able_to_add_root_dir(string path)
|
|||||||
{
|
{
|
||||||
Mocker.GetMock<IMovieRepository>()
|
Mocker.GetMock<IMovieRepository>()
|
||||||
.Setup(s => s.AllMoviePaths())
|
.Setup(s => s.AllMoviePaths())
|
||||||
.Returns(new List<string>());
|
.Returns(new Dictionary<int, string>());
|
||||||
|
|
||||||
var root = new RootFolder { Path = path.AsOsAgnostic() };
|
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>()
|
Mocker.GetMock<IMovieRepository>()
|
||||||
.Setup(s => s.AllMoviePaths())
|
.Setup(s => s.AllMoviePaths())
|
||||||
.Returns(new List<string>());
|
.Returns(new Dictionary<int, string>());
|
||||||
|
|
||||||
Mocker.GetMock<IDiskProvider>()
|
Mocker.GetMock<IDiskProvider>()
|
||||||
.Setup(s => s.GetDirectories(rootFolder.Path))
|
.Setup(s => s.GetDirectories(rootFolder.Path))
|
||||||
|
@ -43,8 +43,8 @@ public List<DiskSpace> GetFreeSpace()
|
|||||||
private IEnumerable<string> GetMoviesRootPaths()
|
private IEnumerable<string> GetMoviesRootPaths()
|
||||||
{
|
{
|
||||||
return _movieService.AllMoviePaths()
|
return _movieService.AllMoviePaths()
|
||||||
.Where(s => _diskProvider.FolderExists(s))
|
.Where(s => _diskProvider.FolderExists(s.Value))
|
||||||
.Select(s => _diskProvider.GetPathRoot(s))
|
.Select(s => _diskProvider.GetPathRoot(s.Value))
|
||||||
.Distinct();
|
.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.
|
// 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()
|
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)
|
.Where(m => m != null && m.MountOptions != null && m.MountOptions.IsReadOnly)
|
||||||
.DistinctBy(m => m.RootDirectory)
|
.DistinctBy(m => m.RootDirectory)
|
||||||
.ToList();
|
.ToList();
|
||||||
|
@ -29,7 +29,7 @@ public RootFolderCheck(IMovieService movieService, IDiskProvider diskProvider, I
|
|||||||
public override HealthCheck Check()
|
public override HealthCheck Check()
|
||||||
{
|
{
|
||||||
var rootFolders = _movieService.AllMoviePaths()
|
var rootFolders = _movieService.AllMoviePaths()
|
||||||
.Select(s => _rootFolderService.GetBestRootFolderPath(s))
|
.Select(s => _rootFolderService.GetBestRootFolderPath(s.Value))
|
||||||
.Distinct();
|
.Distinct();
|
||||||
|
|
||||||
var missingRootFolders = rootFolders.Where(s => !_diskProvider.FolderExists(s))
|
var missingRootFolders = rootFolders.Where(s => !_diskProvider.FolderExists(s))
|
||||||
|
@ -27,7 +27,8 @@ public interface IMovieRepository : IBasicRepository<Movie>
|
|||||||
void SetFileId(int fileId, int movieId);
|
void SetFileId(int fileId, int movieId);
|
||||||
PagingSpec<Movie> MoviesWhereCutoffUnmet(PagingSpec<Movie> pagingSpec, List<QualitiesBelowCutoff> qualitiesBelowCutoff);
|
PagingSpec<Movie> MoviesWhereCutoffUnmet(PagingSpec<Movie> pagingSpec, List<QualitiesBelowCutoff> qualitiesBelowCutoff);
|
||||||
Movie FindByPath(string path);
|
Movie FindByPath(string path);
|
||||||
List<string> AllMoviePaths();
|
Dictionary<int, string> AllMoviePaths();
|
||||||
|
Dictionary<int, string> AllMovieTitleSlugs();
|
||||||
List<int> AllMovieTmdbIds();
|
List<int> AllMovieTmdbIds();
|
||||||
Dictionary<int, List<int>> AllMovieTags();
|
Dictionary<int, List<int>> AllMovieTags();
|
||||||
List<int> GetRecommendations();
|
List<int> GetRecommendations();
|
||||||
@ -275,11 +276,21 @@ public Movie FindByPath(string path)
|
|||||||
return Query(x => x.Path == path).FirstOrDefault();
|
return Query(x => x.Path == path).FirstOrDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<string> AllMoviePaths()
|
public Dictionary<int, string> AllMoviePaths()
|
||||||
{
|
{
|
||||||
using (var conn = _database.OpenConnection())
|
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())
|
using (var conn = _database.OpenConnection())
|
||||||
{
|
{
|
||||||
string strSql = "SELECT Id AS [Key], Tags AS [Value] FROM Movies";
|
var 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 conn.Query<KeyValuePair<int, List<int>>>(strSql).ToDictionary(x => x.Key, x => x.Value);
|
||||||
return tags;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,8 +31,9 @@ public interface IMovieService
|
|||||||
List<Movie> FindByTitleCandidates(string title, out string roman, out string arabic);
|
List<Movie> FindByTitleCandidates(string title, out string roman, out string arabic);
|
||||||
Movie FindByTitleSlug(string slug);
|
Movie FindByTitleSlug(string slug);
|
||||||
Movie FindByPath(string path);
|
Movie FindByPath(string path);
|
||||||
List<string> AllMoviePaths();
|
Dictionary<int, string> AllMoviePaths();
|
||||||
List<int> AllMovieTmdbIds();
|
List<int> AllMovieTmdbIds();
|
||||||
|
Dictionary<int, string> AllMovieTitleSlugs();
|
||||||
bool MovieExists(Movie movie);
|
bool MovieExists(Movie movie);
|
||||||
List<Movie> GetMoviesByFileId(int fileId);
|
List<Movie> GetMoviesByFileId(int fileId);
|
||||||
List<Movie> GetMoviesBetweenDates(DateTime start, DateTime end, bool includeUnmonitored);
|
List<Movie> GetMoviesBetweenDates(DateTime start, DateTime end, bool includeUnmonitored);
|
||||||
@ -189,11 +190,16 @@ public Movie FindByPath(string path)
|
|||||||
return _movieRepository.FindByPath(path);
|
return _movieRepository.FindByPath(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<string> AllMoviePaths()
|
public Dictionary<int, string> AllMoviePaths()
|
||||||
{
|
{
|
||||||
return _movieRepository.AllMoviePaths();
|
return _movieRepository.AllMoviePaths();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Dictionary<int, string> AllMovieTitleSlugs()
|
||||||
|
{
|
||||||
|
return _movieRepository.AllMovieTitleSlugs();
|
||||||
|
}
|
||||||
|
|
||||||
public List<int> AllMovieTmdbIds()
|
public List<int> AllMovieTmdbIds()
|
||||||
{
|
{
|
||||||
return _movieRepository.AllMovieTmdbIds();
|
return _movieRepository.AllMovieTmdbIds();
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using FluentValidation.Validators;
|
using FluentValidation.Validators;
|
||||||
using NzbDrone.Common.Extensions;
|
using NzbDrone.Common.Extensions;
|
||||||
@ -25,16 +26,18 @@ protected override bool IsValid(PropertyValidatorContext context)
|
|||||||
var instanceId = (int)instance.Id;
|
var instanceId = (int)instance.Id;
|
||||||
var slug = context.PropertyValue.ToString();
|
var slug = context.PropertyValue.ToString();
|
||||||
|
|
||||||
var conflictingMovie = _movieService.GetAllMovies()
|
var conflictingId = _movieService.AllMovieTitleSlugs()
|
||||||
.FirstOrDefault(s => s.TitleSlug.IsNotNullOrWhiteSpace() &&
|
.FirstOrDefault(s => s.Value.IsNotNullOrWhiteSpace() &&
|
||||||
s.TitleSlug.Equals(context.PropertyValue.ToString()) &&
|
s.Value.Equals(context.PropertyValue.ToString()) &&
|
||||||
s.Id != instanceId);
|
s.Key != instanceId);
|
||||||
|
|
||||||
if (conflictingMovie == null)
|
if (conflictingId.Equals(default(KeyValuePair<int, string>)))
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var conflictingMovie = _movieService.GetMovie(conflictingId.Key);
|
||||||
|
|
||||||
context.MessageFormatter.AppendArgument("slug", slug);
|
context.MessageFormatter.AppendArgument("slug", slug);
|
||||||
context.MessageFormatter.AppendArgument("movieTitle", conflictingMovie.Title);
|
context.MessageFormatter.AppendArgument("movieTitle", conflictingMovie.Title);
|
||||||
|
|
||||||
|
@ -67,7 +67,7 @@ public List<RootFolder> AllWithUnmappedFolders()
|
|||||||
{
|
{
|
||||||
var rootFolders = _rootFolderRepository.All().ToList();
|
var rootFolders = _rootFolderRepository.All().ToList();
|
||||||
|
|
||||||
var moviePaths = _movieRepository.AllMoviePaths().ToList();
|
var moviePaths = _movieRepository.AllMoviePaths();
|
||||||
|
|
||||||
rootFolders.ForEach(folder =>
|
rootFolders.ForEach(folder =>
|
||||||
{
|
{
|
||||||
@ -116,7 +116,7 @@ public RootFolder Add(RootFolder rootFolder)
|
|||||||
|
|
||||||
_rootFolderRepository.Insert(rootFolder);
|
_rootFolderRepository.Insert(rootFolder);
|
||||||
|
|
||||||
var moviePaths = _movieRepository.AllMoviePaths().ToList();
|
var moviePaths = _movieRepository.AllMoviePaths();
|
||||||
|
|
||||||
GetDetails(rootFolder, moviePaths, true);
|
GetDetails(rootFolder, moviePaths, true);
|
||||||
|
|
||||||
@ -128,7 +128,7 @@ public void Remove(int id)
|
|||||||
_rootFolderRepository.Delete(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");
|
_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 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)
|
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)
|
public RootFolder Get(int id, bool timeout)
|
||||||
{
|
{
|
||||||
var rootFolder = _rootFolderRepository.Get(id);
|
var rootFolder = _rootFolderRepository.Get(id);
|
||||||
var moviePaths = _movieRepository.AllMoviePaths().ToList();
|
var moviePaths = _movieRepository.AllMoviePaths();
|
||||||
|
|
||||||
GetDetails(rootFolder, moviePaths, timeout);
|
GetDetails(rootFolder, moviePaths, timeout);
|
||||||
|
|
||||||
@ -188,7 +188,7 @@ public string GetBestRootFolderPath(string path)
|
|||||||
return possibleRootFolder.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(() =>
|
Task.Run(() =>
|
||||||
{
|
{
|
||||||
|
@ -22,7 +22,7 @@ protected override bool IsValid(PropertyValidatorContext context)
|
|||||||
return true;
|
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 FluentValidation.Validators;
|
||||||
using NzbDrone.Common.Extensions;
|
using NzbDrone.Common.Extensions;
|
||||||
using NzbDrone.Core.Movies;
|
using NzbDrone.Core.Movies;
|
||||||
@ -26,7 +27,7 @@ protected override bool IsValid(PropertyValidatorContext context)
|
|||||||
|
|
||||||
context.MessageFormatter.AppendArgument("moviePath", context.PropertyValue.ToString());
|
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