AuthorBuddy/Services/LocalStorageSyncService.cs

51 lines
1.5 KiB
C#
Raw Permalink Normal View History

2026-09-21 18:18:07 +02:00
using AuthorBuddy.Web.Data;
using LiteDB;
namespace AuthorBuddy.Web.Services
{
public class LocalStorageSyncService : ILocalStorageSyncService
{
private readonly ILiteCollection<UserLocalStorageSync> _collection;
private ISettingsService Settings { get; }
public LocalStorageSyncService(ILiteDatabase database, ISettingsService settings)
{
_collection = database.GetCollection<UserLocalStorageSync>("UserStorageBackup");
Settings = settings;
}
public async Task SaveStateAsync(Dictionary<string, string> data)
{
var user = await Settings.GetCurrentUserAsync();
if (user == null)
return;
var result = _collection.FindOne(x => x.UserId == user.Id);
var insert = false;
if (result == null)
{
result = new UserLocalStorageSync();
result.UserId = user.Id;
insert = true;
}
result.LastSyncedAt = DateTime.Now;
result.StorageData = data;
if (insert)
_collection.Insert(result);
else
_collection.Update(result);
}
public async Task<Dictionary<string, string>?> GetStateAsync()
{
var user = await Settings.GetCurrentUserAsync();
if (user == null)
return new();
var result = _collection.FindOne(x => x.UserId == user.Id);
return result?.StorageData;
}
}
}