sonarr-repo-only/NzbDrone.Api/Config/SettingsModule.cs

102 lines
3.3 KiB
C#
Raw Normal View History

2013-04-25 04:27:49 +00:00
using System;
using System.Collections.Generic;
using Nancy;
using NzbDrone.Api.Extensions;
using NzbDrone.Api.REST;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Organizer;
using FluentValidation;
namespace NzbDrone.Api.Config
{
public class NamingModule : NzbDroneRestModule<NamingConfigResource>
{
private readonly INamingConfigService _namingConfigService;
public NamingModule(INamingConfigService namingConfigService)
: base("config/naming")
{
_namingConfigService = namingConfigService;
GetResourceSingle = GetNamingConfig;
UpdateResource = UpdateNamingConfig;
SharedValidator.RuleFor(c => c.MultiEpisodeStyle).InclusiveBetween(0, 3);
SharedValidator.RuleFor(c => c.NumberStyle).InclusiveBetween(0, 3);
2013-05-30 15:39:47 +00:00
SharedValidator.RuleFor(c => c.Separator).Matches(@"\s|\s\-\s|\.");
2013-04-25 04:27:49 +00:00
}
private NamingConfigResource UpdateNamingConfig(NamingConfigResource resource)
{
2013-05-31 00:12:20 +00:00
return ToResource<NamingConfig>(_namingConfigService.Save, resource);
2013-04-25 04:27:49 +00:00
}
private NamingConfigResource GetNamingConfig()
{
2013-05-31 00:12:20 +00:00
return ToResource(_namingConfigService.GetConfig);
2013-04-25 04:27:49 +00:00
}
}
public class NamingConfigResource : RestResource
{
public Boolean IncludeEpisodeTitle { get; set; }
public Boolean ReplaceSpaces { get; set; }
2013-07-05 03:26:07 +00:00
public Boolean RenameEpisodes { get; set; }
2013-04-25 04:27:49 +00:00
public Int32 MultiEpisodeStyle { get; set; }
public Int32 NumberStyle { get; set; }
public String Separator { get; set; }
public Boolean IncludeQuality { get; set; }
public Boolean IncludeSeriesTitle { get; set; }
}
public class SettingsModule : NzbDroneApiModule
{
private readonly IConfigService _configService;
private readonly IConfigFileProvider _configFileProvider;
2013-04-25 04:27:49 +00:00
public SettingsModule(IConfigService configService, IConfigFileProvider configFileProvider)
2013-04-25 04:27:49 +00:00
: base("/settings")
{
_configService = configService;
_configFileProvider = configFileProvider;
Get["/"] = x => GetGeneralSettings();
Post["/"] = x => SaveGeneralSettings();
Get["/host"] = x => GetHostSettings();
Post["/host"] = x => SaveHostSettings();
}
private Response SaveHostSettings()
{
var request = Request.Body.FromJson<Dictionary<string, object>>();
_configFileProvider.SaveConfigDictionary(request);
return GetHostSettings();
}
private Response GetHostSettings()
{
return _configFileProvider.GetConfigDictionary().AsResponse();
2013-04-25 04:27:49 +00:00
}
private Response GetGeneralSettings()
2013-04-25 04:27:49 +00:00
{
var collection = Request.Query.Collection;
if (collection.HasValue && Boolean.Parse(collection.Value))
return _configService.All().AsResponse();
return _configService.AllWithDefaults().AsResponse();
}
private Response SaveGeneralSettings()
2013-04-25 04:27:49 +00:00
{
var request = Request.Body.FromJson<Dictionary<string, object>>();
_configService.SaveValues(request);
return request.AsResponse();
}
}
}