diff --git a/src/Radarr.Api.V3/ImportLists/ImportListExclusionController.cs b/src/Radarr.Api.V3/ImportLists/ImportListExclusionController.cs index 976b537e4..f1ca38b6e 100644 --- a/src/Radarr.Api.V3/ImportLists/ImportListExclusionController.cs +++ b/src/Radarr.Api.V3/ImportLists/ImportListExclusionController.cs @@ -16,11 +16,15 @@ namespace Radarr.Api.V3.ImportLists { private readonly IImportListExclusionService _importListExclusionService; - public ImportListExclusionController(IImportListExclusionService importListExclusionService) + public ImportListExclusionController(IImportListExclusionService importListExclusionService, + ImportListExclusionExistsValidator importListExclusionExistsValidator) { _importListExclusionService = importListExclusionService; - SharedValidator.RuleFor(c => c.TmdbId).GreaterThan(0); + SharedValidator.RuleFor(c => c.TmdbId).Cascade(CascadeMode.Stop) + .NotEmpty() + .SetValidator(importListExclusionExistsValidator); + SharedValidator.RuleFor(c => c.MovieTitle).NotEmpty(); SharedValidator.RuleFor(c => c.MovieYear).GreaterThan(0); } diff --git a/src/Radarr.Api.V3/ImportLists/ImportListExclusionExistsValidator.cs b/src/Radarr.Api.V3/ImportLists/ImportListExclusionExistsValidator.cs new file mode 100644 index 000000000..bb27c80c4 --- /dev/null +++ b/src/Radarr.Api.V3/ImportLists/ImportListExclusionExistsValidator.cs @@ -0,0 +1,32 @@ +using FluentValidation.Validators; +using NzbDrone.Core.ImportLists.ImportExclusions; + +namespace Radarr.Api.V3.ImportLists +{ + public class ImportListExclusionExistsValidator : PropertyValidator + { + private readonly IImportListExclusionService _importListExclusionService; + + public ImportListExclusionExistsValidator(IImportListExclusionService importListExclusionService) + { + _importListExclusionService = importListExclusionService; + } + + protected override string GetDefaultMessageTemplate() => "This exclusion has already been added."; + + protected override bool IsValid(PropertyValidatorContext context) + { + if (context.PropertyValue == null) + { + return true; + } + + if (context.InstanceToValidate is not ImportListExclusionResource listExclusionResource) + { + return true; + } + + return !_importListExclusionService.All().Exists(v => v.TmdbId == (int)context.PropertyValue && v.Id != listExclusionResource.Id); + } + } +}