mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-07-09 03:04:20 -04:00
Cleaning up the configuration edit
This commit is contained in:
parent
d115797dd7
commit
f3e415a050
@ -1,4 +1,3 @@
|
|||||||
using System;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Kyoo.Models;
|
using Kyoo.Models;
|
||||||
using Kyoo.Models.Exceptions;
|
using Kyoo.Models.Exceptions;
|
||||||
@ -15,19 +14,9 @@ namespace Kyoo.Controllers
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Edit the value of a setting using it's path. Save it to the json file.
|
/// Edit the value of a setting using it's path. Save it to the json file.
|
||||||
/// </summary>
|
/// </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>
|
/// <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>
|
/// <exception cref="ItemNotFoundException">No setting found at the given path.</exception>
|
||||||
Task EditValue<T>(string path, T value);
|
Task EditValue(string path, object 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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -6,6 +6,7 @@ using System.Linq;
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Kyoo.Api;
|
using Kyoo.Api;
|
||||||
using Kyoo.Models;
|
using Kyoo.Models;
|
||||||
|
using Kyoo.Models.Exceptions;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
|
|
||||||
@ -34,17 +35,18 @@ namespace Kyoo.Controllers
|
|||||||
_references = references.ToDictionary(x => x.Path, x => x.Type, StringComparer.OrdinalIgnoreCase);
|
_references = references.ToDictionary(x => x.Path, x => x.Type, StringComparer.OrdinalIgnoreCase);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public Task EditValue<T>(string path, T value)
|
public async Task EditValue(string path, object value)
|
||||||
{
|
{
|
||||||
return EditValue(path, value, typeof(T));
|
path = path.Replace("__", ":");
|
||||||
}
|
if (!_references.TryGetValue(path, out Type type))
|
||||||
|
throw new ItemNotFoundException($"No configuration exists for the name: {path}");
|
||||||
|
|
||||||
/// <inheritdoc />
|
ExpandoObject config = ToObject(_configuration);
|
||||||
public async Task EditValue(string path, object value, Type type)
|
IDictionary<string, object> configDic = config;
|
||||||
{
|
// TODO validate the type
|
||||||
JObject obj = JObject.FromObject(ToObject(_configuration));
|
configDic[path] = value;
|
||||||
|
JObject obj = JObject.FromObject(config);
|
||||||
// TODO allow path to change
|
// TODO allow path to change
|
||||||
await using StreamWriter writer = new("settings.json");
|
await using StreamWriter writer = new("settings.json");
|
||||||
await writer.WriteAsync(obj.ToString());
|
await writer.WriteAsync(obj.ToString());
|
||||||
@ -56,7 +58,7 @@ namespace Kyoo.Controllers
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="config">The configuration to transform</param>
|
/// <param name="config">The configuration to transform</param>
|
||||||
/// <returns>A strongly typed representation of the configuration.</returns>
|
/// <returns>A strongly typed representation of the configuration.</returns>
|
||||||
private object ToObject(IConfiguration config)
|
private ExpandoObject ToObject(IConfiguration config)
|
||||||
{
|
{
|
||||||
ExpandoObject obj = new();
|
ExpandoObject obj = new();
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ using System.Linq;
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Kyoo.Controllers;
|
using Kyoo.Controllers;
|
||||||
using Kyoo.Models;
|
using Kyoo.Models;
|
||||||
|
using Kyoo.Models.Exceptions;
|
||||||
using Kyoo.Models.Permissions;
|
using Kyoo.Models.Permissions;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
@ -18,7 +19,7 @@ namespace Kyoo.Api
|
|||||||
[ApiController]
|
[ApiController]
|
||||||
public class ConfigurationApi : Controller
|
public class ConfigurationApi : Controller
|
||||||
{
|
{
|
||||||
private readonly ConfigurationManager _manager;
|
private readonly IConfigurationManager _manager;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The configuration to retrieve and edit.
|
/// The configuration to retrieve and edit.
|
||||||
@ -35,7 +36,7 @@ namespace Kyoo.Api
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="configuration">The configuration to use.</param>
|
/// <param name="configuration">The configuration to use.</param>
|
||||||
/// <param name="references">The strongly typed option list.</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;
|
_manager = manager;
|
||||||
_configuration = configuration;
|
_configuration = configuration;
|
||||||
@ -77,12 +78,15 @@ namespace Kyoo.Api
|
|||||||
[Permission(nameof(ConfigurationApi), Kind.Admin)]
|
[Permission(nameof(ConfigurationApi), Kind.Admin)]
|
||||||
public async Task<ActionResult<object>> EditConfiguration(string slug, [FromBody] object newValue)
|
public async Task<ActionResult<object>> EditConfiguration(string slug, [FromBody] object newValue)
|
||||||
{
|
{
|
||||||
slug = slug.Replace("__", ":");
|
try
|
||||||
if (!_references.TryGetValue(slug, out Type type))
|
{
|
||||||
return NotFound();
|
await _manager.EditValue(slug, newValue);
|
||||||
await _manager.EditValue(slug, newValue, type);
|
|
||||||
// await _configuration.SetValue(slug, newValue, type);
|
|
||||||
return newValue;
|
return newValue;
|
||||||
}
|
}
|
||||||
|
catch (ItemNotFoundException)
|
||||||
|
{
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user