Fixed: Improve validation for individual album adding

pull/5126/head v2.6.1.4370
Bogdan 4 months ago
parent 34c9300cbf
commit 21d9ecccd6

@ -42,6 +42,14 @@ namespace Lidarr.Api.V1.Albums
IMapCoversToLocal coverMapper,
IUpgradableSpecification upgradableSpecification,
IBroadcastSignalRMessage signalRBroadcaster,
RootFolderValidator rootFolderValidator,
MappedNetworkDriveValidator mappedNetworkDriveValidator,
ArtistPathValidator artistPathValidator,
ArtistAncestorValidator artistAncestorValidator,
RecycleBinValidator recycleBinValidator,
SystemFolderValidator systemFolderValidator,
AlbumExistsValidator albumExistsValidator,
RootFolderExistsValidator rootFolderExistsValidator,
QualityProfileExistsValidator qualityProfileExistsValidator,
MetadataProfileExistsValidator metadataProfileExistsValidator)
@ -51,11 +59,34 @@ namespace Lidarr.Api.V1.Albums
_releaseService = releaseService;
_addAlbumService = addAlbumService;
PostValidator.RuleFor(s => s.ForeignAlbumId).NotEmpty();
PostValidator.RuleFor(s => s.Artist.QualityProfileId).SetValidator(qualityProfileExistsValidator);
PostValidator.RuleFor(s => s.Artist.MetadataProfileId).SetValidator(metadataProfileExistsValidator);
PostValidator.RuleFor(s => s.Artist.RootFolderPath).IsValidPath().When(s => s.Artist.Path.IsNullOrWhiteSpace());
PostValidator.RuleFor(s => s.Artist.ForeignArtistId).NotEmpty();
PostValidator.RuleFor(s => s.ForeignAlbumId).NotEmpty().SetValidator(albumExistsValidator);
PostValidator.RuleFor(s => s.Artist).NotNull();
PostValidator.RuleFor(s => s.Artist.ForeignArtistId).NotEmpty().When(s => s.Artist != null);
PostValidator.RuleFor(s => s.Artist.QualityProfileId).Cascade(CascadeMode.Stop)
.ValidId()
.SetValidator(qualityProfileExistsValidator)
.When(s => s.Artist != null);
PostValidator.RuleFor(s => s.Artist.MetadataProfileId).Cascade(CascadeMode.Stop)
.ValidId()
.SetValidator(metadataProfileExistsValidator)
.When(s => s.Artist != null);
PostValidator.RuleFor(s => s.Artist.Path).Cascade(CascadeMode.Stop)
.IsValidPath()
.SetValidator(rootFolderValidator)
.SetValidator(mappedNetworkDriveValidator)
.SetValidator(artistPathValidator)
.SetValidator(artistAncestorValidator)
.SetValidator(recycleBinValidator)
.SetValidator(systemFolderValidator)
.When(s => s.Artist != null && s.Artist.Path.IsNotNullOrWhiteSpace());
PostValidator.RuleFor(s => s.Artist.RootFolderPath)
.IsValidPath()
.SetValidator(rootFolderExistsValidator)
.When(s => s.Artist != null && s.Artist.Path.IsNullOrWhiteSpace());
}
[HttpGet]

@ -110,8 +110,8 @@ namespace Lidarr.Api.V1.Albums
AlbumType = resource.AlbumType,
Monitored = resource.Monitored,
AnyReleaseOk = resource.AnyReleaseOk,
AlbumReleases = resource.Releases.ToModel(),
AddOptions = resource.AddOptions,
AlbumReleases = resource.Releases?.ToModel() ?? new List<AlbumRelease>(),
AddOptions = resource.AddOptions ?? new AddAlbumOptions(),
Artist = artist,
ArtistMetadata = artist.Metadata.Value
};

@ -0,0 +1,29 @@
using FluentValidation.Validators;
using NzbDrone.Core.Music;
namespace NzbDrone.Core.Validation.Paths
{
public class AlbumExistsValidator : PropertyValidator
{
private readonly IAlbumService _albumService;
public AlbumExistsValidator(IAlbumService albumService)
{
_albumService = albumService;
}
protected override string GetDefaultMessageTemplate() => "This album has already been added.";
protected override bool IsValid(PropertyValidatorContext context)
{
if (context.PropertyValue == null)
{
return true;
}
var foreignAlbumId = context.PropertyValue.ToString();
return _albumService.FindById(foreignAlbumId) == null;
}
}
}
Loading…
Cancel
Save