feat(01-03): SettingsRepository and SettingsService with write-then-replace
- AppSettings model: DataFolder + Lang with camelCase JSON serialization - SettingsRepository: SemaphoreSlim write lock + write-then-replace (tmp→validate→move) - SettingsService: GetSettings/SetLanguage/SetDataFolder; SetLanguage validates en/fr only - All 8 SettingsServiceTests pass; all 18 Unit tests pass
This commit is contained in:
7
SharepointToolbox/Core/Models/AppSettings.cs
Normal file
7
SharepointToolbox/Core/Models/AppSettings.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace SharepointToolbox.Core.Models;
|
||||
|
||||
public class AppSettings
|
||||
{
|
||||
public string DataFolder { get; set; } = string.Empty;
|
||||
public string Lang { get; set; } = "en";
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using SharepointToolbox.Core.Models;
|
||||
|
||||
namespace SharepointToolbox.Infrastructure.Persistence;
|
||||
|
||||
public class SettingsRepository
|
||||
{
|
||||
private readonly string _filePath;
|
||||
private readonly SemaphoreSlim _writeLock = new(1, 1);
|
||||
|
||||
public SettingsRepository(string filePath)
|
||||
{
|
||||
_filePath = filePath;
|
||||
}
|
||||
|
||||
public async Task<AppSettings> LoadAsync()
|
||||
{
|
||||
if (!File.Exists(_filePath))
|
||||
return new AppSettings();
|
||||
|
||||
string json;
|
||||
try
|
||||
{
|
||||
json = await File.ReadAllTextAsync(_filePath, Encoding.UTF8);
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
throw new InvalidDataException($"Failed to read settings file: {_filePath}", ex);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var settings = JsonSerializer.Deserialize<AppSettings>(json,
|
||||
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
||||
return settings ?? new AppSettings();
|
||||
}
|
||||
catch (JsonException ex)
|
||||
{
|
||||
throw new InvalidDataException($"Settings file contains invalid JSON: {_filePath}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SaveAsync(AppSettings settings)
|
||||
{
|
||||
await _writeLock.WaitAsync();
|
||||
try
|
||||
{
|
||||
var json = JsonSerializer.Serialize(settings,
|
||||
new JsonSerializerOptions
|
||||
{
|
||||
WriteIndented = true,
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
||||
});
|
||||
|
||||
var tmpPath = _filePath + ".tmp";
|
||||
var dir = Path.GetDirectoryName(_filePath);
|
||||
if (!string.IsNullOrEmpty(dir))
|
||||
Directory.CreateDirectory(dir);
|
||||
|
||||
await File.WriteAllTextAsync(tmpPath, json, Encoding.UTF8);
|
||||
|
||||
// Validate round-trip before replacing
|
||||
JsonDocument.Parse(await File.ReadAllTextAsync(tmpPath, Encoding.UTF8)).Dispose();
|
||||
|
||||
File.Move(tmpPath, _filePath, overwrite: true);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_writeLock.Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
39
SharepointToolbox/Services/SettingsService.cs
Normal file
39
SharepointToolbox/Services/SettingsService.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using SharepointToolbox.Core.Models;
|
||||
using SharepointToolbox.Infrastructure.Persistence;
|
||||
|
||||
namespace SharepointToolbox.Services;
|
||||
|
||||
public class SettingsService
|
||||
{
|
||||
private readonly SettingsRepository _repository;
|
||||
|
||||
private static readonly HashSet<string> SupportedLanguages = new(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
"en", "fr"
|
||||
};
|
||||
|
||||
public SettingsService(SettingsRepository repository)
|
||||
{
|
||||
_repository = repository;
|
||||
}
|
||||
|
||||
public Task<AppSettings> GetSettingsAsync()
|
||||
=> _repository.LoadAsync();
|
||||
|
||||
public async Task SetLanguageAsync(string cultureCode)
|
||||
{
|
||||
if (!SupportedLanguages.Contains(cultureCode))
|
||||
throw new ArgumentException($"Unsupported language code '{cultureCode}'. Supported: en, fr.", nameof(cultureCode));
|
||||
|
||||
var settings = await _repository.LoadAsync();
|
||||
settings.Lang = cultureCode;
|
||||
await _repository.SaveAsync(settings);
|
||||
}
|
||||
|
||||
public async Task SetDataFolderAsync(string path)
|
||||
{
|
||||
var settings = await _repository.LoadAsync();
|
||||
settings.DataFolder = path;
|
||||
await _repository.SaveAsync(settings);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user