mirror of
https://github.com/Readarr/Readarr
synced 2025-12-27 18:54:54 +01:00
Improve use of All() for Path related queries
Signed-off-by: Robin Dadswell <robin@dadswell.email>
This commit is contained in:
parent
cf0439d4c5
commit
3af8051e3c
10 changed files with 39 additions and 22 deletions
|
|
@ -33,9 +33,9 @@ public void Setup()
|
|||
_metadata = Builder<MetadataFile>.CreateListOfSize(1)
|
||||
.Build().ToList();
|
||||
|
||||
Mocker.GetMock<IAuthorService>()
|
||||
.Setup(c => c.GetAllAuthors())
|
||||
.Returns(_artist);
|
||||
Mocker.GetMock<IArtistService>()
|
||||
.Setup(c => c.AllArtistPaths())
|
||||
.Returns(_artist.ToDictionary(x => x.Id, x => x.Path));
|
||||
|
||||
Mocker.GetMock<IMetadataFileService>()
|
||||
.Setup(c => c.GetFilesByAuthor(_artist.First().Id))
|
||||
|
|
@ -73,7 +73,7 @@ public void should_not_run_if_flag_is_false()
|
|||
Subject.Clean();
|
||||
|
||||
Mocker.GetMock<IConfigService>().VerifySet(c => c.CleanupMetadataImages = true, Times.Never());
|
||||
Mocker.GetMock<IAuthorService>().Verify(c => c.GetAllAuthors(), Times.Never());
|
||||
Mocker.GetMock<IArtistService>().Verify(c => c.GetAllAuthors(), Times.Never());
|
||||
|
||||
AssertImageWasNotRemoved();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,8 +25,8 @@ private void GivenMissingRootFolder()
|
|||
.ToList();
|
||||
|
||||
Mocker.GetMock<IAuthorService>()
|
||||
.Setup(s => s.GetAllAuthors())
|
||||
.Returns(artist);
|
||||
.Setup(s => s.AllAuthorPaths())
|
||||
.Returns(artist.ToDictionary(x => x.Id, x => x.Path));
|
||||
|
||||
Mocker.GetMock<IImportListFactory>()
|
||||
.Setup(s => s.All())
|
||||
|
|
@ -45,8 +45,8 @@ private void GivenMissingRootFolder()
|
|||
public void should_not_return_error_when_no_artist()
|
||||
{
|
||||
Mocker.GetMock<IAuthorService>()
|
||||
.Setup(s => s.GetAllAuthors())
|
||||
.Returns(new List<Author>());
|
||||
.Setup(s => s.AllAuthorPaths())
|
||||
.Returns(new Dictionary<int, string>());
|
||||
|
||||
Mocker.GetMock<IImportListFactory>()
|
||||
.Setup(s => s.All())
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ public interface IAuthorRepository : IBasicRepository<Author>
|
|||
bool AuthorPathExists(string path);
|
||||
Author FindByName(string cleanName);
|
||||
Author FindById(string foreignAuthorId);
|
||||
Dictionary<int, string> AllAuthorPaths();
|
||||
Author GetAuthorByMetadataId(int authorMetadataId);
|
||||
List<Author> GetAuthorsByMetadataId(IEnumerable<int> authorMetadataId);
|
||||
}
|
||||
|
|
@ -55,6 +56,15 @@ public Author FindByName(string cleanName)
|
|||
return Query(s => s.CleanName == cleanName).ExclusiveOrDefault();
|
||||
}
|
||||
|
||||
public Dictionary<int, string> AllAuthorPaths()
|
||||
{
|
||||
using (var conn = _database.OpenConnection())
|
||||
{
|
||||
var strSql = "SELECT Id AS [Key], Path AS [Value] FROM Authors";
|
||||
return conn.Query<KeyValuePair<int, string>>(strSql).ToDictionary(x => x.Key, x => x.Value);
|
||||
}
|
||||
}
|
||||
|
||||
public Author GetAuthorByMetadataId(int authorMetadataId)
|
||||
{
|
||||
return Query(s => s.AuthorMetadataId == authorMetadataId).SingleOrDefault();
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@ public interface IAuthorService
|
|||
List<Author> AllForTag(int tagId);
|
||||
Author UpdateAuthor(Author author);
|
||||
List<Author> UpdateAuthors(List<Author> authors, bool useExistingRelativeFolder);
|
||||
Dictionary<int, string> AllAuthorPaths();
|
||||
bool AuthorPathExists(string folder);
|
||||
void RemoveAddOptions(Author author);
|
||||
}
|
||||
|
|
@ -194,6 +195,11 @@ public List<Author> GetAllAuthors()
|
|||
return _cache.Get("GetAllAuthors", () => _authorRepository.All().ToList(), TimeSpan.FromSeconds(30));
|
||||
}
|
||||
|
||||
public Dictionary<int, string> AllAuthorPaths()
|
||||
{
|
||||
return _authorRepository.AllAuthorPaths();
|
||||
}
|
||||
|
||||
public List<Author> AllForTag(int tagId)
|
||||
{
|
||||
return GetAllAuthors().Where(s => s.Tags.Contains(tagId))
|
||||
|
|
|
|||
|
|
@ -19,8 +19,8 @@ public MountCheck(IDiskProvider diskProvider, IAuthorService authorService)
|
|||
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 = _authorService.GetAllAuthors()
|
||||
.Select(author => _diskProvider.GetMount(author.Path))
|
||||
var mounts = _authorService.AllAuthorPaths()
|
||||
.Select(path => _diskProvider.GetMount(path.Value))
|
||||
.Where(m => m != null && m.MountOptions != null && m.MountOptions.IsReadOnly)
|
||||
.DistinctBy(m => m.RootDirectory)
|
||||
.ToList();
|
||||
|
|
|
|||
|
|
@ -28,8 +28,8 @@ public RootFolderCheck(IArtistService authorService, IImportListFactory importLi
|
|||
|
||||
public override HealthCheck Check()
|
||||
{
|
||||
var rootFolders = _artistService.GetAllAuthors()
|
||||
.Select(s => _rootFolderService.GetBestRootFolderPath(s.Path))
|
||||
var rootFolders = _authorService.AllAuthorPaths()
|
||||
.Select(s => _rootFolderService.GetBestRootFolderPath(s.Value))
|
||||
.Distinct();
|
||||
|
||||
var missingRootFolders = rootFolders.Where(s => !_diskProvider.FolderExists(s))
|
||||
|
|
|
|||
|
|
@ -38,19 +38,19 @@ public void Clean()
|
|||
return;
|
||||
}
|
||||
|
||||
var artists = _authorService.GetAllAuthors();
|
||||
var artists = _authorService.AllAuthorPaths();
|
||||
var imageExtensions = new List<string> { ".jpg", ".png", ".gif" };
|
||||
|
||||
foreach (var author in artists)
|
||||
{
|
||||
var images = _metaFileService.GetFilesByAuthor(author.Id)
|
||||
var images = _metaFileService.GetFilesByAuthor(author.Key)
|
||||
.Where(c => c.LastUpdated > new DateTime(2014, 12, 27) && imageExtensions.Any(x => c.RelativePath.EndsWith(x, StringComparison.InvariantCultureIgnoreCase)));
|
||||
|
||||
foreach (var image in images)
|
||||
{
|
||||
try
|
||||
{
|
||||
var path = Path.Combine(author.Path, image.RelativePath);
|
||||
var path = Path.Combine(author.Value, image.RelativePath);
|
||||
if (!IsValid(path))
|
||||
{
|
||||
_logger.Debug("Deleting invalid image file " + path);
|
||||
|
|
|
|||
|
|
@ -104,22 +104,22 @@ public void HandleAsync(AuthorDeletedEvent message)
|
|||
if (message.DeleteFiles)
|
||||
{
|
||||
var author = message.Author;
|
||||
var allArtists = _authorService.GetAllAuthors();
|
||||
var allArtists = _authorService.AllAuthorPaths();
|
||||
|
||||
foreach (var s in allArtists)
|
||||
{
|
||||
if (s.Id == author.Id)
|
||||
if (s.Key == author.Id)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (author.Path.IsParentPath(s.Path))
|
||||
if (author.Path.IsParentPath(s.Value))
|
||||
{
|
||||
_logger.Error("Author path: '{0}' is a parent of another author, not deleting files.", author.Path);
|
||||
return;
|
||||
}
|
||||
|
||||
if (author.Path.PathEquals(s.Path))
|
||||
if (author.Path.PathEquals(s.Value))
|
||||
{
|
||||
_logger.Error("Author path: '{0}' is the same as another author, not deleting files.", author.Path);
|
||||
return;
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ protected override bool IsValid(PropertyValidatorContext context)
|
|||
return true;
|
||||
}
|
||||
|
||||
return !_authorService.GetAllAuthors().Any(s => context.PropertyValue.ToString().IsParentPath(s.Path));
|
||||
return !_authorService.AllAuthorPaths().Any(s => context.PropertyValue.ToString().IsParentPath(s.Value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using FluentValidation.Validators;
|
||||
using System.Linq;
|
||||
using FluentValidation.Validators;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Core.Books;
|
||||
|
||||
|
|
@ -24,7 +25,7 @@ protected override bool IsValid(PropertyValidatorContext context)
|
|||
dynamic instance = context.ParentContext.InstanceToValidate;
|
||||
var instanceId = (int)instance.Id;
|
||||
|
||||
return !_authorService.GetAllAuthors().Exists(s => s.Path.PathEquals(context.PropertyValue.ToString()) && s.Id != instanceId);
|
||||
return !_authorService.AllAuthorPaths().Any(s => s.Value.PathEquals(context.PropertyValue.ToString()) && s.Key != instanceId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue