Migrate from MongoDB to Couchbase Lite for local storage; update related services and configurations
Some checks failed
Publish Container / publish (push) Failing after 3m43s

This commit is contained in:
Maddo 2026-03-17 13:53:33 +01:00
commit f976d70db8
24 changed files with 328 additions and 218 deletions

View file

@ -0,0 +1,96 @@
using Couchbase.Lite;
using WorkTracker.Domain;
using WorkTracker.Services.Storage;
namespace WorkTracker.Services.Settings;
public sealed class CouchbaseLiteAppSettingsService : IAppSettingsService
{
private const string DefaultSettingsId = "global";
private readonly Collection appSettingsCollection;
public CouchbaseLiteAppSettingsService(CouchbaseLiteDatabaseProvider databaseProvider)
{
appSettingsCollection = databaseProvider.AppSettings;
}
public Task<AppSettingsDocument> GetAsync(CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
var document = appSettingsCollection.GetDocument(DefaultSettingsId);
if (document is not null)
{
return Task.FromResult(Map(document));
}
var defaults = new AppSettingsDocument();
SaveDocument(defaults);
return Task.FromResult(defaults);
}
public async Task<AppSettingsDocument> SaveAsync(AppSettingsDocument settings, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
var existing = await GetAsync(cancellationToken);
settings.Id = DefaultSettingsId;
settings.CreatedAtUtc = existing.CreatedAtUtc;
settings.UpdatedAtUtc = DateTimeOffset.UtcNow;
SaveDocument(settings);
return settings;
}
private void SaveDocument(AppSettingsDocument settings)
{
var document = new MutableDocument(DefaultSettingsId);
document.SetDouble("standardWorkHoursPerDay", Decimal.ToDouble(settings.StandardWorkHoursPerDay));
document.SetDouble("lunchBreakHours", Decimal.ToDouble(settings.LunchBreakHours));
document.SetDouble("hourlyGrossRate", Decimal.ToDouble(settings.HourlyGrossRate));
document.SetDouble("profitabilityCoefficient", Decimal.ToDouble(settings.ProfitabilityCoefficient));
document.SetDouble("inpsRate", Decimal.ToDouble(settings.InpsRate));
document.SetDouble("substituteTaxRate", Decimal.ToDouble(settings.SubstituteTaxRate));
document.SetString("currency", settings.Currency);
document.SetString("locale", settings.Locale);
document.SetString("createdAtUtc", settings.CreatedAtUtc.ToString("O"));
document.SetString("updatedAtUtc", settings.UpdatedAtUtc.ToString("O"));
appSettingsCollection.Save(document);
}
private static AppSettingsDocument Map(Document document)
{
return new AppSettingsDocument
{
Id = document.Id,
StandardWorkHoursPerDay = ReadDecimal(document, "standardWorkHoursPerDay", 8m),
LunchBreakHours = ReadDecimal(document, "lunchBreakHours", 1m),
HourlyGrossRate = ReadDecimal(document, "hourlyGrossRate", 17.5m),
ProfitabilityCoefficient = ReadDecimal(document, "profitabilityCoefficient", 0.67m),
InpsRate = ReadDecimal(document, "inpsRate", 0.2607m),
SubstituteTaxRate = ReadDecimal(document, "substituteTaxRate", 0.15m),
Currency = document.GetString("currency") ?? "EUR",
Locale = document.GetString("locale") ?? "it-IT",
CreatedAtUtc = ReadDateTimeOffset(document, "createdAtUtc"),
UpdatedAtUtc = ReadDateTimeOffset(document, "updatedAtUtc")
};
}
private static decimal ReadDecimal(Document document, string key, decimal defaultValue)
{
return document.Contains(key)
? Convert.ToDecimal(document.GetDouble(key))
: defaultValue;
}
private static DateTimeOffset ReadDateTimeOffset(Document document, string key)
{
var value = document.GetString(key);
return DateTimeOffset.TryParse(value, out var parsed)
? parsed
: DateTimeOffset.UtcNow;
}
}

View file

@ -1,44 +0,0 @@
using MongoDB.Driver;
using WorkTracker.Domain;
namespace WorkTracker.Services.Settings;
public sealed class MongoAppSettingsService : IAppSettingsService
{
private const string DefaultSettingsId = "global";
private readonly IMongoCollection<AppSettingsDocument> appSettingsCollection;
public MongoAppSettingsService(IMongoDatabase database)
{
appSettingsCollection = database.GetCollection<AppSettingsDocument>("app_settings");
}
public async Task<AppSettingsDocument> GetAsync(CancellationToken cancellationToken = default)
{
var filter = Builders<AppSettingsDocument>.Filter.Eq(x => x.Id, DefaultSettingsId);
var settings = await appSettingsCollection.Find(filter).FirstOrDefaultAsync(cancellationToken);
if (settings is not null)
{
return settings;
}
var defaults = new AppSettingsDocument();
await appSettingsCollection.InsertOneAsync(defaults, cancellationToken: cancellationToken);
return defaults;
}
public async Task<AppSettingsDocument> SaveAsync(AppSettingsDocument settings, CancellationToken cancellationToken = default)
{
var existing = await GetAsync(cancellationToken);
settings.Id = DefaultSettingsId;
settings.CreatedAtUtc = existing.CreatedAtUtc;
settings.UpdatedAtUtc = DateTimeOffset.UtcNow;
var filter = Builders<AppSettingsDocument>.Filter.Eq(x => x.Id, DefaultSettingsId);
await appSettingsCollection.ReplaceOneAsync(filter, settings, new ReplaceOptions { IsUpsert = true }, cancellationToken);
return settings;
}
}