From 3963807c96c13386fdd84aafc06474d4f04bd06c Mon Sep 17 00:00:00 2001 From: Bogdan Date: Wed, 25 Oct 2023 13:04:02 +0300 Subject: [PATCH] New: Add App Profile validation for indexers Fixes #1903 --- .../Profiles/AppSyncProfileService.cs | 4 +-- .../Validation/AppProfileExistsValidator.cs | 22 ++++++++++++++++ .../Indexers/IndexerController.cs | 6 ++++- .../Profiles/App/AppProfileController.cs | 16 ++++++------ .../Profiles/App/AppProfileSchemaModule.cs | 26 ------------------- 5 files changed, 37 insertions(+), 37 deletions(-) create mode 100644 src/NzbDrone.Core/Validation/AppProfileExistsValidator.cs delete mode 100644 src/Prowlarr.Api.V1/Profiles/App/AppProfileSchemaModule.cs diff --git a/src/NzbDrone.Core/Profiles/AppSyncProfileService.cs b/src/NzbDrone.Core/Profiles/AppSyncProfileService.cs index 73c07d0a8..135cd9e2a 100644 --- a/src/NzbDrone.Core/Profiles/AppSyncProfileService.cs +++ b/src/NzbDrone.Core/Profiles/AppSyncProfileService.cs @@ -7,7 +7,7 @@ using NzbDrone.Core.Messaging.Events; namespace NzbDrone.Core.Profiles { - public interface IProfileService + public interface IAppProfileService { AppSyncProfile Add(AppSyncProfile profile); void Update(AppSyncProfile profile); @@ -18,7 +18,7 @@ namespace NzbDrone.Core.Profiles AppSyncProfile GetDefaultProfile(string name); } - public class AppSyncProfileService : IProfileService, + public class AppSyncProfileService : IAppProfileService, IHandle { private readonly IAppProfileRepository _profileRepository; diff --git a/src/NzbDrone.Core/Validation/AppProfileExistsValidator.cs b/src/NzbDrone.Core/Validation/AppProfileExistsValidator.cs new file mode 100644 index 000000000..a3a1c4330 --- /dev/null +++ b/src/NzbDrone.Core/Validation/AppProfileExistsValidator.cs @@ -0,0 +1,22 @@ +using FluentValidation.Validators; +using NzbDrone.Core.Profiles; + +namespace NzbDrone.Core.Validation +{ + public class AppProfileExistsValidator : PropertyValidator + { + private readonly IAppProfileService _appProfileService; + + public AppProfileExistsValidator(IAppProfileService appProfileService) + { + _appProfileService = appProfileService; + } + + protected override string GetDefaultMessageTemplate() => "App Profile does not exist"; + + protected override bool IsValid(PropertyValidatorContext context) + { + return context?.PropertyValue == null || _appProfileService.Exists((int)context.PropertyValue); + } + } +} diff --git a/src/Prowlarr.Api.V1/Indexers/IndexerController.cs b/src/Prowlarr.Api.V1/Indexers/IndexerController.cs index 0e38f0ccb..bb2ffc782 100644 --- a/src/Prowlarr.Api.V1/Indexers/IndexerController.cs +++ b/src/Prowlarr.Api.V1/Indexers/IndexerController.cs @@ -1,4 +1,5 @@ using NzbDrone.Core.Indexers; +using NzbDrone.Core.Validation; using Prowlarr.Http; namespace Prowlarr.Api.V1.Indexers @@ -6,9 +7,12 @@ namespace Prowlarr.Api.V1.Indexers [V1ApiController] public class IndexerController : ProviderControllerBase { - public IndexerController(IndexerFactory indexerFactory, IndexerResourceMapper resourceMapper, IndexerBulkResourceMapper bulkResourceMapper) + public IndexerController(IndexerFactory indexerFactory, IndexerResourceMapper resourceMapper, IndexerBulkResourceMapper bulkResourceMapper, AppProfileExistsValidator appProfileExistsValidator) : base(indexerFactory, "indexer", resourceMapper, bulkResourceMapper) { + Http.Validation.RuleBuilderExtensions.ValidId(SharedValidator.RuleFor(s => s.AppProfileId)); + + SharedValidator.RuleFor(c => c.AppProfileId).SetValidator(appProfileExistsValidator); } } } diff --git a/src/Prowlarr.Api.V1/Profiles/App/AppProfileController.cs b/src/Prowlarr.Api.V1/Profiles/App/AppProfileController.cs index 0b4b7ae4f..8b3236ec4 100644 --- a/src/Prowlarr.Api.V1/Profiles/App/AppProfileController.cs +++ b/src/Prowlarr.Api.V1/Profiles/App/AppProfileController.cs @@ -11,11 +11,11 @@ namespace Prowlarr.Api.V1.Profiles.App [V1ApiController] public class AppProfileController : RestController { - private readonly IProfileService _profileService; + private readonly IAppProfileService _appProfileService; - public AppProfileController(IProfileService profileService) + public AppProfileController(IAppProfileService appProfileService) { - _profileService = profileService; + _appProfileService = appProfileService; SharedValidator.RuleFor(c => c.Name).NotEmpty(); } @@ -25,7 +25,7 @@ namespace Prowlarr.Api.V1.Profiles.App public ActionResult Create(AppProfileResource resource) { var model = resource.ToModel(); - model = _profileService.Add(model); + model = _appProfileService.Add(model); return Created(model.Id); } @@ -33,7 +33,7 @@ namespace Prowlarr.Api.V1.Profiles.App [Produces("application/json")] public object DeleteProfile(int id) { - _profileService.Delete(id); + _appProfileService.Delete(id); return new { }; } @@ -44,7 +44,7 @@ namespace Prowlarr.Api.V1.Profiles.App { var model = resource.ToModel(); - _profileService.Update(model); + _appProfileService.Update(model); return Accepted(model.Id); } @@ -55,14 +55,14 @@ namespace Prowlarr.Api.V1.Profiles.App [ProducesResponseType(500)] public override AppProfileResource GetResourceById(int id) { - return _profileService.Get(id).ToResource(); + return _appProfileService.Get(id).ToResource(); } [HttpGet] [Produces("application/json")] public List GetAll() { - return _profileService.All().ToResource(); + return _appProfileService.All().ToResource(); } } } diff --git a/src/Prowlarr.Api.V1/Profiles/App/AppProfileSchemaModule.cs b/src/Prowlarr.Api.V1/Profiles/App/AppProfileSchemaModule.cs deleted file mode 100644 index 7cdbda0ed..000000000 --- a/src/Prowlarr.Api.V1/Profiles/App/AppProfileSchemaModule.cs +++ /dev/null @@ -1,26 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using NzbDrone.Core.Profiles; -using Prowlarr.Http; - -namespace Prowlarr.Api.V1.Profiles.App -{ - [V1ApiController("appprofile/schema")] - public class QualityProfileSchemaController : Controller - { - private readonly IProfileService _profileService; - - public QualityProfileSchemaController(IProfileService profileService) - { - _profileService = profileService; - } - - [HttpGet] - [Produces("application/json")] - public AppProfileResource GetSchema() - { - var qualityProfile = _profileService.GetDefaultProfile(string.Empty); - - return qualityProfile.ToResource(); - } - } -}