feat(Konstanten): Erzeugen von Optionen aus Appsettings über IOptions.

- Konstantenmodell mit UserLanguages String-Array erstellen. Hinzufügen einer benutzerdefinierten Methode zum Abrufen der Eigenschaft über Name.
- imlement in appsettings und confgire in Program.cs
This commit is contained in:
Developer 02 2024-09-26 13:03:49 +02:00
parent 84982e0286
commit 698151baf3
4 changed files with 52 additions and 2 deletions

View File

@ -0,0 +1,27 @@
using DigitalData.UserManager.API.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
namespace DigitalData.UserManager.API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ConstantsController : ControllerBase
{
private readonly Constants _constants;
public ConstantsController(IOptions<Constants> constantsOptions)
{
_constants = constantsOptions.Value;
}
[HttpGet]
public IActionResult GetConstant(string? name = null)
{
if(name is null)
return Ok(_constants);
return Ok(_constants[name]);
}
}
}

View File

@ -0,0 +1,13 @@
using System.Reflection;
namespace DigitalData.UserManager.API.Models
{
public class Constants
{
public IEnumerable<string> UserLanguages { get; init; } = Array.Empty<string>();
public object? this[string propertyName] => GetType()
.GetProperty(propertyName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance)?
.GetValue(this, null);
}
}

View File

@ -9,6 +9,8 @@ using DigitalData.Core.API;
using DigitalData.UserManager.API.Controllers;
using DigitalData.UserManager.Application.Services;
using Microsoft.Data.SqlClient;
using System.Reflection.Metadata;
using DigitalData.UserManager.API.Models;
var logger = LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();
logger.Debug("init main");
@ -77,7 +79,9 @@ try {
builder.Services.AddDirectorySearchService();
builder.Services.AddCookieBasedLocalizer();
builder.ConfigureBySection<Constants>();
var app = builder.Build();
cnn_str = new(() =>

View File

@ -73,5 +73,11 @@
},
// Delete below in production
"UseEncryptor": true,
"UseSwagger": true
"UseSwagger": true,
"Constants": {
"UserLanguages": [
"de-DE",
"en-US"
]
}
}