Cleanup RootFolderService and Validation

This commit is contained in:
Qstick 2017-10-08 15:09:46 -04:00
parent 1d120be466
commit 7d045d5c54
9 changed files with 40 additions and 99 deletions

View File

@ -47,7 +47,7 @@ namespace Lidarr.Api.V3.Artist
RootFolderValidator rootFolderValidator,
ArtistPathValidator artistPathValidator,
ArtistExistsValidator artistExistsValidator,
SeriesAncestorValidator seriesAncestorValidator,
ArtistAncestorValidator artistAncestorValidator,
ProfileExistsValidator profileExistsValidator,
LanguageProfileExistsValidator languageProfileExistsValidator
)
@ -73,7 +73,7 @@ namespace Lidarr.Api.V3.Artist
.IsValidPath()
.SetValidator(rootFolderValidator)
.SetValidator(artistPathValidator)
.SetValidator(seriesAncestorValidator)
.SetValidator(artistAncestorValidator)
.When(s => !s.Path.IsNullOrWhiteSpace());
SharedValidator.RuleFor(s => s.QualityProfileId).SetValidator(profileExistsValidator);

View File

@ -16,15 +16,15 @@ namespace NzbDrone.Core.Music
public class AddArtistValidator : AbstractValidator<Artist>, IAddArtistValidator
{
public AddArtistValidator(RootFolderValidator rootFolderValidator,
SeriesPathValidator seriesPathValidator,
SeriesAncestorValidator seriesAncestorValidator,
ArtistPathValidator artistPathValidator,
ArtistAncestorValidator artistAncestorValidator,
ArtistSlugValidator artistTitleSlugValidator)
{
RuleFor(c => c.Path).Cascade(CascadeMode.StopOnFirstFailure)
.IsValidPath()
.SetValidator(rootFolderValidator)
.SetValidator(seriesPathValidator)
.SetValidator(seriesAncestorValidator);
.SetValidator(artistPathValidator)
.SetValidator(artistAncestorValidator);
RuleFor(c => c.NameSlug).SetValidator(artistTitleSlugValidator);// TODO: Check if we are going to use a slug or artistName
}

View File

@ -1216,9 +1216,7 @@
<Compile Include="Validation\Paths\PathValidator.cs" />
<Compile Include="Validation\Paths\StartupFolderValidator.cs" />
<Compile Include="Validation\Paths\RootFolderValidator.cs" />
<Compile Include="Validation\Paths\SeriesAncestorValidator.cs" />
<Compile Include="Validation\Paths\SeriesExistsValidator.cs" />
<Compile Include="Validation\Paths\SeriesPathValidator.cs" />
<Compile Include="Validation\Paths\ArtistAncestorValidator.cs" />
<Compile Include="Validation\ProfileExistsValidator.cs" />
<Compile Include="Validation\RuleBuilderExtensions.cs" />
<Compile Include="Validation\UrlValidator.cs" />

View File

@ -7,7 +7,7 @@ using NzbDrone.Common;
using NzbDrone.Common.Disk;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Tv;
using NzbDrone.Core.Music;
namespace NzbDrone.Core.RootFolders
{
@ -24,7 +24,7 @@ namespace NzbDrone.Core.RootFolders
{
private readonly IRootFolderRepository _rootFolderRepository;
private readonly IDiskProvider _diskProvider;
private readonly ISeriesRepository _seriesRepository;
private readonly IArtistRepository _artistRepository;
private readonly IConfigService _configService;
private readonly Logger _logger;
@ -44,13 +44,13 @@ namespace NzbDrone.Core.RootFolders
public RootFolderService(IRootFolderRepository rootFolderRepository,
IDiskProvider diskProvider,
ISeriesRepository seriesRepository,
IArtistRepository artistRepository,
IConfigService configService,
Logger logger)
{
_rootFolderRepository = rootFolderRepository;
_diskProvider = diskProvider;
_seriesRepository = seriesRepository;
_artistRepository = artistRepository;
_configService = configService;
_logger = logger;
}
@ -134,7 +134,7 @@ namespace NzbDrone.Core.RootFolders
}
var results = new List<UnmappedFolder>();
var series = _seriesRepository.All().ToList();
var artist = _artistRepository.All().ToList();
if (!_diskProvider.FolderExists(path))
{
@ -142,8 +142,8 @@ namespace NzbDrone.Core.RootFolders
return results;
}
var possibleSeriesFolders = _diskProvider.GetDirectories(path).ToList();
var unmappedFolders = possibleSeriesFolders.Except(series.Select(s => s.Path), PathEqualityComparer.Instance).ToList();
var possibleArtistFolders = _diskProvider.GetDirectories(path).ToList();
var unmappedFolders = possibleArtistFolders.Except(artist.Select(s => s.Path), PathEqualityComparer.Instance).ToList();
foreach (string unmappedFolder in unmappedFolders)
{

View File

@ -12,15 +12,11 @@ namespace NzbDrone.Core.Tv
public class AddSeriesValidator : AbstractValidator<Series>, IAddSeriesValidator
{
public AddSeriesValidator(RootFolderValidator rootFolderValidator,
SeriesPathValidator seriesPathValidator,
SeriesAncestorValidator seriesAncestorValidator,
SeriesTitleSlugValidator seriesTitleSlugValidator)
{
RuleFor(c => c.Path).Cascade(CascadeMode.StopOnFirstFailure)
.IsValidPath()
.SetValidator(rootFolderValidator)
.SetValidator(seriesPathValidator)
.SetValidator(seriesAncestorValidator);
.SetValidator(rootFolderValidator);
RuleFor(c => c.TitleSlug).SetValidator(seriesTitleSlugValidator);
}

View File

@ -0,0 +1,25 @@
using System.Linq;
using FluentValidation.Validators;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Music;
namespace NzbDrone.Core.Validation.Paths
{
public class ArtistAncestorValidator : PropertyValidator
{
private readonly IArtistService _artistService;
public ArtistAncestorValidator(IArtistService artistService)
: base("Path is an ancestor of an existing path")
{
_artistService = artistService;
}
protected override bool IsValid(PropertyValidatorContext context)
{
if (context.PropertyValue == null) return true;
return !_artistService.GetAllArtists().Any(s => context.PropertyValue.ToString().IsParentPath(s.Path));
}
}
}

View File

@ -1,25 +0,0 @@
using System.Linq;
using FluentValidation.Validators;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Tv;
namespace NzbDrone.Core.Validation.Paths
{
public class SeriesAncestorValidator : PropertyValidator
{
private readonly ISeriesService _seriesService;
public SeriesAncestorValidator(ISeriesService seriesService)
: base("Path is an ancestor of an existing path")
{
_seriesService = seriesService;
}
protected override bool IsValid(PropertyValidatorContext context)
{
if (context.PropertyValue == null) return true;
return !_seriesService.GetAllSeries().Any(s => context.PropertyValue.ToString().IsParentPath(s.Path));
}
}
}

View File

@ -1,26 +0,0 @@
using System;
using FluentValidation.Validators;
using NzbDrone.Core.Tv;
namespace NzbDrone.Core.Validation.Paths
{
public class SeriesExistsValidator : PropertyValidator
{
private readonly ISeriesService _seriesService;
public SeriesExistsValidator(ISeriesService seriesService)
: base("This series has already been added")
{
_seriesService = seriesService;
}
protected override bool IsValid(PropertyValidatorContext context)
{
if (context.PropertyValue == null) return true;
var tvdbId = Convert.ToInt32(context.PropertyValue.ToString());
return (!_seriesService.GetAllSeries().Exists(s => s.TvdbId == tvdbId));
}
}
}

View File

@ -1,27 +0,0 @@
using FluentValidation.Validators;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Tv;
namespace NzbDrone.Core.Validation.Paths
{
public class SeriesPathValidator : PropertyValidator
{
private readonly ISeriesService _seriesService;
public SeriesPathValidator(ISeriesService seriesService)
: base("Path is already configured for another series")
{
_seriesService = seriesService;
}
protected override bool IsValid(PropertyValidatorContext context)
{
if (context.PropertyValue == null) return true;
dynamic instance = context.ParentContext.InstanceToValidate;
var instanceId = (int)instance.Id;
return (!_seriesService.GetAllSeries().Exists(s => s.Path.PathEquals(context.PropertyValue.ToString()) && s.Id != instanceId));
}
}
}