using LiteDB; using LehrerApp.Core.Models; namespace LehrerApp.Data; /// /// Zentrale LiteDB-Verbindung. Singleton – eine Datei = ein Nutzer. /// public class LiteDbContext : IDisposable { private readonly LiteDatabase _db; public LiteDbContext(string databasePath) { _db = new LiteDatabase(new ConnectionString(databasePath) { Connection = ConnectionType.Shared, }); MigrateExistingData(); EnsureIndexes(); } public ILiteCollection Students => _db.GetCollection("students"); public ILiteCollection Groups => _db.GetCollection("groups"); public ILiteCollection Enrollments => _db.GetCollection("enrollments"); public ILiteCollection Exams => _db.GetCollection("exams"); public ILiteCollection ExamResults => _db.GetCollection("exam_results"); public ILiteCollection Grades => _db.GetCollection("grades"); public ILiteCollection GradingKeyTemplates => _db.GetCollection("grading_key_templates"); public ILiteCollection Units => _db.GetCollection("units"); public ILiteCollection Lessons => _db.GetCollection("lessons"); public ILiteCollection Documentation => _db.GetCollection("documentation"); public ILiteCollection Tasks => _db.GetCollection("tasks"); public ILiteCollection TimeEntries => _db.GetCollection("time_entries"); public ILiteCollection ParticipationSessions => _db.GetCollection("participation_sessions"); public ILiteCollection ParticipationEntries => _db.GetCollection("participation"); public ILiteCollection ParticipationAspects => _db.GetCollection("participation_aspects"); public ILiteCollection Subjects => _db.GetCollection("subjects"); public ILiteCollection CompetencyDomains => _db.GetCollection("competency_domains"); public void Checkpoint() => _db.Checkpoint(); private void MigrateExistingData() { var groups = _db.GetCollection("groups"); var missingArchiveState = groups.FindAll() .Where(g => !g.ContainsKey(nameof(LearningGroup.IsActive))) .ToList(); foreach (var group in missingArchiveState) { group[nameof(LearningGroup.IsActive)] = true; groups.Update(group); } } private void EnsureIndexes() { Students.EnsureIndex(x => x.LastName); Students.EnsureIndex(x => x.IsActive); Groups.EnsureIndex(x => x.SchoolYear); Groups.EnsureIndex(x => x.IsActive); Enrollments.EnsureIndex(x => x.StudentId); Enrollments.EnsureIndex(x => x.GroupId); Enrollments.EnsureIndex(x => x.SchoolYear); Exams.EnsureIndex(x => x.GroupId); Exams.EnsureIndex(x => x.Status); ExamResults.EnsureIndex(x => x.ExamId); ExamResults.EnsureIndex(x => x.StudentId); Grades.EnsureIndex(x => x.StudentId); Grades.EnsureIndex(x => x.GroupId); GradingKeyTemplates.EnsureIndex(x => x.GradingSystem); Units.EnsureIndex(x => x.GroupId); Lessons.EnsureIndex(x => x.UnitId); Lessons.EnsureIndex(x => x.GroupId); Lessons.EnsureIndex(x => x.Date); Documentation.EnsureIndex(x => x.StudentId); Tasks.EnsureIndex(x => x.Status); TimeEntries.EnsureIndex(x => x.Date); ParticipationSessions.EnsureIndex(x => x.GroupId); ParticipationSessions.EnsureIndex(x => x.Date); ParticipationEntries.EnsureIndex(x => x.SessionId); ParticipationEntries.EnsureIndex(x => x.StudentId); ParticipationAspects.EnsureIndex(x => x.GroupId); Subjects.EnsureIndex(x => x.Name); CompetencyDomains.EnsureIndex(x => x.SubjectId); CompetencyDomains.EnsureIndex(x => x.GradeLevel); } public void Dispose() => _db.Dispose(); }