diff --git a/EnvelopeGenerator.Infrastructure/UserPreferencesService.cs b/EnvelopeGenerator.Infrastructure/UserPreferencesService.cs index a8443a8c..963c24ce 100644 --- a/EnvelopeGenerator.Infrastructure/UserPreferencesService.cs +++ b/EnvelopeGenerator.Infrastructure/UserPreferencesService.cs @@ -16,26 +16,31 @@ public sealed class UserPreferencesService(IDistributedCache cache) : IUserPrefe { private const string KeyPrefix = "signflow.user-preferences:"; - private static string BuildKey(string username) => $"{KeyPrefix}{username}"; + private static string BuildKey(string id) => $"{KeyPrefix}{id}"; /// - public async Task GetAsync(string username, CancellationToken ct = default) + public async Task GetOrCreateAsync(string id, CancellationToken ct = default) { - var json = await cache.GetStringAsync(BuildKey(username), ct); - - if (json is null) - return new UserPreferencesDto(); - - return JsonSerializer.Deserialize(json) ?? new UserPreferencesDto(); + var json = await cache.GetStringAsync(BuildKey(id), ct); + if(json is not null && JsonSerializer.Deserialize(json) is UserPreferencesDto pref) + { + return pref; + } + else + { + // init new + await SaveAsync(id, UserPreferencesDto.Default, ct); + return UserPreferencesDto.Default; + } } /// - public async Task SaveAsync(string username, UserPreferencesDto preferences, CancellationToken ct = default) + public async Task SaveAsync(string id, UserPreferencesDto preferences, CancellationToken ct = default) { var json = JsonSerializer.Serialize(preferences); // No expiration — preferences are stored indefinitely. - await cache.SetStringAsync(BuildKey(username), json, new DistributedCacheEntryOptions(), ct); + await cache.SetStringAsync(BuildKey(id), json, new DistributedCacheEntryOptions(), ct); } } #endif