1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-19 17:32:38 +01:00

Fixed: Validate uniqueness for import list exclusions

This commit is contained in:
Bogdan 2024-08-12 13:41:42 +03:00 committed by Qstick
parent b6d9c73a17
commit 3be2c6b0be
2 changed files with 38 additions and 2 deletions

View File

@ -16,11 +16,15 @@ public class ImportListExclusionController : RestController<ImportListExclusionR
{
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);
}

View File

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