Cleaning up the configuration edit

This commit is contained in:
Zoe Roux 2021-05-18 22:41:58 +02:00
parent d115797dd7
commit f3e415a050
3 changed files with 25 additions and 30 deletions

View File

@ -1,4 +1,3 @@
using System;
using System.Threading.Tasks;
using Kyoo.Models;
using Kyoo.Models.Exceptions;
@ -15,19 +14,9 @@ namespace Kyoo.Controllers
/// <summary>
/// Edit the value of a setting using it's path. Save it to the json file.
/// </summary>
/// <param name="path">The path of the resource (can be separated by ':' or '__'</param>
/// <param name="path">The path of the resource (can be separated by ':' or '__')</param>
/// <param name="value">The new value of the resource</param>
/// <typeparam name="T">The type of the resource</typeparam>
/// <exception cref="ItemNotFoundException">No setting found at the given path.</exception>
Task EditValue<T>(string path, T value);
/// <summary>
/// Edit the value of a setting using it's path. Save it to the json file.
/// </summary>
/// <param name="path">The path of the resource (can be separated by ':' or '__'</param>
/// <param name="value">The new value of the resource</param>
/// <param name="type">The type of the resource</param>
/// <exception cref="ItemNotFoundException">No setting found at the given path.</exception>
Task EditValue(string path, object value, Type type);
Task EditValue(string path, object value);
}
}

View File

@ -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);
}
/// <inheritdoc />
public Task EditValue<T>(string path, T value)
public async Task EditValue(string path, object value)
{
return EditValue(path, value, typeof(T));
}
/// <inheritdoc />
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<string, object> 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
/// </summary>
/// <param name="config">The configuration to transform</param>
/// <returns>A strongly typed representation of the configuration.</returns>
private object ToObject(IConfiguration config)
private ExpandoObject ToObject(IConfiguration config)
{
ExpandoObject obj = new();

View File

@ -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;
/// <summary>
/// The configuration to retrieve and edit.
@ -35,7 +36,7 @@ namespace Kyoo.Api
/// </summary>
/// <param name="configuration">The configuration to use.</param>
/// <param name="references">The strongly typed option list.</param>
public ConfigurationApi(ConfigurationManager manager, IConfiguration configuration, IEnumerable<ConfigurationReference> references)
public ConfigurationApi(IConfigurationManager manager, IConfiguration configuration, IEnumerable<ConfigurationReference> references)
{
_manager = manager;
_configuration = configuration;
@ -77,12 +78,15 @@ namespace Kyoo.Api
[Permission(nameof(ConfigurationApi), Kind.Admin)]
public async Task<ActionResult<object>> 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;
}
}
}
}