Baustein 2: Outbound-Hook-Infrastruktur (Kapitel 10)
LiteDbContext.OnChange-Hook (Sync-agnostisch, kein Verweis auf LehrerApp.Sync aus LehrerApp.Data), damit Repositories lokale Schreibvorgaenge signalisieren koennen, ohne dass Data von Sync abhaengt. StudentRepository als Vorlage verdrahtet. GroupRepository.Delete-Kaskade (14 betroffene Collections) nach LiteDbContext.CascadeDeleteGroup extrahiert - reine Verschiebung, kein Verhaltensunterschied, macht sie aber von einem spaeter eingehenden Sync-Ereignis (Baustein 5) wiederverwendbar, ohne ueber Repository-Save/Delete (und damit erneut ueber OnChange) zu laufen. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -3,6 +3,10 @@ using LehrerApp.Core.Models;
|
||||
|
||||
namespace LehrerApp.Data;
|
||||
|
||||
/// Wird nach jedem Save/Delete einer Entität aufgerufen; payload ist die gespeicherte Entität
|
||||
/// bzw. null bei Delete. Sync-agnostisch – siehe <see cref="LiteDbContext.OnChange"/>.
|
||||
public delegate void ChangeHandler(string entityType, string entityId, string operation, object? payload);
|
||||
|
||||
/// <summary>
|
||||
/// Zentrale LiteDB-Verbindung. Singleton – eine Datei = ein Nutzer.
|
||||
/// </summary>
|
||||
@@ -67,6 +71,91 @@ public class LiteDbContext : IDisposable
|
||||
|
||||
public int SchemaVersion => ReadSchemaVersion();
|
||||
|
||||
/// Wird von den Repositories nach jedem Save/Delete aufgerufen (payload = die gespeicherte
|
||||
/// Entität bzw. null bei Delete). Bewusst hier statt in LehrerApp.Sync definiert, damit Data
|
||||
/// weiterhin ohne Verweis auf Sync auskommt — die eigentliche Sync-Anbindung setzt diesen
|
||||
/// Hook von außen (siehe AppBootstrapper).
|
||||
public ChangeHandler? OnChange { get; set; }
|
||||
|
||||
/// Führt dieselbe Kaskade wie <c>GroupRepository.Delete</c> aus. Hier auf dem Context statt
|
||||
/// im Repository, damit ein später eingehendes Sync-Ereignis (Baustein 5) dieselbe Kaskade
|
||||
/// nachvollziehen kann, ohne über die Repository-Save/Delete-Methoden (und damit erneut über
|
||||
/// <see cref="OnChange"/>) zu laufen.
|
||||
internal void CascadeDeleteGroup(Guid id)
|
||||
{
|
||||
ExecuteInTransaction(() =>
|
||||
{
|
||||
foreach (var membership in Memberships.Find(e => e.GroupId == id).ToList())
|
||||
Memberships.Delete(membership.Id);
|
||||
|
||||
foreach (var exam in Exams.Find(e => e.GroupId == id).ToList())
|
||||
{
|
||||
foreach (var result in ExamResults.Find(r => r.ExamId == exam.Id).ToList())
|
||||
ExamResults.Delete(result.Id);
|
||||
Exams.Delete(exam.Id);
|
||||
}
|
||||
|
||||
foreach (var grade in Grades.Find(g => g.GroupId == id).ToList())
|
||||
Grades.Delete(grade.Id);
|
||||
|
||||
foreach (var reportGrade in ReportGrades.Find(g => g.GroupId == id).ToList())
|
||||
ReportGrades.Delete(reportGrade.Id);
|
||||
|
||||
foreach (var scheme in GradingSchemes.Find(s => s.GroupId == id).ToList())
|
||||
GradingSchemes.Delete(scheme.Id);
|
||||
|
||||
foreach (var unit in Units.Find(u => u.GroupId == id).ToList())
|
||||
{
|
||||
foreach (var lesson in Lessons.Find(l => l.UnitId == unit.Id).ToList())
|
||||
Lessons.Delete(lesson.Id);
|
||||
Units.Delete(unit.Id);
|
||||
}
|
||||
|
||||
foreach (var lesson in Lessons.Find(l => l.GroupId == id).ToList())
|
||||
Lessons.Delete(lesson.Id);
|
||||
|
||||
foreach (var session in ParticipationSessions.Find(s => s.GroupId == id).ToList())
|
||||
{
|
||||
foreach (var entry in ParticipationEntries.Find(e => e.SessionId == session.Id).ToList())
|
||||
ParticipationEntries.Delete(entry.Id);
|
||||
ParticipationSessions.Delete(session.Id);
|
||||
}
|
||||
|
||||
foreach (var aspect in ParticipationAspects.Find(a => a.GroupId == id).ToList())
|
||||
ParticipationAspects.Delete(aspect.Id);
|
||||
|
||||
foreach (var section in ParticipationSections.Find(s => s.GroupId == id).ToList())
|
||||
ParticipationSections.Delete(section.Id);
|
||||
|
||||
foreach (var plan in SeatingPlans.Find(p => p.GroupId == id).ToList())
|
||||
SeatingPlans.Delete(plan.Id);
|
||||
|
||||
// Dokumentation und Arbeitszeit sind historische Nachweise. Sie bleiben erhalten,
|
||||
// werden aber von der nicht mehr existierenden Lerngruppe entkoppelt.
|
||||
foreach (var documentation in Documentation.Find(d => d.GroupId == id).ToList())
|
||||
{
|
||||
documentation.GroupId = null;
|
||||
documentation.UpdatedAt = DateTime.UtcNow;
|
||||
Documentation.Update(documentation);
|
||||
}
|
||||
|
||||
foreach (var task in Tasks.Find(t => t.GroupId == id).ToList())
|
||||
{
|
||||
task.GroupId = null;
|
||||
task.UpdatedAt = DateTime.UtcNow;
|
||||
Tasks.Update(task);
|
||||
}
|
||||
|
||||
foreach (var timeEntry in TimeEntries.Find(t => t.GroupId == id).ToList())
|
||||
{
|
||||
timeEntry.GroupId = null;
|
||||
TimeEntries.Update(timeEntry);
|
||||
}
|
||||
|
||||
Groups.Delete(id);
|
||||
});
|
||||
}
|
||||
|
||||
internal void ExecuteInTransaction(Action action)
|
||||
{
|
||||
_db.BeginTrans();
|
||||
|
||||
Reference in New Issue
Block a user