using LehrerApp.Core.Models; using LiteDB; namespace LehrerApp.Api; /// Unverschlüsselte Serverdaten, die der Server zur Abfrage standortbezogener öffentlicher /// Wetterdaten benötigt. Bewusst getrennt vom clientseitig verschlüsselten EventStore. public sealed class SchoolWeatherStore : IDisposable { private readonly LiteDatabase _db; public SchoolWeatherStore(string dataPath) { Directory.CreateDirectory(dataPath); _db = new LiteDatabase(Path.Combine(dataPath, "school-weather.db")); } private ILiteCollection Locations { get { var col = _db.GetCollection("locations"); col.EnsureIndex(x => x.UserId, unique: true); return col; } } private ILiteCollection Weather { get { var col = _db.GetCollection("weather"); col.EnsureIndex(x => x.UserId, unique: true); return col; } } public SchoolLocationProfile? GetLocation(string userId) => Locations.FindOne(x => x.UserId == userId)?.Profile; public void SaveLocation(string userId, SchoolLocationProfile profile) { var existing = Locations.FindOne(x => x.UserId == userId); Locations.Upsert(new LocationEntry { Id = existing?.Id ?? ObjectId.NewObjectId(), UserId = userId, Profile = profile, }); // Ein geänderter Standort darf niemals den Wetterstand des alten Orts liefern. Weather.DeleteMany(x => x.UserId == userId); } public WeatherSnapshot? GetWeather(string userId) => Weather.FindOne(x => x.UserId == userId)?.Snapshot; public void SaveWeather(string userId, WeatherSnapshot snapshot) { var existing = Weather.FindOne(x => x.UserId == userId); Weather.Upsert(new WeatherEntry { Id = existing?.Id ?? ObjectId.NewObjectId(), UserId = userId, Snapshot = snapshot, }); } public void Dispose() => _db.Dispose(); private sealed class LocationEntry { public ObjectId Id { get; set; } = ObjectId.NewObjectId(); public string UserId { get; set; } = ""; public SchoolLocationProfile Profile { get; set; } = new(); } private sealed class WeatherEntry { public ObjectId Id { get; set; } = ObjectId.NewObjectId(); public string UserId { get; set; } = ""; public WeatherSnapshot Snapshot { get; set; } = new(); } }