Files
LehrerApp/LehrerApp.Api/ReadableSnapshotStore.cs
2026-06-19 00:42:00 +02:00

24 lines
913 B
C#

using System.Text.Json;
using LehrerApp.Core.Models;
namespace LehrerApp.Api;
public class ReadableSnapshotStore(string dataPath)
{
private readonly string _path = Path.Combine(dataPath, "readable");
private static readonly JsonSerializerOptions _opts = new() { WriteIndented = false, PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
public void Store(string userId, ReadableSnapshot snap)
{
Directory.CreateDirectory(_path);
File.WriteAllText(FilePath(userId), JsonSerializer.Serialize(snap, _opts));
}
public ReadableSnapshot? Load(string userId)
{
var p = FilePath(userId);
return File.Exists(p) ? JsonSerializer.Deserialize<ReadableSnapshot>(File.ReadAllText(p), _opts) : null;
}
private string FilePath(string userId) =>
Path.Combine(_path, $"{string.Concat(userId.Where(c => char.IsLetterOrDigit(c) || c == '-'))}.json");
}