1
0
Fork 0
mirror of https://github.com/Sonarr/Sonarr synced 2025-01-03 13:45:02 +00:00

Validation for bulk series editor

This commit is contained in:
Bogdan 2024-08-11 18:45:15 +03:00 committed by GitHub
parent e6f82270a9
commit 8b253c36ea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 41 additions and 9 deletions

View file

@ -1,5 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using FluentValidation;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
using NzbDrone.Core.Messaging.Commands; using NzbDrone.Core.Messaging.Commands;
@ -14,11 +15,13 @@ namespace Sonarr.Api.V3.Series
{ {
private readonly ISeriesService _seriesService; private readonly ISeriesService _seriesService;
private readonly IManageCommandQueue _commandQueueManager; private readonly IManageCommandQueue _commandQueueManager;
private readonly SeriesEditorValidator _seriesEditorValidator;
public SeriesEditorController(ISeriesService seriesService, IManageCommandQueue commandQueueManager) public SeriesEditorController(ISeriesService seriesService, IManageCommandQueue commandQueueManager, SeriesEditorValidator seriesEditorValidator)
{ {
_seriesService = seriesService; _seriesService = seriesService;
_commandQueueManager = commandQueueManager; _commandQueueManager = commandQueueManager;
_seriesEditorValidator = seriesEditorValidator;
} }
[HttpPut] [HttpPut]
@ -58,10 +61,10 @@ namespace Sonarr.Api.V3.Series
{ {
series.RootFolderPath = resource.RootFolderPath; series.RootFolderPath = resource.RootFolderPath;
seriesToMove.Add(new BulkMoveSeries seriesToMove.Add(new BulkMoveSeries
{ {
SeriesId = series.Id, SeriesId = series.Id,
SourcePath = series.Path SourcePath = series.Path
}); });
} }
if (resource.Tags != null) if (resource.Tags != null)
@ -82,15 +85,22 @@ namespace Sonarr.Api.V3.Series
break; break;
} }
} }
var validationResult = _seriesEditorValidator.Validate(series);
if (!validationResult.IsValid)
{
throw new ValidationException(validationResult.Errors);
}
} }
if (resource.MoveFiles && seriesToMove.Any()) if (resource.MoveFiles && seriesToMove.Any())
{ {
_commandQueueManager.Push(new BulkMoveSeriesCommand _commandQueueManager.Push(new BulkMoveSeriesCommand
{ {
DestinationRootFolder = resource.RootFolderPath, DestinationRootFolder = resource.RootFolderPath,
Series = seriesToMove Series = seriesToMove
}); });
} }
return Accepted(_seriesService.UpdateSeries(seriesToUpdate, !resource.MoveFiles).ToResource()); return Accepted(_seriesService.UpdateSeries(seriesToUpdate, !resource.MoveFiles).ToResource());

View file

@ -0,0 +1,22 @@
using FluentValidation;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Validation;
using NzbDrone.Core.Validation.Paths;
namespace Sonarr.Api.V3.Series
{
public class SeriesEditorValidator : AbstractValidator<NzbDrone.Core.Tv.Series>
{
public SeriesEditorValidator(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);
}
}
}