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:
parent
63b33c5fc0
commit
f21639455d
@ -1,3 +1,5 @@
|
||||
#nullable enable
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
|
@ -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<AppConfig> 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<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()
|
||||
{
|
||||
try
|
||||
|
@ -1,3 +1,5 @@
|
||||
#nullable enable
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
@ -44,7 +46,7 @@ namespace TransmissionRssManager.Services
|
||||
Task<List<LogEntry>> GetLogsAsync(LogFilterOptions options);
|
||||
Task ClearLogsAsync(DateTime? olderThan = null);
|
||||
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
|
||||
@ -166,7 +168,7 @@ namespace TransmissionRssManager.Services
|
||||
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();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user