You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.5 KiB
53 lines
1.5 KiB
7 years ago
|
using System.Linq;
|
||
7 years ago
|
using System.Reflection;
|
||
|
using NzbDrone.Core.Configuration;
|
||
|
using Lidarr.Http;
|
||
|
using Lidarr.Http.REST;
|
||
|
|
||
7 years ago
|
namespace Lidarr.Api.V1.Config
|
||
7 years ago
|
{
|
||
7 years ago
|
public abstract class LidarrConfigModule<TResource> : LidarrRestModule<TResource> where TResource : RestResource, new()
|
||
7 years ago
|
{
|
||
|
private readonly IConfigService _configService;
|
||
|
|
||
7 years ago
|
protected LidarrConfigModule(IConfigService configService)
|
||
7 years ago
|
: this(new TResource().ResourceName.Replace("config", ""), configService)
|
||
|
{
|
||
|
}
|
||
|
|
||
7 years ago
|
protected LidarrConfigModule(string resource, IConfigService configService) :
|
||
7 years ago
|
base("config/" + resource.Trim('/'))
|
||
|
{
|
||
|
_configService = configService;
|
||
|
|
||
|
GetResourceSingle = GetConfig;
|
||
|
GetResourceById = GetConfig;
|
||
|
UpdateResource = SaveConfig;
|
||
|
}
|
||
|
|
||
|
private TResource GetConfig()
|
||
|
{
|
||
|
var resource = ToResource(_configService);
|
||
|
resource.Id = 1;
|
||
|
|
||
|
return resource;
|
||
|
}
|
||
|
|
||
|
protected abstract TResource ToResource(IConfigService model);
|
||
|
|
||
|
private TResource GetConfig(int id)
|
||
|
{
|
||
|
return GetConfig();
|
||
|
}
|
||
|
|
||
|
private void SaveConfig(TResource resource)
|
||
|
{
|
||
|
var dictionary = resource.GetType()
|
||
|
.GetProperties(BindingFlags.Instance | BindingFlags.Public)
|
||
|
.ToDictionary(prop => prop.Name, prop => prop.GetValue(resource, null));
|
||
|
|
||
|
_configService.SaveConfigDictionary(dictionary);
|
||
|
}
|
||
|
}
|
||
|
}
|