fix: Implement required interface methods and address nullable warnings

- Added GetConfiguration() and SaveConfigurationAsync() methods to ConfigService
- Added synchronous file reading for GetConfiguration()
- Fixed nullable reference type warnings in LoggingService
- Added nullable annotations to interface definitions
- Enabled nullable reference types for affected files

🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
MasterDraco 2025-03-12 22:36:39 +00:00
parent 63b33c5fc0
commit f21639455d
3 changed files with 85 additions and 13 deletions

View File

@ -1,3 +1,5 @@
#nullable enable
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading; using System.Threading;

View File

@ -35,6 +35,44 @@ namespace TransmissionRssManager.Services
_logger.LogInformation($"Using configuration file: {_configFilePath}"); _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<AppConfig> GetConfigAsync() public async Task<AppConfig> GetConfigAsync()
{ {
if (_cachedConfig != null) if (_cachedConfig != null)
@ -56,17 +94,7 @@ namespace TransmissionRssManager.Services
public async Task SaveConfigAsync(AppConfig config) public async Task SaveConfigAsync(AppConfig config)
{ {
try await SaveConfigurationAsync(config);
{
_cachedConfig = config;
await SaveConfigToFileAsync(config);
_logger.LogInformation("Configuration saved successfully to file");
}
catch (Exception ex)
{
_logger.LogError(ex, "Error saving configuration to file");
throw;
}
} }
public async Task<string> GetSettingAsync(string key, string defaultValue = "") public async Task<string> 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<AppConfig>(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<AppConfig> LoadConfigFromFileAsync() private async Task<AppConfig> LoadConfigFromFileAsync()
{ {
try try

View File

@ -1,3 +1,5 @@
#nullable enable
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
@ -44,7 +46,7 @@ namespace TransmissionRssManager.Services
Task<List<LogEntry>> GetLogsAsync(LogFilterOptions options); Task<List<LogEntry>> GetLogsAsync(LogFilterOptions options);
Task ClearLogsAsync(DateTime? olderThan = null); Task ClearLogsAsync(DateTime? olderThan = null);
Task<byte[]> ExportLogsAsync(LogFilterOptions options); Task<byte[]> ExportLogsAsync(LogFilterOptions options);
void Log(LogLevel level, string message, string context = null, Dictionary<string, string> properties = null); void Log(LogLevel level, string message, string? context = null, Dictionary<string, string>? properties = null);
} }
public class LogFilterOptions public class LogFilterOptions
@ -166,7 +168,7 @@ namespace TransmissionRssManager.Services
return Encoding.UTF8.GetBytes(json); return Encoding.UTF8.GetBytes(json);
} }
public void Log(LogLevel level, string message, string context = null, Dictionary<string, string> properties = null) public void Log(LogLevel level, string message, string? context = null, Dictionary<string, string>? properties = null)
{ {
var levelString = level.ToString(); var levelString = level.ToString();