Initial
This commit is contained in:
117
LehrerApp.Data/LiteDbContext.cs
Normal file
117
LehrerApp.Data/LiteDbContext.cs
Normal file
@@ -0,0 +1,117 @@
|
||||
using LiteDB;
|
||||
using LehrerApp.Core.Models;
|
||||
|
||||
namespace LehrerApp.Data;
|
||||
|
||||
/// <summary>
|
||||
/// Zentrale Datenbankverbindung. Einmalig als Singleton registrieren.
|
||||
/// Eine Instanz = eine .db Datei = ein Nutzer.
|
||||
/// </summary>
|
||||
public class LiteDbContext : IDisposable
|
||||
{
|
||||
private readonly LiteDatabase _db;
|
||||
|
||||
public LiteDbContext(string databasePath)
|
||||
{
|
||||
// WAL-Modus für bessere Performance bei Concurrent Reads
|
||||
var connectionString = new ConnectionString(databasePath)
|
||||
{
|
||||
Connection = ConnectionType.Shared,
|
||||
};
|
||||
|
||||
_db = new LiteDatabase(connectionString);
|
||||
EnsureIndexes();
|
||||
}
|
||||
|
||||
// ── Collections ──────────────────────────────────────────────────────────
|
||||
|
||||
public ILiteCollection<Student> Students =>
|
||||
_db.GetCollection<Student>("students");
|
||||
|
||||
public ILiteCollection<LearningGroup> Groups =>
|
||||
_db.GetCollection<LearningGroup>("groups");
|
||||
|
||||
public ILiteCollection<Enrollment> Enrollments =>
|
||||
_db.GetCollection<Enrollment>("enrollments");
|
||||
|
||||
public ILiteCollection<Exam> Exams =>
|
||||
_db.GetCollection<Exam>("exams");
|
||||
|
||||
public ILiteCollection<ExamResult> ExamResults =>
|
||||
_db.GetCollection<ExamResult>("exam_results");
|
||||
|
||||
public ILiteCollection<Grade> Grades =>
|
||||
_db.GetCollection<Grade>("grades");
|
||||
|
||||
public ILiteCollection<Unit> Units =>
|
||||
_db.GetCollection<Unit>("units");
|
||||
|
||||
public ILiteCollection<Lesson> Lessons =>
|
||||
_db.GetCollection<Lesson>("lessons");
|
||||
|
||||
public ILiteCollection<Documentation> Documentation =>
|
||||
_db.GetCollection<Documentation>("documentation");
|
||||
|
||||
public ILiteCollection<WorkTask> Tasks =>
|
||||
_db.GetCollection<WorkTask>("tasks");
|
||||
|
||||
public ILiteCollection<TimeEntry> TimeEntries =>
|
||||
_db.GetCollection<TimeEntry>("time_entries");
|
||||
|
||||
// ── Transaktionen ─────────────────────────────────────────────────────────
|
||||
|
||||
public T Transaction<T>(Func<T> action) => _db.BeginTrans() ? action() : action();
|
||||
|
||||
public void Checkpoint() => _db.Checkpoint();
|
||||
|
||||
// ── Backup ───────────────────────────────────────────────────────────────
|
||||
|
||||
public void BackupTo(string targetPath)
|
||||
{
|
||||
_db.Checkpoint();
|
||||
File.Copy(_db.UserVersion.ToString(), targetPath, overwrite: true);
|
||||
}
|
||||
|
||||
// ── Indizes ──────────────────────────────────────────────────────────────
|
||||
|
||||
private void EnsureIndexes()
|
||||
{
|
||||
Students.EnsureIndex(x => x.LastName);
|
||||
Students.EnsureIndex(x => x.IsActive);
|
||||
|
||||
Groups.EnsureIndex(x => x.SchoolYear);
|
||||
Groups.EnsureIndex(x => x.Type);
|
||||
|
||||
Enrollments.EnsureIndex(x => x.StudentId);
|
||||
Enrollments.EnsureIndex(x => x.GroupId);
|
||||
Enrollments.EnsureIndex(x => x.SchoolYear);
|
||||
|
||||
Exams.EnsureIndex(x => x.GroupId);
|
||||
Exams.EnsureIndex(x => x.Date);
|
||||
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);
|
||||
|
||||
Units.EnsureIndex(x => x.GroupId);
|
||||
Units.EnsureIndex(x => x.Status);
|
||||
|
||||
Lessons.EnsureIndex(x => x.UnitId);
|
||||
Lessons.EnsureIndex(x => x.GroupId);
|
||||
Lessons.EnsureIndex(x => x.Date);
|
||||
|
||||
Documentation.EnsureIndex(x => x.StudentId);
|
||||
Documentation.EnsureIndex(x => x.Type);
|
||||
|
||||
Tasks.EnsureIndex(x => x.Status);
|
||||
Tasks.EnsureIndex(x => x.DueDate);
|
||||
|
||||
TimeEntries.EnsureIndex(x => x.Date);
|
||||
TimeEntries.EnsureIndex(x => x.TaskId);
|
||||
}
|
||||
|
||||
public void Dispose() => _db.Dispose();
|
||||
}
|
||||
Reference in New Issue
Block a user