From 68c20713e50635c9f292a68c09ef85168d05aa53 Mon Sep 17 00:00:00 2001 From: Bogdan Date: Fri, 2 Aug 2024 10:20:05 +0300 Subject: [PATCH] Validation for bulk movies editor --- .../Movies/MovieEditorController.cs | 11 +++++++++ .../Movies/MovieEditorValidator.cs | 23 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 src/Radarr.Api.V3/Movies/MovieEditorValidator.cs diff --git a/src/Radarr.Api.V3/Movies/MovieEditorController.cs b/src/Radarr.Api.V3/Movies/MovieEditorController.cs index e9ef0b28d..d10e5b2d9 100644 --- a/src/Radarr.Api.V3/Movies/MovieEditorController.cs +++ b/src/Radarr.Api.V3/Movies/MovieEditorController.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Linq; +using FluentValidation; using Microsoft.AspNetCore.Mvc; using NzbDrone.Common.Extensions; using NzbDrone.Core.Configuration; @@ -22,6 +23,7 @@ namespace Radarr.Api.V3.Movies private readonly IMapCoversToLocal _coverMapper; private readonly IConfigService _configService; private readonly IManageCommandQueue _commandQueueManager; + private readonly MovieEditorValidator _movieEditorValidator; private readonly IUpgradableSpecification _upgradableSpecification; public MovieEditorController(IMovieService movieService, @@ -29,6 +31,7 @@ namespace Radarr.Api.V3.Movies IMapCoversToLocal coverMapper, IConfigService configService, IManageCommandQueue commandQueueManager, + MovieEditorValidator movieEditorValidator, IUpgradableSpecification upgradableSpecification) { _movieService = movieService; @@ -36,6 +39,7 @@ namespace Radarr.Api.V3.Movies _coverMapper = coverMapper; _configService = configService; _commandQueueManager = commandQueueManager; + _movieEditorValidator = movieEditorValidator; _upgradableSpecification = upgradableSpecification; } @@ -90,6 +94,13 @@ namespace Radarr.Api.V3.Movies break; } } + + var validationResult = _movieEditorValidator.Validate(movie); + + if (!validationResult.IsValid) + { + throw new ValidationException(validationResult.Errors); + } } if (resource.MoveFiles && moviesToMove.Any()) diff --git a/src/Radarr.Api.V3/Movies/MovieEditorValidator.cs b/src/Radarr.Api.V3/Movies/MovieEditorValidator.cs new file mode 100644 index 000000000..eda10f032 --- /dev/null +++ b/src/Radarr.Api.V3/Movies/MovieEditorValidator.cs @@ -0,0 +1,23 @@ +using FluentValidation; +using NzbDrone.Common.Extensions; +using NzbDrone.Core.Movies; +using NzbDrone.Core.Validation; +using NzbDrone.Core.Validation.Paths; + +namespace Radarr.Api.V3.Movies +{ + public class MovieEditorValidator : AbstractValidator + { + public MovieEditorValidator(RootFolderExistsValidator rootFolderExistsValidator, QualityProfileExistsValidator qualityProfileExistsValidator) + { + RuleFor(s => s.RootFolderPath).Cascade(CascadeMode.Stop) + .IsValidPath() + .SetValidator(rootFolderExistsValidator) + .When(s => s.RootFolderPath.IsNotNullOrWhiteSpace()); + + RuleFor(c => c.QualityProfileId).Cascade(CascadeMode.Stop) + .ValidId() + .SetValidator(qualityProfileExistsValidator); + } + } +}