From a9ff0d88f2213d91351ba8b1c077ec985be672fe Mon Sep 17 00:00:00 2001 From: TekH Date: Thu, 3 Sep 2026 14:57:32 +0200 Subject: [PATCH] =?UTF-8?q?SF-74:=20Infrastructure=20=E2=80=94=20UserPrefe?= =?UTF-8?q?rencesService=20(IDistributedCache,=20no=20try-catch)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../UserPreferencesService.cs | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) 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