using System.Text.Json; using NozCli.Core.Models; using NozCli.Core.Serialization; namespace NozCli.Repl; public static class NotesCache { private static string CachePath => Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".cache", "noz", "notes.json"); public static NotesCacheFile? TryLoad() { try { if (!File.Exists(CachePath)) return null; var json = File.ReadAllText(CachePath); return JsonSerializer.Deserialize(json, NozJsonContext.Default.NotesCacheFile); } catch { return null; } } public static async Task SaveAsync(List notes) { try { var dir = Path.GetDirectoryName(CachePath)!; Directory.CreateDirectory(dir); var cache = new NotesCacheFile(DateTimeOffset.UtcNow, notes); var json = JsonSerializer.Serialize(cache, NozJsonContext.Default.NotesCacheFile); // Atomic write: tmp → rename var tmp = CachePath + ".tmp"; await File.WriteAllTextAsync(tmp, json); File.Move(tmp, CachePath, overwrite: true); } catch { /* cache write failures are non-fatal */ } } public static string TimeAgo(DateTimeOffset dt) { var diff = DateTimeOffset.UtcNow - dt; return diff.TotalSeconds switch { < 60 => "gerade eben", < 3600 => $"vor {(int)diff.TotalMinutes} Min.", < 86400 => $"vor {(int)diff.TotalHours} Std.", _ => $"vor {(int)diff.TotalDays} Tagen", }; } }