2013-09-25 02:42:55 +03:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using FluentValidation;
|
|
|
|
using Nancy;
|
|
|
|
using NzbDrone.Api.ClientSchema;
|
|
|
|
using NzbDrone.Api.Extensions;
|
|
|
|
using NzbDrone.Api.Mapping;
|
|
|
|
using NzbDrone.Common.Reflection;
|
|
|
|
using NzbDrone.Core.ThingiProvider;
|
|
|
|
using Omu.ValueInjecter;
|
|
|
|
|
|
|
|
namespace NzbDrone.Api
|
|
|
|
{
|
|
|
|
public abstract class ProviderModuleBase<TProviderResource, TProvider, TProviderDefinition> : NzbDroneRestModule<TProviderResource>
|
|
|
|
where TProviderDefinition : ProviderDefinition, new()
|
|
|
|
where TProvider : IProvider
|
|
|
|
where TProviderResource : ProviderResource, new()
|
|
|
|
{
|
|
|
|
private readonly IProviderFactory<TProvider, TProviderDefinition> _providerFactory;
|
|
|
|
|
2013-10-01 20:27:22 +03:00
|
|
|
protected ProviderModuleBase(IProviderFactory<TProvider, TProviderDefinition> providerFactory, string resource)
|
|
|
|
: base(resource)
|
2013-09-25 02:42:55 +03:00
|
|
|
{
|
|
|
|
_providerFactory = providerFactory;
|
|
|
|
Get["templates"] = x => GetTemplates();
|
|
|
|
GetResourceAll = GetAll;
|
|
|
|
GetResourceById = GetProviderById;
|
|
|
|
CreateResource = CreateProvider;
|
|
|
|
UpdateResource = UpdateProvider;
|
|
|
|
DeleteResource = DeleteProvider;
|
|
|
|
|
|
|
|
SharedValidator.RuleFor(c => c.Name).NotEmpty();
|
|
|
|
SharedValidator.RuleFor(c => c.Implementation).NotEmpty();
|
|
|
|
SharedValidator.RuleFor(c => c.ConfigContract).NotEmpty();
|
|
|
|
|
|
|
|
PostValidator.RuleFor(c => c.Fields).NotEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
private TProviderResource GetProviderById(int id)
|
|
|
|
{
|
|
|
|
return _providerFactory.Get(id).InjectTo<TProviderResource>();
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<TProviderResource> GetAll()
|
|
|
|
{
|
2014-01-08 04:29:43 +03:00
|
|
|
var providerDefinitions = _providerFactory.All();
|
2013-09-25 02:42:55 +03:00
|
|
|
|
2014-01-08 04:29:43 +03:00
|
|
|
var result = new List<TProviderResource>(providerDefinitions.Count);
|
2013-09-25 02:42:55 +03:00
|
|
|
|
2014-01-08 04:29:43 +03:00
|
|
|
foreach (var definition in providerDefinitions)
|
2013-09-25 02:42:55 +03:00
|
|
|
{
|
2014-01-08 04:29:43 +03:00
|
|
|
var providerResource = new TProviderResource();
|
|
|
|
providerResource.InjectFrom(definition);
|
|
|
|
providerResource.Fields = SchemaBuilder.ToSchema(definition.Settings);
|
2013-09-25 02:42:55 +03:00
|
|
|
|
2014-01-08 04:29:43 +03:00
|
|
|
result.Add(providerResource);
|
2013-09-25 02:42:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-01-08 04:29:43 +03:00
|
|
|
private int CreateProvider(TProviderResource providerResource)
|
2013-09-25 02:42:55 +03:00
|
|
|
{
|
2014-01-08 04:29:43 +03:00
|
|
|
var provider = GetDefinition(providerResource);
|
|
|
|
provider = _providerFactory.Create(provider);
|
|
|
|
return provider.Id;
|
2013-09-25 02:42:55 +03:00
|
|
|
}
|
|
|
|
|
2013-10-12 21:44:40 +03:00
|
|
|
private void UpdateProvider(TProviderResource providerResource)
|
2013-09-25 02:42:55 +03:00
|
|
|
{
|
2013-10-12 21:44:40 +03:00
|
|
|
var providerDefinition = GetDefinition(providerResource);
|
2013-09-25 02:42:55 +03:00
|
|
|
|
2013-10-12 21:44:40 +03:00
|
|
|
Validate(providerDefinition);
|
2013-09-25 02:42:55 +03:00
|
|
|
|
2013-10-12 21:44:40 +03:00
|
|
|
_providerFactory.Update(providerDefinition);
|
2013-09-25 02:42:55 +03:00
|
|
|
}
|
|
|
|
|
2013-10-12 21:44:40 +03:00
|
|
|
private TProviderDefinition GetDefinition(TProviderResource providerResource)
|
2013-09-25 02:42:55 +03:00
|
|
|
{
|
|
|
|
var definition = new TProviderDefinition();
|
|
|
|
|
2013-10-12 21:44:40 +03:00
|
|
|
definition.InjectFrom(providerResource);
|
2013-09-25 02:42:55 +03:00
|
|
|
|
|
|
|
var configContract = ReflectionExtensions.CoreAssembly.FindTypeByName(definition.ConfigContract);
|
2013-10-12 21:44:40 +03:00
|
|
|
definition.Settings = (IProviderConfig)SchemaBuilder.ReadFormSchema(providerResource.Fields, configContract);
|
2013-09-25 02:42:55 +03:00
|
|
|
|
2013-10-12 21:44:40 +03:00
|
|
|
Validate(definition);
|
2013-09-25 02:42:55 +03:00
|
|
|
|
|
|
|
return definition;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void DeleteProvider(int id)
|
|
|
|
{
|
|
|
|
_providerFactory.Delete(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Response GetTemplates()
|
|
|
|
{
|
2013-10-12 21:44:40 +03:00
|
|
|
var templates = _providerFactory.Templates();
|
2013-09-25 02:42:55 +03:00
|
|
|
|
2013-10-12 21:44:40 +03:00
|
|
|
var result = new List<TProviderResource>(templates.Count());
|
2013-09-25 02:42:55 +03:00
|
|
|
|
2013-10-12 21:44:40 +03:00
|
|
|
foreach (var providerDefinition in templates)
|
2013-09-25 02:42:55 +03:00
|
|
|
{
|
2013-10-12 21:44:40 +03:00
|
|
|
var providerResource = new TProviderResource();
|
|
|
|
providerResource.InjectFrom(providerDefinition);
|
|
|
|
providerResource.Fields = SchemaBuilder.ToSchema(providerDefinition.Settings);
|
2013-09-25 02:42:55 +03:00
|
|
|
|
2013-10-12 21:44:40 +03:00
|
|
|
result.Add(providerResource);
|
2013-09-25 02:42:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return result.AsResponse();
|
|
|
|
}
|
2013-10-12 21:44:40 +03:00
|
|
|
|
|
|
|
protected virtual void Validate(TProviderDefinition definition)
|
|
|
|
{
|
|
|
|
var validationResult = definition.Settings.Validate();
|
|
|
|
|
|
|
|
if (!validationResult.IsValid)
|
|
|
|
{
|
|
|
|
throw new ValidationException(validationResult.Errors);
|
|
|
|
}
|
|
|
|
}
|
2013-09-25 02:42:55 +03:00
|
|
|
}
|
|
|
|
}
|