New: Add App Profile validation for indexers

Fixes #1903
pull/1906/head
Bogdan 1 year ago
parent e0f6726a3d
commit 3963807c96

@ -7,7 +7,7 @@ using NzbDrone.Core.Messaging.Events;
namespace NzbDrone.Core.Profiles namespace NzbDrone.Core.Profiles
{ {
public interface IProfileService public interface IAppProfileService
{ {
AppSyncProfile Add(AppSyncProfile profile); AppSyncProfile Add(AppSyncProfile profile);
void Update(AppSyncProfile profile); void Update(AppSyncProfile profile);
@ -18,7 +18,7 @@ namespace NzbDrone.Core.Profiles
AppSyncProfile GetDefaultProfile(string name); AppSyncProfile GetDefaultProfile(string name);
} }
public class AppSyncProfileService : IProfileService, public class AppSyncProfileService : IAppProfileService,
IHandle<ApplicationStartedEvent> IHandle<ApplicationStartedEvent>
{ {
private readonly IAppProfileRepository _profileRepository; private readonly IAppProfileRepository _profileRepository;

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

@ -1,4 +1,5 @@
using NzbDrone.Core.Indexers; using NzbDrone.Core.Indexers;
using NzbDrone.Core.Validation;
using Prowlarr.Http; using Prowlarr.Http;
namespace Prowlarr.Api.V1.Indexers namespace Prowlarr.Api.V1.Indexers
@ -6,9 +7,12 @@ namespace Prowlarr.Api.V1.Indexers
[V1ApiController] [V1ApiController]
public class IndexerController : ProviderControllerBase<IndexerResource, IndexerBulkResource, IIndexer, IndexerDefinition> public class IndexerController : ProviderControllerBase<IndexerResource, IndexerBulkResource, IIndexer, IndexerDefinition>
{ {
public IndexerController(IndexerFactory indexerFactory, IndexerResourceMapper resourceMapper, IndexerBulkResourceMapper bulkResourceMapper) public IndexerController(IndexerFactory indexerFactory, IndexerResourceMapper resourceMapper, IndexerBulkResourceMapper bulkResourceMapper, AppProfileExistsValidator appProfileExistsValidator)
: base(indexerFactory, "indexer", resourceMapper, bulkResourceMapper) : base(indexerFactory, "indexer", resourceMapper, bulkResourceMapper)
{ {
Http.Validation.RuleBuilderExtensions.ValidId(SharedValidator.RuleFor(s => s.AppProfileId));
SharedValidator.RuleFor(c => c.AppProfileId).SetValidator(appProfileExistsValidator);
} }
} }
} }

@ -11,11 +11,11 @@ namespace Prowlarr.Api.V1.Profiles.App
[V1ApiController] [V1ApiController]
public class AppProfileController : RestController<AppProfileResource> public class AppProfileController : RestController<AppProfileResource>
{ {
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(); SharedValidator.RuleFor(c => c.Name).NotEmpty();
} }
@ -25,7 +25,7 @@ namespace Prowlarr.Api.V1.Profiles.App
public ActionResult<AppProfileResource> Create(AppProfileResource resource) public ActionResult<AppProfileResource> Create(AppProfileResource resource)
{ {
var model = resource.ToModel(); var model = resource.ToModel();
model = _profileService.Add(model); model = _appProfileService.Add(model);
return Created(model.Id); return Created(model.Id);
} }
@ -33,7 +33,7 @@ namespace Prowlarr.Api.V1.Profiles.App
[Produces("application/json")] [Produces("application/json")]
public object DeleteProfile(int id) public object DeleteProfile(int id)
{ {
_profileService.Delete(id); _appProfileService.Delete(id);
return new { }; return new { };
} }
@ -44,7 +44,7 @@ namespace Prowlarr.Api.V1.Profiles.App
{ {
var model = resource.ToModel(); var model = resource.ToModel();
_profileService.Update(model); _appProfileService.Update(model);
return Accepted(model.Id); return Accepted(model.Id);
} }
@ -55,14 +55,14 @@ namespace Prowlarr.Api.V1.Profiles.App
[ProducesResponseType(500)] [ProducesResponseType(500)]
public override AppProfileResource GetResourceById(int id) public override AppProfileResource GetResourceById(int id)
{ {
return _profileService.Get(id).ToResource(); return _appProfileService.Get(id).ToResource();
} }
[HttpGet] [HttpGet]
[Produces("application/json")] [Produces("application/json")]
public List<AppProfileResource> GetAll() public List<AppProfileResource> GetAll()
{ {
return _profileService.All().ToResource(); return _appProfileService.All().ToResource();
} }
} }
} }

@ -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();
}
}
}
Loading…
Cancel
Save