init 1.0.0
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
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");
|
||||
}
|
||||
Reference in New Issue
Block a user