From 8ff8c27e24113b178ab65386d847d66260e51b1e Mon Sep 17 00:00:00 2001 From: Bogdan Date: Thu, 9 May 2024 20:09:45 +0300 Subject: [PATCH] New: Root folder exists validation for import lists Closes #4820 --- .../ImportLists/ImportListController.cs | 17 +++++++++---- .../MetadataProfileExistsValidator.cs | 15 ++++------- .../Paths/RootFolderExistsValidator.cs | 25 +++++++++++++++++++ 3 files changed, 42 insertions(+), 15 deletions(-) create mode 100644 src/NzbDrone.Core/Validation/Paths/RootFolderExistsValidator.cs diff --git a/src/Lidarr.Api.V1/ImportLists/ImportListController.cs b/src/Lidarr.Api.V1/ImportLists/ImportListController.cs index f98c841f9..ff2ed98db 100644 --- a/src/Lidarr.Api.V1/ImportLists/ImportListController.cs +++ b/src/Lidarr.Api.V1/ImportLists/ImportListController.cs @@ -1,3 +1,4 @@ +using FluentValidation; using Lidarr.Http; using NzbDrone.Core.ImportLists; using NzbDrone.Core.Validation; @@ -12,16 +13,22 @@ namespace Lidarr.Api.V1.ImportLists public static readonly ImportListBulkResourceMapper BulkResourceMapper = new (); public ImportListController(IImportListFactory importListFactory, + RootFolderExistsValidator rootFolderExistsValidator, QualityProfileExistsValidator qualityProfileExistsValidator, MetadataProfileExistsValidator metadataProfileExistsValidator) : base(importListFactory, "importlist", ResourceMapper, BulkResourceMapper) { - Http.Validation.RuleBuilderExtensions.ValidId(SharedValidator.RuleFor(s => s.QualityProfileId)); - Http.Validation.RuleBuilderExtensions.ValidId(SharedValidator.RuleFor(s => s.MetadataProfileId)); + SharedValidator.RuleFor(c => c.RootFolderPath).Cascade(CascadeMode.Stop) + .IsValidPath() + .SetValidator(rootFolderExistsValidator); - SharedValidator.RuleFor(c => c.RootFolderPath).IsValidPath(); - SharedValidator.RuleFor(c => c.QualityProfileId).SetValidator(qualityProfileExistsValidator); - SharedValidator.RuleFor(c => c.MetadataProfileId).SetValidator(metadataProfileExistsValidator); + SharedValidator.RuleFor(c => c.QualityProfileId).Cascade(CascadeMode.Stop) + .ValidId() + .SetValidator(qualityProfileExistsValidator); + + SharedValidator.RuleFor(c => c.MetadataProfileId).Cascade(CascadeMode.Stop) + .ValidId() + .SetValidator(metadataProfileExistsValidator); } } } diff --git a/src/NzbDrone.Core/Validation/MetadataProfileExistsValidator.cs b/src/NzbDrone.Core/Validation/MetadataProfileExistsValidator.cs index 328d7f8c3..e0df1569e 100644 --- a/src/NzbDrone.Core/Validation/MetadataProfileExistsValidator.cs +++ b/src/NzbDrone.Core/Validation/MetadataProfileExistsValidator.cs @@ -5,28 +5,23 @@ namespace NzbDrone.Core.Validation { 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 bool IsValid(PropertyValidatorContext context) { - if (context.PropertyValue == null) + if (context?.PropertyValue == null || (int)context.PropertyValue == 0) { return true; } - if ((int)context.PropertyValue == 0) - { - return true; - } - - return _profileService.Exists((int)context.PropertyValue); + return _metadataProfileService.Exists((int)context.PropertyValue); } } } diff --git a/src/NzbDrone.Core/Validation/Paths/RootFolderExistsValidator.cs b/src/NzbDrone.Core/Validation/Paths/RootFolderExistsValidator.cs new file mode 100644 index 000000000..8b4c4e7a0 --- /dev/null +++ b/src/NzbDrone.Core/Validation/Paths/RootFolderExistsValidator.cs @@ -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())); + } + } +}