diff --git a/Kyoo.Common/Controllers/IConfigurationManager.cs b/Kyoo.Common/Controllers/IConfigurationManager.cs index ccd2bd05..089f7312 100644 --- a/Kyoo.Common/Controllers/IConfigurationManager.cs +++ b/Kyoo.Common/Controllers/IConfigurationManager.cs @@ -1,4 +1,3 @@ -using System; using System.Threading.Tasks; using Kyoo.Models; using Kyoo.Models.Exceptions; @@ -15,19 +14,9 @@ namespace Kyoo.Controllers /// /// Edit the value of a setting using it's path. Save it to the json file. /// - /// The path of the resource (can be separated by ':' or '__' + /// The path of the resource (can be separated by ':' or '__') /// The new value of the resource - /// The type of the resource /// No setting found at the given path. - Task EditValue(string path, T value); - - /// - /// Edit the value of a setting using it's path. Save it to the json file. - /// - /// The path of the resource (can be separated by ':' or '__' - /// The new value of the resource - /// The type of the resource - /// No setting found at the given path. - Task EditValue(string path, object value, Type type); + Task EditValue(string path, object value); } } \ No newline at end of file diff --git a/Kyoo/Controllers/ConfigurationManager.cs b/Kyoo/Controllers/ConfigurationManager.cs index 3d9ddda8..38710017 100644 --- a/Kyoo/Controllers/ConfigurationManager.cs +++ b/Kyoo/Controllers/ConfigurationManager.cs @@ -6,6 +6,7 @@ using System.Linq; using System.Threading.Tasks; using Kyoo.Api; using Kyoo.Models; +using Kyoo.Models.Exceptions; using Microsoft.Extensions.Configuration; using Newtonsoft.Json.Linq; @@ -34,17 +35,18 @@ namespace Kyoo.Controllers _references = references.ToDictionary(x => x.Path, x => x.Type, StringComparer.OrdinalIgnoreCase); } - /// - public Task EditValue(string path, T value) + public async Task EditValue(string path, object value) { - return EditValue(path, value, typeof(T)); - } - - /// - public async Task EditValue(string path, object value, Type type) - { - JObject obj = JObject.FromObject(ToObject(_configuration)); + path = path.Replace("__", ":"); + if (!_references.TryGetValue(path, out Type type)) + throw new ItemNotFoundException($"No configuration exists for the name: {path}"); + + ExpandoObject config = ToObject(_configuration); + IDictionary configDic = config; + // TODO validate the type + configDic[path] = value; + JObject obj = JObject.FromObject(config); // TODO allow path to change await using StreamWriter writer = new("settings.json"); await writer.WriteAsync(obj.ToString()); @@ -56,7 +58,7 @@ namespace Kyoo.Controllers /// /// The configuration to transform /// A strongly typed representation of the configuration. - private object ToObject(IConfiguration config) + private ExpandoObject ToObject(IConfiguration config) { ExpandoObject obj = new(); diff --git a/Kyoo/Views/ConfigurationApi.cs b/Kyoo/Views/ConfigurationApi.cs index dd6dbc9b..71697551 100644 --- a/Kyoo/Views/ConfigurationApi.cs +++ b/Kyoo/Views/ConfigurationApi.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Threading.Tasks; using Kyoo.Controllers; using Kyoo.Models; +using Kyoo.Models.Exceptions; using Kyoo.Models.Permissions; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; @@ -18,7 +19,7 @@ namespace Kyoo.Api [ApiController] public class ConfigurationApi : Controller { - private readonly ConfigurationManager _manager; + private readonly IConfigurationManager _manager; /// /// The configuration to retrieve and edit. @@ -35,7 +36,7 @@ namespace Kyoo.Api /// /// The configuration to use. /// The strongly typed option list. - public ConfigurationApi(ConfigurationManager manager, IConfiguration configuration, IEnumerable references) + public ConfigurationApi(IConfigurationManager manager, IConfiguration configuration, IEnumerable references) { _manager = manager; _configuration = configuration; @@ -77,12 +78,15 @@ namespace Kyoo.Api [Permission(nameof(ConfigurationApi), Kind.Admin)] public async Task> EditConfiguration(string slug, [FromBody] object newValue) { - slug = slug.Replace("__", ":"); - if (!_references.TryGetValue(slug, out Type type)) + try + { + await _manager.EditValue(slug, newValue); + return newValue; + } + catch (ItemNotFoundException) + { return NotFound(); - await _manager.EditValue(slug, newValue, type); - // await _configuration.SetValue(slug, newValue, type); - return newValue; + } } } } \ No newline at end of file