Initial commit
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using SharepointToolbox.Web.Core.Models;
|
||||
|
||||
namespace SharepointToolbox.Web.Infrastructure.Persistence;
|
||||
|
||||
/// <summary>Append-only JSONL audit log. Each line is one AuditEntry JSON object.</summary>
|
||||
public class AuditRepository
|
||||
{
|
||||
private readonly string _filePath;
|
||||
private readonly SemaphoreSlim _writeLock = new(1, 1);
|
||||
private static readonly JsonSerializerOptions _opts = new()
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
PropertyNameCaseInsensitive = true
|
||||
};
|
||||
|
||||
public AuditRepository(string filePath) { _filePath = filePath; }
|
||||
|
||||
public async Task AppendAsync(AuditEntry entry)
|
||||
{
|
||||
await _writeLock.WaitAsync();
|
||||
try
|
||||
{
|
||||
var dir = Path.GetDirectoryName(_filePath);
|
||||
if (!string.IsNullOrEmpty(dir)) Directory.CreateDirectory(dir);
|
||||
var line = JsonSerializer.Serialize(entry, _opts) + "\n";
|
||||
await File.AppendAllTextAsync(_filePath, line, Encoding.UTF8);
|
||||
}
|
||||
finally { _writeLock.Release(); }
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<AuditEntry>> LoadAllAsync()
|
||||
{
|
||||
if (!File.Exists(_filePath)) return Array.Empty<AuditEntry>();
|
||||
var lines = await File.ReadAllLinesAsync(_filePath, Encoding.UTF8);
|
||||
var result = new List<AuditEntry>(lines.Length);
|
||||
foreach (var line in lines)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(line)) continue;
|
||||
try
|
||||
{
|
||||
var entry = JsonSerializer.Deserialize<AuditEntry>(line, _opts);
|
||||
if (entry != null) result.Add(entry);
|
||||
}
|
||||
catch { /* skip corrupt lines */ }
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using SharepointToolbox.Web.Core.Models;
|
||||
|
||||
namespace SharepointToolbox.Web.Infrastructure.Persistence;
|
||||
|
||||
public class ProfileRepository
|
||||
{
|
||||
private readonly string _filePath;
|
||||
private readonly SemaphoreSlim _writeLock = new(1, 1);
|
||||
|
||||
public ProfileRepository(string filePath) { _filePath = filePath; }
|
||||
|
||||
public async Task<IReadOnlyList<TenantProfile>> LoadAsync()
|
||||
{
|
||||
if (!File.Exists(_filePath)) return Array.Empty<TenantProfile>();
|
||||
string json;
|
||||
try { json = await File.ReadAllTextAsync(_filePath, Encoding.UTF8); }
|
||||
catch (IOException ex) { throw new InvalidDataException($"Failed to read profiles: {_filePath}", ex); }
|
||||
|
||||
ProfilesRoot? root;
|
||||
try
|
||||
{
|
||||
root = JsonSerializer.Deserialize<ProfilesRoot>(json,
|
||||
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
||||
}
|
||||
catch (JsonException ex) { throw new InvalidDataException($"Invalid JSON in profiles: {_filePath}", ex); }
|
||||
|
||||
return (IReadOnlyList<TenantProfile>?)root?.Profiles ?? Array.Empty<TenantProfile>();
|
||||
}
|
||||
|
||||
public async Task SaveAsync(IReadOnlyList<TenantProfile> profiles)
|
||||
{
|
||||
await _writeLock.WaitAsync();
|
||||
try
|
||||
{
|
||||
var root = new ProfilesRoot { Profiles = profiles.ToList() };
|
||||
var json = JsonSerializer.Serialize(root, 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);
|
||||
JsonDocument.Parse(await File.ReadAllTextAsync(tmpPath, Encoding.UTF8)).Dispose();
|
||||
File.Move(tmpPath, _filePath, overwrite: true);
|
||||
}
|
||||
finally { _writeLock.Release(); }
|
||||
}
|
||||
|
||||
private sealed class ProfilesRoot { public List<TenantProfile> Profiles { get; set; } = new(); }
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using SharepointToolbox.Web.Core.Models;
|
||||
|
||||
namespace SharepointToolbox.Web.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();
|
||||
try
|
||||
{
|
||||
var json = await File.ReadAllTextAsync(_filePath, Encoding.UTF8);
|
||||
return JsonSerializer.Deserialize<AppSettings>(json,
|
||||
new JsonSerializerOptions { PropertyNameCaseInsensitive = true }) ?? new AppSettings();
|
||||
}
|
||||
catch { return new AppSettings(); }
|
||||
}
|
||||
|
||||
public async Task SaveAsync(AppSettings settings)
|
||||
{
|
||||
await _writeLock.WaitAsync();
|
||||
try
|
||||
{
|
||||
var json = JsonSerializer.Serialize(settings, new JsonSerializerOptions
|
||||
{
|
||||
WriteIndented = true, PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
||||
});
|
||||
var dir = Path.GetDirectoryName(_filePath);
|
||||
if (!string.IsNullOrEmpty(dir)) Directory.CreateDirectory(dir);
|
||||
var tmp = _filePath + ".tmp";
|
||||
await File.WriteAllTextAsync(tmp, json, Encoding.UTF8);
|
||||
File.Move(tmp, _filePath, overwrite: true);
|
||||
}
|
||||
finally { _writeLock.Release(); }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using SharepointToolbox.Web.Core.Models;
|
||||
|
||||
namespace SharepointToolbox.Web.Infrastructure.Persistence;
|
||||
|
||||
public class TemplateRepository
|
||||
{
|
||||
private readonly string _directory;
|
||||
private readonly SemaphoreSlim _lock = new(1, 1);
|
||||
|
||||
public TemplateRepository(string directory) { _directory = directory; }
|
||||
|
||||
public async Task<IReadOnlyList<SiteTemplate>> GetAllAsync()
|
||||
{
|
||||
if (!Directory.Exists(_directory)) return Array.Empty<SiteTemplate>();
|
||||
var files = Directory.GetFiles(_directory, "*.json");
|
||||
var templates = new List<SiteTemplate>();
|
||||
foreach (var file in files)
|
||||
{
|
||||
try
|
||||
{
|
||||
var json = await File.ReadAllTextAsync(file, Encoding.UTF8);
|
||||
var t = JsonSerializer.Deserialize<SiteTemplate>(json,
|
||||
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
||||
if (t is not null) templates.Add(t);
|
||||
}
|
||||
catch { /* skip corrupt files */ }
|
||||
}
|
||||
return templates.OrderByDescending(t => t.CapturedAt).ToList();
|
||||
}
|
||||
|
||||
public async Task<SiteTemplate?> GetByIdAsync(string id)
|
||||
{
|
||||
var file = Path.Combine(_directory, $"{id}.json");
|
||||
if (!File.Exists(file)) return null;
|
||||
try
|
||||
{
|
||||
var json = await File.ReadAllTextAsync(file, Encoding.UTF8);
|
||||
return JsonSerializer.Deserialize<SiteTemplate>(json,
|
||||
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
||||
}
|
||||
catch { return null; }
|
||||
}
|
||||
|
||||
public async Task SaveAsync(SiteTemplate template)
|
||||
{
|
||||
await _lock.WaitAsync();
|
||||
try
|
||||
{
|
||||
Directory.CreateDirectory(_directory);
|
||||
var json = JsonSerializer.Serialize(template, new JsonSerializerOptions
|
||||
{
|
||||
WriteIndented = true, PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
||||
});
|
||||
var path = Path.Combine(_directory, $"{template.Id}.json");
|
||||
var tmp = path + ".tmp";
|
||||
await File.WriteAllTextAsync(tmp, json, Encoding.UTF8);
|
||||
File.Move(tmp, path, overwrite: true);
|
||||
}
|
||||
finally { _lock.Release(); }
|
||||
}
|
||||
|
||||
public Task DeleteAsync(string id)
|
||||
{
|
||||
var file = Path.Combine(_directory, $"{id}.json");
|
||||
if (File.Exists(file)) File.Delete(file);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public async Task RenameAsync(string id, string newName)
|
||||
{
|
||||
var t = await GetByIdAsync(id);
|
||||
if (t is null) return;
|
||||
t.Name = newName;
|
||||
await SaveAsync(t);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using SharepointToolbox.Web.Core.Models;
|
||||
|
||||
namespace SharepointToolbox.Web.Infrastructure.Persistence;
|
||||
|
||||
public class UserRepository
|
||||
{
|
||||
private readonly string _filePath;
|
||||
private readonly SemaphoreSlim _writeLock = new(1, 1);
|
||||
private static readonly JsonSerializerOptions _opts = new()
|
||||
{
|
||||
WriteIndented = true,
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
PropertyNameCaseInsensitive = true
|
||||
};
|
||||
|
||||
public UserRepository(string filePath) { _filePath = filePath; }
|
||||
|
||||
public async Task<IReadOnlyList<AppUser>> LoadAsync()
|
||||
{
|
||||
if (!File.Exists(_filePath)) return Array.Empty<AppUser>();
|
||||
var json = await File.ReadAllTextAsync(_filePath, Encoding.UTF8);
|
||||
var root = JsonSerializer.Deserialize<UsersRoot>(json, _opts);
|
||||
return (IReadOnlyList<AppUser>?)root?.Users ?? Array.Empty<AppUser>();
|
||||
}
|
||||
|
||||
public async Task<AppUser?> FindByEmailAsync(string email)
|
||||
{
|
||||
var users = await LoadAsync();
|
||||
return users.FirstOrDefault(u => u.Email.Equals(email, StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
public async Task SaveAsync(IReadOnlyList<AppUser> users)
|
||||
{
|
||||
await _writeLock.WaitAsync();
|
||||
try
|
||||
{
|
||||
var root = new UsersRoot { Users = users.ToList() };
|
||||
var json = JsonSerializer.Serialize(root, _opts);
|
||||
var tmpPath = _filePath + ".tmp";
|
||||
var dir = Path.GetDirectoryName(_filePath);
|
||||
if (!string.IsNullOrEmpty(dir)) Directory.CreateDirectory(dir);
|
||||
await File.WriteAllTextAsync(tmpPath, json, Encoding.UTF8);
|
||||
File.Move(tmpPath, _filePath, overwrite: true);
|
||||
}
|
||||
finally { _writeLock.Release(); }
|
||||
}
|
||||
|
||||
public async Task UpsertAsync(AppUser user)
|
||||
{
|
||||
await _writeLock.WaitAsync();
|
||||
try
|
||||
{
|
||||
var users = (await LoadInternal()).ToList();
|
||||
var idx = users.FindIndex(u => u.Id == user.Id);
|
||||
if (idx >= 0) users[idx] = user;
|
||||
else users.Add(user);
|
||||
await SaveInternal(users);
|
||||
}
|
||||
finally { _writeLock.Release(); }
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(string userId)
|
||||
{
|
||||
await _writeLock.WaitAsync();
|
||||
try
|
||||
{
|
||||
var users = (await LoadInternal()).ToList();
|
||||
users.RemoveAll(u => u.Id == userId);
|
||||
await SaveInternal(users);
|
||||
}
|
||||
finally { _writeLock.Release(); }
|
||||
}
|
||||
|
||||
private async Task<IReadOnlyList<AppUser>> LoadInternal()
|
||||
{
|
||||
if (!File.Exists(_filePath)) return Array.Empty<AppUser>();
|
||||
var json = await File.ReadAllTextAsync(_filePath, Encoding.UTF8);
|
||||
var root = JsonSerializer.Deserialize<UsersRoot>(json, _opts);
|
||||
return (IReadOnlyList<AppUser>?)root?.Users ?? Array.Empty<AppUser>();
|
||||
}
|
||||
|
||||
private async Task SaveInternal(List<AppUser> users)
|
||||
{
|
||||
var root = new UsersRoot { Users = users };
|
||||
var json = JsonSerializer.Serialize(root, _opts);
|
||||
var tmpPath = _filePath + ".tmp";
|
||||
var dir = Path.GetDirectoryName(_filePath);
|
||||
if (!string.IsNullOrEmpty(dir)) Directory.CreateDirectory(dir);
|
||||
await File.WriteAllTextAsync(tmpPath, json, Encoding.UTF8);
|
||||
File.Move(tmpPath, _filePath, overwrite: true);
|
||||
}
|
||||
|
||||
private sealed class UsersRoot { public List<AppUser> Users { get; set; } = new(); }
|
||||
}
|
||||
Reference in New Issue
Block a user