Add v5 UI settings endpoints

This commit is contained in:
Mark McDowall 2025-12-28 16:00:01 -08:00
parent 7e70238005
commit e9011011ed
3 changed files with 143 additions and 0 deletions

View file

@ -0,0 +1,49 @@
using System.Reflection;
using Microsoft.AspNetCore.Mvc;
using NzbDrone.Core.Configuration;
using Sonarr.Http.REST;
using Sonarr.Http.REST.Attributes;
namespace Sonarr.Api.V5.Config
{
public abstract class ConfigController<TResource> : RestController<TResource>
where TResource : RestResource, new()
{
protected readonly IConfigService _configService;
protected ConfigController(IConfigService configService)
{
_configService = configService;
}
protected override TResource GetResourceById(int id)
{
return GetConfig();
}
[HttpGet]
[Produces("application/json")]
public TResource GetConfig()
{
var resource = ToResource(_configService);
resource.Id = 1;
return resource;
}
[RestPutById]
[Consumes("application/json")]
public virtual ActionResult<TResource> SaveConfig([FromBody] TResource resource)
{
var dictionary = resource.GetType()
.GetProperties(BindingFlags.Instance | BindingFlags.Public)
.ToDictionary(prop => prop.Name, prop => prop.GetValue(resource, null));
_configService.SaveConfigDictionary(dictionary);
return Accepted(resource.Id);
}
protected abstract TResource ToResource(IConfigService model);
}
}

View file

@ -0,0 +1,50 @@
using System.Reflection;
using FluentValidation;
using Microsoft.AspNetCore.Mvc;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Languages;
using Sonarr.Http;
using Sonarr.Http.REST.Attributes;
namespace Sonarr.Api.V5.Config;
[V5ApiController("settings/ui")]
public class UiSettingsController : ConfigController<UiSettingsResource>
{
private readonly IConfigFileProvider _configFileProvider;
public UiSettingsController(IConfigFileProvider configFileProvider, IConfigService configService)
: base(configService)
{
_configFileProvider = configFileProvider;
SharedValidator.RuleFor(c => c.UiLanguage).Custom((value, context) =>
{
if (!Language.All.Any(o => o.Id == value))
{
context.AddFailure("Invalid UI Language value");
}
});
SharedValidator.RuleFor(c => c.UiLanguage)
.GreaterThanOrEqualTo(1)
.WithMessage("The UI Language value cannot be less than 1");
}
[RestPutById]
public override ActionResult<UiSettingsResource> SaveConfig([FromBody] UiSettingsResource resource)
{
var dictionary = resource.GetType()
.GetProperties(BindingFlags.Instance | BindingFlags.Public)
.ToDictionary(prop => prop.Name, prop => prop.GetValue(resource, null));
_configFileProvider.SaveConfigDictionary(dictionary);
_configService.SaveConfigDictionary(dictionary);
return Accepted(resource.Id);
}
protected override UiSettingsResource ToResource(IConfigService model)
{
return UiSettingsResourceMapper.ToResource(_configFileProvider, model);
}
}

View file

@ -0,0 +1,44 @@
using NzbDrone.Core.Configuration;
using Sonarr.Http.REST;
namespace Sonarr.Api.V5.Config;
public class UiSettingsResource : RestResource
{
// Calendar
public int FirstDayOfWeek { get; set; }
public string? CalendarWeekColumnHeader { get; set; }
// Dates
public string? ShortDateFormat { get; set; }
public string? LongDateFormat { get; set; }
public string? TimeFormat { get; set; }
public string? TimeZone { get; set; }
public bool ShowRelativeDates { get; set; }
public bool EnableColorImpairedMode { get; set; }
public string? Theme { get; set; }
public int UiLanguage { get; set; }
}
public static class UiSettingsResourceMapper
{
public static UiSettingsResource ToResource(IConfigFileProvider config, IConfigService model)
{
return new UiSettingsResource
{
FirstDayOfWeek = model.FirstDayOfWeek,
CalendarWeekColumnHeader = model.CalendarWeekColumnHeader,
ShortDateFormat = model.ShortDateFormat,
LongDateFormat = model.LongDateFormat,
TimeFormat = model.TimeFormat,
TimeZone = model.TimeZone,
ShowRelativeDates = model.ShowRelativeDates,
EnableColorImpairedMode = model.EnableColorImpairedMode,
Theme = config.Theme,
UiLanguage = model.UILanguage
};
}
}