using System; using System.Collections.Generic; using System.Threading.Tasks; using QuantEngine.Core.Interfaces; using QuantEngine.Core.Models; namespace QuantEngine.Application.Services { public class WorkspaceService { private readonly IWorkspaceRepository _repository; private readonly IPostgresqlHistoryStore _historyStore; public WorkspaceService(IWorkspaceRepository repository, IPostgresqlHistoryStore historyStore) { _repository = repository; _historyStore = historyStore; } public Task> GetSettingsAsync() => _repository.GetSettingsAsync(); public Task GetSettingByKeyAsync(string key) => _repository.GetSettingByKeyAsync(RequireValue(key, nameof(key))); public Task UpsertSettingAsync(Setting setting) { ArgumentNullException.ThrowIfNull(setting); return _repository.UpsertSettingAsync(setting); } public Task DeleteSettingAsync(string key) => _repository.DeleteSettingAsync(RequireValue(key, nameof(key))); public Task> GetAccountSnapshotsAsync() => _repository.GetAccountSnapshotsAsync(); public Task InsertAccountSnapshotsAsync(IEnumerable snapshots) => _repository.InsertAccountSnapshotsAsync(snapshots); public Task ClearAccountSnapshotsAsync() => _repository.ClearAccountSnapshotsAsync(); public Task AppendHistoryAsync(string domain, IDictionary payload) => _historyStore.AppendAsync(RequireValue(domain, nameof(domain)), payload); public Task>> ReadHistorySnapshotAsync(string domain, int limit = 500) => _historyStore.SnapshotAsync(RequireValue(domain, nameof(domain)), Math.Clamp(limit, 1, 2000)); private static string RequireValue(string value, string parameterName) { if (string.IsNullOrWhiteSpace(value)) { throw new ArgumentException("Value is required.", parameterName); } return value.Trim(); } } }