New: Root folder exists validation for import lists

Closes #4820
pull/5051/head
Bogdan 8 months ago
parent 281bcb28fe
commit 8ff8c27e24

@ -1,3 +1,4 @@
using FluentValidation;
using Lidarr.Http; using Lidarr.Http;
using NzbDrone.Core.ImportLists; using NzbDrone.Core.ImportLists;
using NzbDrone.Core.Validation; using NzbDrone.Core.Validation;
@ -12,16 +13,22 @@ namespace Lidarr.Api.V1.ImportLists
public static readonly ImportListBulkResourceMapper BulkResourceMapper = new (); public static readonly ImportListBulkResourceMapper BulkResourceMapper = new ();
public ImportListController(IImportListFactory importListFactory, public ImportListController(IImportListFactory importListFactory,
RootFolderExistsValidator rootFolderExistsValidator,
QualityProfileExistsValidator qualityProfileExistsValidator, QualityProfileExistsValidator qualityProfileExistsValidator,
MetadataProfileExistsValidator metadataProfileExistsValidator) MetadataProfileExistsValidator metadataProfileExistsValidator)
: base(importListFactory, "importlist", ResourceMapper, BulkResourceMapper) : base(importListFactory, "importlist", ResourceMapper, BulkResourceMapper)
{ {
Http.Validation.RuleBuilderExtensions.ValidId(SharedValidator.RuleFor(s => s.QualityProfileId)); SharedValidator.RuleFor(c => c.RootFolderPath).Cascade(CascadeMode.Stop)
Http.Validation.RuleBuilderExtensions.ValidId(SharedValidator.RuleFor(s => s.MetadataProfileId)); .IsValidPath()
.SetValidator(rootFolderExistsValidator);
SharedValidator.RuleFor(c => c.RootFolderPath).IsValidPath(); SharedValidator.RuleFor(c => c.QualityProfileId).Cascade(CascadeMode.Stop)
SharedValidator.RuleFor(c => c.QualityProfileId).SetValidator(qualityProfileExistsValidator); .ValidId()
SharedValidator.RuleFor(c => c.MetadataProfileId).SetValidator(metadataProfileExistsValidator); .SetValidator(qualityProfileExistsValidator);
SharedValidator.RuleFor(c => c.MetadataProfileId).Cascade(CascadeMode.Stop)
.ValidId()
.SetValidator(metadataProfileExistsValidator);
} }
} }
} }

@ -5,28 +5,23 @@ namespace NzbDrone.Core.Validation
{ {
public class MetadataProfileExistsValidator : PropertyValidator public class MetadataProfileExistsValidator : PropertyValidator
{ {
private readonly IMetadataProfileService _profileService; private readonly IMetadataProfileService _metadataProfileService;
public MetadataProfileExistsValidator(IMetadataProfileService profileService) public MetadataProfileExistsValidator(IMetadataProfileService metadataProfileService)
{ {
_profileService = profileService; _metadataProfileService = metadataProfileService;
} }
protected override string GetDefaultMessageTemplate() => "Metadata profile does not exist"; protected override string GetDefaultMessageTemplate() => "Metadata profile does not exist";
protected override bool IsValid(PropertyValidatorContext context) protected override bool IsValid(PropertyValidatorContext context)
{ {
if (context.PropertyValue == null) if (context?.PropertyValue == null || (int)context.PropertyValue == 0)
{ {
return true; return true;
} }
if ((int)context.PropertyValue == 0) return _metadataProfileService.Exists((int)context.PropertyValue);
{
return true;
}
return _profileService.Exists((int)context.PropertyValue);
} }
} }
} }

@ -0,0 +1,25 @@
using FluentValidation.Validators;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.RootFolders;
namespace NzbDrone.Core.Validation.Paths
{
public class RootFolderExistsValidator : PropertyValidator
{
private readonly IRootFolderService _rootFolderService;
public RootFolderExistsValidator(IRootFolderService rootFolderService)
{
_rootFolderService = rootFolderService;
}
protected override string GetDefaultMessageTemplate() => "Root folder '{path}' does not exist";
protected override bool IsValid(PropertyValidatorContext context)
{
context.MessageFormatter.AppendArgument("path", context.PropertyValue?.ToString());
return context.PropertyValue == null || _rootFolderService.All().Exists(r => r.Path.PathEquals(context.PropertyValue.ToString()));
}
}
}
Loading…
Cancel
Save