diff --git a/src/Core/Interfaces.cs b/src/Core/Interfaces.cs index 4d1651e..78d59b2 100644 --- a/src/Core/Interfaces.cs +++ b/src/Core/Interfaces.cs @@ -1,3 +1,5 @@ +#nullable enable + using System; using System.Collections.Generic; using System.Threading; diff --git a/src/Services/ConfigService.cs b/src/Services/ConfigService.cs index 612062b..1e1936f 100644 --- a/src/Services/ConfigService.cs +++ b/src/Services/ConfigService.cs @@ -35,6 +35,44 @@ namespace TransmissionRssManager.Services _logger.LogInformation($"Using configuration file: {_configFilePath}"); } + // Implement the interface methods required by IConfigService + public AppConfig GetConfiguration() + { + // Non-async method required by interface + if (_cachedConfig != null) + { + return _cachedConfig; + } + + try + { + // Load synchronously since this is a sync method + _cachedConfig = LoadConfigFromFileSync(); + return _cachedConfig; + } + catch (Exception ex) + { + _logger.LogError(ex, "Error loading configuration, using default values"); + return CreateDefaultConfig(); + } + } + + public async Task SaveConfigurationAsync(AppConfig config) + { + try + { + _cachedConfig = config; + await SaveConfigToFileAsync(config); + _logger.LogInformation("Configuration saved successfully to file"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error saving configuration to file"); + throw; + } + } + + // Additional methods for backward compatibility public async Task GetConfigAsync() { if (_cachedConfig != null) @@ -56,17 +94,7 @@ namespace TransmissionRssManager.Services public async Task SaveConfigAsync(AppConfig config) { - try - { - _cachedConfig = config; - await SaveConfigToFileAsync(config); - _logger.LogInformation("Configuration saved successfully to file"); - } - catch (Exception ex) - { - _logger.LogError(ex, "Error saving configuration to file"); - throw; - } + await SaveConfigurationAsync(config); } public async Task GetSettingAsync(string key, string defaultValue = "") @@ -245,6 +273,46 @@ namespace TransmissionRssManager.Services } } + private AppConfig LoadConfigFromFileSync() + { + try + { + if (!File.Exists(_configFilePath)) + { + _logger.LogWarning($"Configuration file not found at {_configFilePath}, creating default config"); + var defaultConfig = CreateDefaultConfig(); + // Save synchronously since we're in a sync method + File.WriteAllText(_configFilePath, JsonSerializer.Serialize(defaultConfig, new JsonSerializerOptions + { + WriteIndented = true, + PropertyNamingPolicy = JsonNamingPolicy.CamelCase + })); + return defaultConfig; + } + + string json = File.ReadAllText(_configFilePath); + var config = JsonSerializer.Deserialize(json, new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true + }); + + if (config == null) + { + throw new InvalidOperationException("Failed to deserialize configuration"); + } + + // Fill in any missing values with defaults + EnsureCompleteConfig(config); + + return config; + } + catch (Exception ex) + { + _logger.LogError(ex, "Error loading configuration from file"); + throw; + } + } + private async Task LoadConfigFromFileAsync() { try diff --git a/src/Services/LoggingService.cs b/src/Services/LoggingService.cs index b9c8ad7..39b843d 100644 --- a/src/Services/LoggingService.cs +++ b/src/Services/LoggingService.cs @@ -1,3 +1,5 @@ +#nullable enable + using System; using System.Collections.Generic; using System.IO; @@ -44,7 +46,7 @@ namespace TransmissionRssManager.Services Task> GetLogsAsync(LogFilterOptions options); Task ClearLogsAsync(DateTime? olderThan = null); Task ExportLogsAsync(LogFilterOptions options); - void Log(LogLevel level, string message, string context = null, Dictionary properties = null); + void Log(LogLevel level, string message, string? context = null, Dictionary? properties = null); } public class LogFilterOptions @@ -166,7 +168,7 @@ namespace TransmissionRssManager.Services return Encoding.UTF8.GetBytes(json); } - public void Log(LogLevel level, string message, string context = null, Dictionary properties = null) + public void Log(LogLevel level, string message, string? context = null, Dictionary? properties = null) { var levelString = level.ToString();