1
0
Fork 0
mirror of https://github.com/lidarr/Lidarr synced 2024-12-21 23:32:27 +00:00

New: Root folder exists validation for import lists

Closes #4820
This commit is contained in:
Bogdan 2024-05-09 20:09:45 +03:00
parent 281bcb28fe
commit 8ff8c27e24
3 changed files with 42 additions and 15 deletions

View file

@ -1,3 +1,4 @@
using FluentValidation;
using Lidarr.Http;
using NzbDrone.Core.ImportLists;
using NzbDrone.Core.Validation;
@ -12,16 +13,22 @@ public class ImportListController : ProviderControllerBase<ImportListResource, I
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);
}
}
}

View file

@ -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);
}
}
}

View file

@ -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()));
}
}
}