50 lines
1.5 KiB
C#
50 lines
1.5 KiB
C#
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;
|
|
}
|
|
}
|
|
}
|
|
|