Add server configuration service and repo

This commit is contained in:
Holden
2026-06-18 13:43:59 -05:00
parent 7fc7e1dc91
commit 608def3ac8
15 changed files with 272 additions and 9 deletions

View File

@@ -1,4 +1,5 @@
using Microsoft.Extensions.DependencyInjection;
using RecNet.Application.Services.Configuration;
namespace RecNet.Application;
@@ -7,6 +8,8 @@ public static class DependencyInjection
public static IServiceCollection AddApplication(this IServiceCollection services)
{
services.AddAutoMapper(_ => { }, typeof(DependencyInjection).Assembly);
services.AddScoped<IConfigService, ConfigService>();
return services;
}

View File

@@ -0,0 +1,33 @@
using System.Text.Json;
using RecNet.Domain.Repositories;
namespace RecNet.Application.Services.Configuration;
public class ConfigService(IServerConfigRepository repository) : IConfigService
{
private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web);
public async Task<T?> GetAsync<T>(
string key,
T? defaultValue = default,
CancellationToken ct = default)
{
var config = await repository.GetAsync(key, ct);
if (config == null)
return defaultValue;
return JsonSerializer.Deserialize<T>(config.Value, JsonOptions);
}
public async Task SetAsync<T>(
string key,
T value,
CancellationToken ct = default)
{
var json = JsonSerializer.Serialize(value, JsonOptions);
await repository.SetAsync(key, json, ct);
await repository.SaveChangesAsync(ct);
}
}

View File

@@ -0,0 +1,7 @@
namespace RecNet.Application.Services.Configuration;
public interface IConfigService
{
Task<T?> GetAsync<T>(string key, T? defaultValue = default, CancellationToken ct = default);
Task SetAsync<T>(string key, T value, CancellationToken ct = default);
}