Started implementing setup wizard backend for initial instance configuration. Adjusted ui

This commit is contained in:
2026-01-22 16:24:53 +01:00
parent 248af498a8
commit bb5737bd0b
6 changed files with 257 additions and 9 deletions

View File

@@ -0,0 +1,79 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
using Moonlight.Api.Configuration;
using Moonlight.Api.Database;
using Moonlight.Api.Database.Entities;
namespace Moonlight.Api.Services;
public class SettingsService
{
private readonly DatabaseRepository<SettingsOption> Repository;
private readonly IOptions<SettingsOptions> Options;
private readonly IMemoryCache Cache;
private const string CacheKey = "Moonlight.Api.SettingsService.{0}";
public SettingsService(
DatabaseRepository<SettingsOption> repository,
IOptions<SettingsOptions> options,
IMemoryCache cache
)
{
Repository = repository;
Cache = cache;
Options = options;
}
public async Task<string?> GetValueAsync(string key)
{
var cacheKey = string.Format(CacheKey, key);
if (!Cache.TryGetValue<string>(cacheKey, out var value))
{
var retrievedValue = await Repository
.Query()
.Where(x => x.Key == key)
.Select(o => o.Value)
.FirstOrDefaultAsync();
Cache.Set(
cacheKey,
retrievedValue,
TimeSpan.FromMinutes(Options.Value.CacheMinutes)
);
return retrievedValue;
}
return value;
}
public async Task SetValueAsync(string key, string value)
{
var cacheKey = string.Format(CacheKey, key);
var option = await Repository
.Query()
.FirstOrDefaultAsync(x => x.Key == key);
if (option != null)
{
option.Value = value;
await Repository.UpdateAsync(option);
}
else
{
option = new SettingsOption()
{
Key = key,
Value = value
};
await Repository.AddAsync(option);
}
Cache.Remove(cacheKey);
}
}