From 9ccb6af61b4cd95ffcbde3b3cc2fe5fe4847c26c Mon Sep 17 00:00:00 2001 From: Bogdan Date: Wed, 25 Oct 2023 15:11:41 +0300 Subject: [PATCH] New: Add Download Client validation for indexers (cherry picked from commit e53b7f8c945e3597ca1719961e82540f1f01f0e9) Closes #4246 --- .../Indexers/IndexerController.cs | 4 ++- .../DownloadClientExistsValidator.cs | 27 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 src/NzbDrone.Core/Validation/DownloadClientExistsValidator.cs diff --git a/src/Lidarr.Api.V1/Indexers/IndexerController.cs b/src/Lidarr.Api.V1/Indexers/IndexerController.cs index 101faf018..2ebcd3f29 100644 --- a/src/Lidarr.Api.V1/Indexers/IndexerController.cs +++ b/src/Lidarr.Api.V1/Indexers/IndexerController.cs @@ -1,5 +1,6 @@ using Lidarr.Http; using NzbDrone.Core.Indexers; +using NzbDrone.Core.Validation; namespace Lidarr.Api.V1.Indexers { @@ -9,9 +10,10 @@ namespace Lidarr.Api.V1.Indexers public static readonly IndexerResourceMapper ResourceMapper = new (); public static readonly IndexerBulkResourceMapper BulkResourceMapper = new (); - public IndexerController(IndexerFactory indexerFactory) + public IndexerController(IndexerFactory indexerFactory, DownloadClientExistsValidator downloadClientExistsValidator) : base(indexerFactory, "indexer", ResourceMapper, BulkResourceMapper) { + SharedValidator.RuleFor(c => c.DownloadClientId).SetValidator(downloadClientExistsValidator); } } } diff --git a/src/NzbDrone.Core/Validation/DownloadClientExistsValidator.cs b/src/NzbDrone.Core/Validation/DownloadClientExistsValidator.cs new file mode 100644 index 000000000..cf021f464 --- /dev/null +++ b/src/NzbDrone.Core/Validation/DownloadClientExistsValidator.cs @@ -0,0 +1,27 @@ +using FluentValidation.Validators; +using NzbDrone.Core.Download; + +namespace NzbDrone.Core.Validation +{ + public class DownloadClientExistsValidator : PropertyValidator + { + private readonly IDownloadClientFactory _downloadClientFactory; + + public DownloadClientExistsValidator(IDownloadClientFactory downloadClientFactory) + { + _downloadClientFactory = downloadClientFactory; + } + + protected override string GetDefaultMessageTemplate() => "Download Client does not exist"; + + protected override bool IsValid(PropertyValidatorContext context) + { + if (context?.PropertyValue == null || (int)context.PropertyValue == 0) + { + return true; + } + + return _downloadClientFactory.Exists((int)context.PropertyValue); + } + } +}