using System; using System.Threading.Tasks; using Kyoo.Abstractions.Controllers; using Kyoo.Abstractions.Models.Exceptions; using Kyoo.Abstractions.Models.Permissions; using Microsoft.AspNetCore.Mvc; namespace Kyoo.Core.Api { /// /// An API to retrieve or edit configuration settings /// [Route("api/config")] [Route("api/configuration")] [ApiController] public class ConfigurationApi : Controller { /// /// The configuration manager used to retrieve and edit configuration values (while being type safe). /// private readonly IConfigurationManager _manager; /// /// Create a new using the given configuration. /// /// The configuration manager used to retrieve and edit configuration values public ConfigurationApi(IConfigurationManager manager) { _manager = manager; } /// /// Get a permission from it's slug. /// /// The permission to retrieve. You can use ':' or "__" to get a child value. /// The associate value or list of values. /// Return the configuration value or the list of configurations /// No configuration exists for the given slug [HttpGet("{slug}")] [Permission(nameof(ConfigurationApi), Kind.Read, Group.Admin)] public ActionResult GetConfiguration(string slug) { try { return _manager.GetValue(slug); } catch (ItemNotFoundException) { return NotFound(); } } /// /// Edit a permission from it's slug. /// /// The permission to edit. You can use ':' or "__" to get a child value. /// The new value of the configuration /// The edited value. /// Return the edited value /// No configuration exists for the given slug [HttpPut("{slug}")] [Permission(nameof(ConfigurationApi), Kind.Write, Group.Admin)] public async Task> EditConfiguration(string slug, [FromBody] object newValue) { try { await _manager.EditValue(slug, newValue); return newValue; } catch (ItemNotFoundException) { return NotFound(); } catch (ArgumentException ex) { return BadRequest(ex.Message); } } } }