Updated settings service to support generic types with JSON serialization/deserialization, adjusted setup wizard checks and modified database schema for proper JSONB storage.

This commit is contained in:
2026-01-29 14:47:56 +01:00
parent bb5737bd0b
commit 743f41cbe8
6 changed files with 331 additions and 28 deletions

View File

@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using System.Text.Json;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
using Moonlight.Api.Configuration;
@@ -26,31 +27,32 @@ public class SettingsService
Options = options;
}
public async Task<string?> GetValueAsync(string key)
public async Task<T?> GetValueAsync<T>(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();
if (Cache.TryGetValue<string>(cacheKey, out var value))
return JsonSerializer.Deserialize<T>(value!);
value = await Repository
.Query()
.Where(x => x.Key == key)
.Select(o => o.ValueJson)
.FirstOrDefaultAsync();
if(string.IsNullOrEmpty(value))
return default;
Cache.Set(
cacheKey,
retrievedValue,
TimeSpan.FromMinutes(Options.Value.CacheMinutes)
);
Cache.Set(
cacheKey,
value,
TimeSpan.FromMinutes(Options.Value.CacheMinutes)
);
return retrievedValue;
}
return value;
return JsonSerializer.Deserialize<T>(value);
}
public async Task SetValueAsync(string key, string value)
public async Task SetValueAsync<T>(string key, T value)
{
var cacheKey = string.Format(CacheKey, key);
@@ -58,9 +60,11 @@ public class SettingsService
.Query()
.FirstOrDefaultAsync(x => x.Key == key);
var json = JsonSerializer.Serialize(value);
if (option != null)
{
option.Value = value;
option.ValueJson = json;
await Repository.UpdateAsync(option);
}
else
@@ -68,7 +72,7 @@ public class SettingsService
option = new SettingsOption()
{
Key = key,
Value = value
ValueJson = json
};
await Repository.AddAsync(option);