OnChange-Hook auf den 19 einfachen Repositories (keine Kaskaden/Batches) ergaenzt: SeatingPlan, GroupMembership, GradingKeyTemplate, Grade, GradingScheme, ReportGrade, Unit, Lesson, WorkTask, TimeEntry, ParticipationAspect, ParticipationSection, Subject, ShorthandCode, AlternativeLessonPath, TimetableSlot, SchoolHoliday, SupervisionDuty, SubstitutionEntry. Mechanisch, ein Aufruf nach dem bestehenden Upsert/Delete. Tabellengetriebener Test statt 19 fast identischer Testdateien. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
712 lines
32 KiB
C#
712 lines
32 KiB
C#
using LehrerApp.Core.Interfaces;
|
|
using LehrerApp.Core.Models;
|
|
|
|
namespace LehrerApp.Data.Repositories;
|
|
|
|
internal static class ArchivedGroupWriteGuard
|
|
{
|
|
public const string Message =
|
|
"Diese Lerngruppe ist archiviert. Bitte reaktiviere sie, bevor du Änderungen vornimmst.";
|
|
|
|
public static void EnsureActive(LiteDbContext db, Guid groupId)
|
|
{
|
|
if (db.Groups.FindById(groupId) is { IsActive: false })
|
|
throw new InvalidOperationException(Message);
|
|
}
|
|
}
|
|
|
|
public class StudentRepository(LiteDbContext db) : IStudentRepository
|
|
{
|
|
public Student? GetById(Guid id) => db.Students.FindById(id);
|
|
public List<Student> GetAll(bool includeInactive = false) =>
|
|
(includeInactive ? db.Students.FindAll() : db.Students.Find(s => s.IsActive))
|
|
.OrderBy(s => s.LastName).ToList();
|
|
public List<Student> GetByGroup(Guid groupId)
|
|
{
|
|
var ids = db.Memberships
|
|
.Find(e => e.GroupId == groupId)
|
|
.Select(e => e.StudentId).ToHashSet();
|
|
return db.Students.Find(s => ids.Contains(s.Id)).OrderBy(s => s.LastName).ToList();
|
|
}
|
|
public StudentReferenceSummary GetReferenceSummary(Guid studentId) => new(
|
|
db.Memberships.Count(m => m.StudentId == studentId),
|
|
db.ExamResults.Count(r => r.StudentId == studentId),
|
|
db.Grades.Count(g => g.StudentId == studentId),
|
|
db.ReportGrades.Count(g => g.StudentId == studentId),
|
|
db.ParticipationEntries.Count(e => e.StudentId == studentId),
|
|
db.Documentation.Count(d => d.StudentId == studentId));
|
|
public void Save(Student s)
|
|
{
|
|
s.UpdatedAt = DateTime.UtcNow;
|
|
db.Students.Upsert(s);
|
|
db.OnChange?.Invoke(nameof(Student), s.Id.ToString(), "Save", s);
|
|
}
|
|
public void Delete(Guid id)
|
|
{
|
|
var references = GetReferenceSummary(id);
|
|
if (references.HasReferences)
|
|
throw new InvalidOperationException(
|
|
"Der Schüler besitzt verknüpfte Daten und kann nur deaktiviert werden.");
|
|
db.Students.Delete(id);
|
|
db.OnChange?.Invoke(nameof(Student), id.ToString(), "Delete", null);
|
|
}
|
|
}
|
|
|
|
public class GroupRepository(LiteDbContext db) : IGroupRepository
|
|
{
|
|
public LearningGroup? GetById(Guid id) => db.Groups.FindById(id);
|
|
public List<LearningGroup> GetAll(bool includeInactive = false) =>
|
|
(includeInactive ? db.Groups.FindAll() : db.Groups.Find(g => g.IsActive))
|
|
.OrderBy(g => g.SchoolYear).ThenBy(g => g.Name).ToList();
|
|
public List<LearningGroup> GetBySchoolYear(string schoolYear, bool includeInactive = false) =>
|
|
(includeInactive
|
|
? db.Groups.Find(g => g.SchoolYear == schoolYear)
|
|
: db.Groups.Find(g => g.SchoolYear == schoolYear && g.IsActive))
|
|
.OrderBy(g => g.Name).ToList();
|
|
public void Save(LearningGroup g)
|
|
{
|
|
var existing = db.Groups.FindById(g.Id);
|
|
if (existing is { IsActive: false } && !g.IsActive)
|
|
throw new InvalidOperationException(ArchivedGroupWriteGuard.Message);
|
|
if (existing is { IsActive: false } && g.IsActive
|
|
&& (existing.Name != g.Name || existing.Type != g.Type || existing.SubjectId != g.SubjectId
|
|
|| existing.SchoolYear != g.SchoolYear || existing.GradeLevel != g.GradeLevel
|
|
|| existing.GradingSystem != g.GradingSystem || existing.HoursPerWeek != g.HoursPerWeek
|
|
|| existing.IsOwnClass != g.IsOwnClass || existing.IsDifferentiated != g.IsDifferentiated))
|
|
throw new InvalidOperationException(
|
|
"Bitte reaktiviere die Lerngruppe zuerst und nimm die Änderungen anschließend vor.");
|
|
if (g.SubjectId is Guid subjectId && db.Subjects.FindById(subjectId) is null)
|
|
throw new InvalidOperationException("Das gewählte Fach existiert nicht mehr.");
|
|
g.UpdatedAt = DateTime.UtcNow;
|
|
db.Groups.Upsert(g);
|
|
}
|
|
public void Delete(Guid id)
|
|
{
|
|
ArchivedGroupWriteGuard.EnsureActive(db, id);
|
|
db.CascadeDeleteGroup(id);
|
|
}
|
|
}
|
|
|
|
public class SeatingPlanRepository(LiteDbContext db) : ISeatingPlanRepository
|
|
{
|
|
public SeatingPlan? GetById(Guid id) => db.SeatingPlans.FindById(id);
|
|
|
|
public List<SeatingPlan> GetByGroup(Guid groupId) =>
|
|
db.SeatingPlans.Find(p => p.GroupId == groupId)
|
|
.OrderBy(p => p.Name).ThenBy(p => p.Room).ToList();
|
|
|
|
public void Save(SeatingPlan plan)
|
|
{
|
|
ArchivedGroupWriteGuard.EnsureActive(db, plan.GroupId);
|
|
// LiteDB can deserialize missing/legacy optional string fields as null even
|
|
// though the current model initializes them with an empty string.
|
|
plan.Name = plan.Name?.Trim() ?? "";
|
|
plan.Room = plan.Room?.Trim() ?? "";
|
|
plan.Assignments ??= [];
|
|
if (plan.Name.Length == 0)
|
|
throw new ArgumentException("Der Name des Sitzplans darf nicht leer sein.");
|
|
if (plan.Rows is < 1 or > 10 || plan.Columns is < 1 or > 10)
|
|
throw new ArgumentOutOfRangeException(nameof(plan), "Ein Sitzplan muss zwischen 1 und 10 Reihen und Spalten haben.");
|
|
if (db.Groups.FindById(plan.GroupId) is null)
|
|
throw new InvalidOperationException("Die zugehörige Lerngruppe existiert nicht.");
|
|
|
|
var duplicateName = db.SeatingPlans.Find(p => p.GroupId == plan.GroupId)
|
|
.FirstOrDefault(p => p.Id != plan.Id
|
|
&& string.Equals(p.Name, plan.Name, StringComparison.OrdinalIgnoreCase)
|
|
&& string.Equals(p.Room, plan.Room, StringComparison.OrdinalIgnoreCase));
|
|
if (duplicateName is not null)
|
|
throw new InvalidOperationException("Für diese Lerngruppe existiert bereits ein gleichnamiger Sitzplan in diesem Raum.");
|
|
|
|
plan.Assignments = plan.Assignments
|
|
.Where(a => a.Row >= 0 && a.Row < plan.Rows && a.Column >= 0 && a.Column < plan.Columns)
|
|
.ToList();
|
|
if (plan.Assignments.GroupBy(a => (a.Row, a.Column)).Any(g => g.Count() > 1))
|
|
throw new InvalidOperationException("Ein Sitzplatz darf nur einmal belegt werden.");
|
|
if (plan.Assignments.GroupBy(a => a.StudentId).Any(g => g.Count() > 1))
|
|
throw new InvalidOperationException("Ein Schüler darf in einem Sitzplan nur einmal vorkommen.");
|
|
|
|
var memberIds = db.Memberships.Find(m => m.GroupId == plan.GroupId)
|
|
.Select(m => m.StudentId).ToHashSet();
|
|
if (plan.Assignments.Any(a => !memberIds.Contains(a.StudentId)))
|
|
throw new InvalidOperationException("Der Sitzplan enthält einen Schüler, der nicht zur Lerngruppe gehört.");
|
|
|
|
plan.UpdatedAt = DateTime.UtcNow;
|
|
db.SeatingPlans.Upsert(plan);
|
|
db.OnChange?.Invoke(nameof(SeatingPlan), plan.Id.ToString(), "Save", plan);
|
|
}
|
|
|
|
public void Delete(Guid id)
|
|
{
|
|
if (db.SeatingPlans.FindById(id) is { } plan)
|
|
ArchivedGroupWriteGuard.EnsureActive(db, plan.GroupId);
|
|
db.SeatingPlans.Delete(id);
|
|
db.OnChange?.Invoke(nameof(SeatingPlan), id.ToString(), "Delete", null);
|
|
}
|
|
}
|
|
|
|
public class GroupMembershipRepository(LiteDbContext db) : IGroupMembershipRepository
|
|
{
|
|
public List<GroupMembership> GetByStudent(Guid id) =>
|
|
db.Memberships.Find(e => e.StudentId == id).ToList();
|
|
public List<GroupMembership> GetByGroup(Guid id) =>
|
|
db.Memberships.Find(e => e.GroupId == id).ToList();
|
|
public GroupMembership? GetByStudentAndGroup(Guid studentId, Guid groupId) =>
|
|
db.Memberships.FindOne(e => e.StudentId == studentId && e.GroupId == groupId);
|
|
public void Save(GroupMembership membership)
|
|
{
|
|
ArchivedGroupWriteGuard.EnsureActive(db, membership.GroupId);
|
|
var existing = GetByStudentAndGroup(membership.StudentId, membership.GroupId);
|
|
if (existing is not null && existing.Id != membership.Id)
|
|
throw new InvalidOperationException("Der Schüler ist dieser Lerngruppe bereits zugeordnet.");
|
|
db.Memberships.Upsert(membership);
|
|
db.OnChange?.Invoke(nameof(GroupMembership), membership.Id.ToString(), "Save", membership);
|
|
}
|
|
public void Delete(Guid id)
|
|
{
|
|
if (db.Memberships.FindById(id) is { } membership)
|
|
ArchivedGroupWriteGuard.EnsureActive(db, membership.GroupId);
|
|
db.Memberships.Delete(id);
|
|
db.OnChange?.Invoke(nameof(GroupMembership), id.ToString(), "Delete", null);
|
|
}
|
|
}
|
|
|
|
public class ExamRepository(LiteDbContext db) : IExamRepository
|
|
{
|
|
public Exam? GetById(Guid id) => db.Exams.FindById(id);
|
|
public List<Exam> GetAll() => db.Exams.FindAll().OrderBy(e => e.Date).ToList();
|
|
public List<Exam> GetByGroup(Guid groupId) =>
|
|
db.Exams.Find(e => e.GroupId == groupId).OrderByDescending(e => e.Date).ToList();
|
|
public void Save(Exam e)
|
|
{
|
|
ArchivedGroupWriteGuard.EnsureActive(db, e.GroupId);
|
|
e.UpdatedAt = DateTime.UtcNow;
|
|
db.Exams.Upsert(e);
|
|
}
|
|
public void Delete(Guid id)
|
|
{
|
|
if (db.Exams.FindById(id) is { } exam)
|
|
ArchivedGroupWriteGuard.EnsureActive(db, exam.GroupId);
|
|
foreach (var result in db.ExamResults.Find(r => r.ExamId == id).ToList())
|
|
db.ExamResults.Delete(result.Id);
|
|
db.Exams.Delete(id);
|
|
}
|
|
}
|
|
|
|
public class ExamResultRepository(LiteDbContext db) : IExamResultRepository
|
|
{
|
|
public List<ExamResult> GetByExam(Guid id) =>
|
|
db.ExamResults.Find(r => r.ExamId == id).ToList();
|
|
public List<ExamResult> GetByStudent(Guid id) =>
|
|
db.ExamResults.Find(r => r.StudentId == id).ToList();
|
|
public ExamResult? GetByExamAndStudent(Guid examId, Guid studentId) =>
|
|
db.ExamResults.FindOne(r => r.ExamId == examId && r.StudentId == studentId);
|
|
public void Save(ExamResult r)
|
|
{
|
|
if (db.Exams.FindById(r.ExamId) is { } exam)
|
|
ArchivedGroupWriteGuard.EnsureActive(db, exam.GroupId);
|
|
r.UpdatedAt = DateTime.UtcNow;
|
|
db.ExamResults.Upsert(r);
|
|
}
|
|
public void SaveMany(List<ExamResult> results)
|
|
{
|
|
foreach (var examId in results.Select(r => r.ExamId).Distinct())
|
|
if (db.Exams.FindById(examId) is { } exam)
|
|
ArchivedGroupWriteGuard.EnsureActive(db, exam.GroupId);
|
|
var now = DateTime.UtcNow;
|
|
foreach (var r in results) r.UpdatedAt = now;
|
|
db.ExamResults.Upsert(results);
|
|
}
|
|
}
|
|
|
|
public class GradingKeyTemplateRepository(LiteDbContext db) : IGradingKeyTemplateRepository
|
|
{
|
|
public List<GradingKeyTemplate> GetAll() =>
|
|
db.GradingKeyTemplates.FindAll().OrderBy(t => t.Name).ToList();
|
|
public List<GradingKeyTemplate> GetByGradingSystem(GradingSystem system) =>
|
|
db.GradingKeyTemplates.Find(t => t.GradingSystem == system).OrderBy(t => t.Name).ToList();
|
|
public GradingKeyTemplate? GetById(Guid id) => db.GradingKeyTemplates.FindById(id);
|
|
public void Save(GradingKeyTemplate t)
|
|
{
|
|
t.UpdatedAt = DateTime.UtcNow;
|
|
db.GradingKeyTemplates.Upsert(t);
|
|
db.OnChange?.Invoke(nameof(GradingKeyTemplate), t.Id.ToString(), "Save", t);
|
|
}
|
|
public void Delete(Guid id)
|
|
{
|
|
db.GradingKeyTemplates.Delete(id);
|
|
db.OnChange?.Invoke(nameof(GradingKeyTemplate), id.ToString(), "Delete", null);
|
|
}
|
|
}
|
|
|
|
public class GradeRepository(LiteDbContext db) : IGradeRepository
|
|
{
|
|
public List<Grade> GetByStudentAndGroup(Guid sid, Guid gid) =>
|
|
db.Grades.Find(g => g.StudentId == sid && g.GroupId == gid).OrderBy(g => g.Date).ToList();
|
|
public List<Grade> GetByGroup(Guid id) =>
|
|
db.Grades.Find(g => g.GroupId == id).OrderBy(g => g.Date).ToList();
|
|
public void Save(Grade g)
|
|
{
|
|
ArchivedGroupWriteGuard.EnsureActive(db, g.GroupId);
|
|
db.Grades.Upsert(g);
|
|
db.OnChange?.Invoke(nameof(Grade), g.Id.ToString(), "Save", g);
|
|
}
|
|
public void Delete(Guid id)
|
|
{
|
|
if (db.Grades.FindById(id) is { } grade)
|
|
ArchivedGroupWriteGuard.EnsureActive(db, grade.GroupId);
|
|
db.Grades.Delete(id);
|
|
db.OnChange?.Invoke(nameof(Grade), id.ToString(), "Delete", null);
|
|
}
|
|
}
|
|
|
|
public class GradingSchemeRepository(LiteDbContext db) : IGradingSchemeRepository
|
|
{
|
|
public GradingScheme? GetByGroup(Guid groupId) => db.GradingSchemes.FindOne(s => s.GroupId == groupId);
|
|
public GradingScheme? GetDefaultForType(GroupType type) =>
|
|
db.GradingSchemes.FindOne(s => s.GroupId == null && s.GroupType == type);
|
|
public void Save(GradingScheme s)
|
|
{
|
|
if (s.GroupId is Guid groupId) ArchivedGroupWriteGuard.EnsureActive(db, groupId);
|
|
s.UpdatedAt = DateTime.UtcNow;
|
|
db.GradingSchemes.Upsert(s);
|
|
db.OnChange?.Invoke(nameof(GradingScheme), s.Id.ToString(), "Save", s);
|
|
}
|
|
public void Delete(Guid id)
|
|
{
|
|
if (db.GradingSchemes.FindById(id)?.GroupId is Guid groupId)
|
|
ArchivedGroupWriteGuard.EnsureActive(db, groupId);
|
|
db.GradingSchemes.Delete(id);
|
|
db.OnChange?.Invoke(nameof(GradingScheme), id.ToString(), "Delete", null);
|
|
}
|
|
}
|
|
|
|
public class ReportGradeRepository(LiteDbContext db) : IReportGradeRepository
|
|
{
|
|
public List<ReportGrade> GetByGroup(Guid groupId) => db.ReportGrades.Find(r => r.GroupId == groupId).ToList();
|
|
public ReportGrade? GetByStudentGroupPeriod(Guid studentId, Guid groupId, string period) =>
|
|
db.ReportGrades.FindOne(r => r.StudentId == studentId && r.GroupId == groupId && r.Period == period);
|
|
public void Save(ReportGrade r)
|
|
{
|
|
ArchivedGroupWriteGuard.EnsureActive(db, r.GroupId);
|
|
r.UpdatedAt = DateTime.UtcNow;
|
|
db.ReportGrades.Upsert(r);
|
|
db.OnChange?.Invoke(nameof(ReportGrade), r.Id.ToString(), "Save", r);
|
|
}
|
|
public void Delete(Guid id)
|
|
{
|
|
if (db.ReportGrades.FindById(id) is { } grade)
|
|
ArchivedGroupWriteGuard.EnsureActive(db, grade.GroupId);
|
|
db.ReportGrades.Delete(id);
|
|
db.OnChange?.Invoke(nameof(ReportGrade), id.ToString(), "Delete", null);
|
|
}
|
|
}
|
|
|
|
public class UnitRepository(LiteDbContext db) : IUnitRepository
|
|
{
|
|
public Unit? GetById(Guid id) => db.Units.FindById(id);
|
|
public List<Unit> GetByGroup(Guid id) =>
|
|
db.Units.Find(u => u.GroupId == id).OrderBy(u => u.StartDate).ToList();
|
|
public void Save(Unit u)
|
|
{
|
|
ArchivedGroupWriteGuard.EnsureActive(db, u.GroupId);
|
|
u.UpdatedAt = DateTime.UtcNow;
|
|
db.Units.Upsert(u);
|
|
db.OnChange?.Invoke(nameof(Unit), u.Id.ToString(), "Save", u);
|
|
}
|
|
public void Delete(Guid id)
|
|
{
|
|
if (db.Units.FindById(id) is { } unit)
|
|
ArchivedGroupWriteGuard.EnsureActive(db, unit.GroupId);
|
|
db.Units.Delete(id);
|
|
db.OnChange?.Invoke(nameof(Unit), id.ToString(), "Delete", null);
|
|
}
|
|
}
|
|
|
|
public class LessonRepository(LiteDbContext db) : ILessonRepository
|
|
{
|
|
public List<Lesson> GetByUnit(Guid id) =>
|
|
db.Lessons.Find(l => l.UnitId == id).OrderBy(l => l.Date).ThenBy(l => l.LessonNumber).ToList();
|
|
public List<Lesson> GetByGroupAndDate(Guid gid, DateOnly date) =>
|
|
db.Lessons.Find(l => l.GroupId == gid && l.Date == date).ToList();
|
|
public List<Lesson> GetByGroupAndRange(Guid gid, DateOnly from, DateOnly to) =>
|
|
db.Lessons.Find(l => l.GroupId == gid && l.Date >= from && l.Date <= to)
|
|
.OrderBy(l => l.Date).ToList();
|
|
public void Save(Lesson l)
|
|
{
|
|
ArchivedGroupWriteGuard.EnsureActive(db, l.GroupId);
|
|
l.UpdatedAt = DateTime.UtcNow;
|
|
db.Lessons.Upsert(l);
|
|
db.OnChange?.Invoke(nameof(Lesson), l.Id.ToString(), "Save", l);
|
|
}
|
|
public void Delete(Guid id)
|
|
{
|
|
if (db.Lessons.FindById(id) is { } lesson)
|
|
ArchivedGroupWriteGuard.EnsureActive(db, lesson.GroupId);
|
|
db.Lessons.Delete(id);
|
|
db.OnChange?.Invoke(nameof(Lesson), id.ToString(), "Delete", null);
|
|
}
|
|
}
|
|
|
|
public class DocumentationRepository(LiteDbContext db) : IDocumentationRepository
|
|
{
|
|
public List<Documentation> GetByStudent(Guid id) =>
|
|
db.Documentation.Find(d => d.StudentId == id && !d.IsDeleted).OrderByDescending(d => d.Date).ToList();
|
|
public List<Documentation> GetByStudentAndType(Guid sid, DocumentationType type) =>
|
|
db.Documentation.Find(d => d.StudentId == sid && d.Type == type && !d.IsDeleted)
|
|
.OrderByDescending(d => d.Date).ToList();
|
|
public List<Documentation> GetAll() =>
|
|
db.Documentation.Find(d => !d.IsDeleted).OrderByDescending(d => d.Date).ToList();
|
|
public void Save(Documentation d) { d.UpdatedAt = DateTime.UtcNow; db.Documentation.Upsert(d); }
|
|
public void Delete(Guid id)
|
|
{
|
|
var doc = db.Documentation.FindById(id);
|
|
if (doc is null) return;
|
|
doc.IsDeleted = true;
|
|
doc.DeletedAt = DateTime.UtcNow;
|
|
db.Documentation.Update(doc);
|
|
}
|
|
public void HardDelete(Guid id)
|
|
{
|
|
var doc = db.Documentation.FindById(id);
|
|
if (doc is not null)
|
|
foreach (var attachment in doc.Attachments) db.Attachments.Delete(attachment.StorageId);
|
|
db.Documentation.Delete(id);
|
|
}
|
|
}
|
|
|
|
public class WorkTaskRepository(LiteDbContext db) : IWorkTaskRepository
|
|
{
|
|
public List<WorkTask> GetByStatus(WorkTaskStatus s) =>
|
|
db.Tasks.Find(t => t.Status == s).OrderBy(t => t.DueDate).ToList();
|
|
public List<WorkTask> GetAll() =>
|
|
db.Tasks.FindAll().OrderBy(t => t.Status).ThenBy(t => t.DueDate).ToList();
|
|
public void Save(WorkTask t)
|
|
{
|
|
t.UpdatedAt = DateTime.UtcNow;
|
|
db.Tasks.Upsert(t);
|
|
db.OnChange?.Invoke(nameof(WorkTask), t.Id.ToString(), "Save", t);
|
|
}
|
|
public void Delete(Guid id)
|
|
{
|
|
db.Tasks.Delete(id);
|
|
db.OnChange?.Invoke(nameof(WorkTask), id.ToString(), "Delete", null);
|
|
}
|
|
}
|
|
|
|
public class TimeEntryRepository(LiteDbContext db) : ITimeEntryRepository
|
|
{
|
|
public List<TimeEntry> GetByDate(DateOnly date) =>
|
|
db.TimeEntries.Find(e => e.Date == date).OrderBy(e => e.StartTime).ToList();
|
|
public List<TimeEntry> GetByDateRange(DateOnly from, DateOnly to) =>
|
|
db.TimeEntries.Find(e => e.Date >= from && e.Date <= to).OrderBy(e => e.Date).ToList();
|
|
public List<TimeEntry> GetByTask(Guid id) =>
|
|
db.TimeEntries.Find(e => e.TaskId == id).ToList();
|
|
public void Save(TimeEntry e)
|
|
{
|
|
db.TimeEntries.Upsert(e);
|
|
db.OnChange?.Invoke(nameof(TimeEntry), e.Id.ToString(), "Save", e);
|
|
}
|
|
public void Delete(Guid id)
|
|
{
|
|
db.TimeEntries.Delete(id);
|
|
db.OnChange?.Invoke(nameof(TimeEntry), id.ToString(), "Delete", null);
|
|
}
|
|
}
|
|
|
|
public class ParticipationSessionRepository(LiteDbContext db) : IParticipationSessionRepository
|
|
{
|
|
public List<ParticipationSession> GetByGroup(Guid groupId) =>
|
|
db.ParticipationSessions.Find(s => s.GroupId == groupId).OrderByDescending(s => s.Date).ToList();
|
|
public ParticipationSession? GetById(Guid id) => db.ParticipationSessions.FindById(id);
|
|
public void Save(ParticipationSession s)
|
|
{
|
|
ArchivedGroupWriteGuard.EnsureActive(db, s.GroupId);
|
|
s.UpdatedAt = DateTime.UtcNow;
|
|
db.ParticipationSessions.Upsert(s);
|
|
}
|
|
public void Delete(Guid id)
|
|
{
|
|
if (db.ParticipationSessions.FindById(id) is { } session)
|
|
ArchivedGroupWriteGuard.EnsureActive(db, session.GroupId);
|
|
db.ParticipationSessions.Delete(id);
|
|
foreach (var e in db.ParticipationEntries.Find(e => e.SessionId == id).ToList())
|
|
db.ParticipationEntries.Delete(e.Id);
|
|
}
|
|
}
|
|
|
|
public class ParticipationRepository(LiteDbContext db) : IParticipationRepository
|
|
{
|
|
public List<ParticipationEntry> GetBySession(Guid sessionId) =>
|
|
db.ParticipationEntries.Find(e => e.SessionId == sessionId).ToList();
|
|
public List<ParticipationEntry> GetByStudent(Guid studentId) =>
|
|
db.ParticipationEntries.Find(e => e.StudentId == studentId).ToList();
|
|
public ParticipationEntry? GetBySessionAndStudent(Guid sessionId, Guid studentId) =>
|
|
db.ParticipationEntries.FindOne(e => e.SessionId == sessionId && e.StudentId == studentId);
|
|
public void Save(ParticipationEntry e)
|
|
{
|
|
if (db.ParticipationSessions.FindById(e.SessionId) is { } session)
|
|
ArchivedGroupWriteGuard.EnsureActive(db, session.GroupId);
|
|
e.UpdatedAt = DateTime.UtcNow;
|
|
db.ParticipationEntries.Upsert(e);
|
|
}
|
|
public void SaveMany(List<ParticipationEntry> entries)
|
|
{
|
|
foreach (var sessionId in entries.Select(e => e.SessionId).Distinct())
|
|
if (db.ParticipationSessions.FindById(sessionId) is { } session)
|
|
ArchivedGroupWriteGuard.EnsureActive(db, session.GroupId);
|
|
var now = DateTime.UtcNow;
|
|
foreach (var e in entries) e.UpdatedAt = now;
|
|
db.ParticipationEntries.Upsert(entries);
|
|
}
|
|
public void DeleteBySession(Guid sessionId)
|
|
{
|
|
if (db.ParticipationSessions.FindById(sessionId) is { } session)
|
|
ArchivedGroupWriteGuard.EnsureActive(db, session.GroupId);
|
|
foreach (var e in db.ParticipationEntries.Find(e => e.SessionId == sessionId).ToList())
|
|
db.ParticipationEntries.Delete(e.Id);
|
|
}
|
|
}
|
|
|
|
public class ParticipationAspectRepository(LiteDbContext db) : IParticipationAspectRepository
|
|
{
|
|
public List<ParticipationAspect> GetDefaults() =>
|
|
db.ParticipationAspects.Find(a => a.GroupId == null && a.IsActive).OrderBy(a => a.SortOrder).ToList();
|
|
public List<ParticipationAspect> GetByGroup(Guid groupId) =>
|
|
db.ParticipationAspects.Find(a => a.GroupId == groupId && a.IsActive).OrderBy(a => a.SortOrder).ToList();
|
|
public List<ParticipationAspect> GetAllByGroup(Guid groupId) =>
|
|
db.ParticipationAspects.Find(a => a.GroupId == groupId).OrderBy(a => a.SortOrder).ToList();
|
|
public void Save(ParticipationAspect a)
|
|
{
|
|
if (a.GroupId is Guid groupId) ArchivedGroupWriteGuard.EnsureActive(db, groupId);
|
|
a.Key = a.Key.Trim();
|
|
a.Label = a.Label.Trim();
|
|
if (a.Key.Length == 0) throw new ArgumentException("Der Schlüssel darf nicht leer sein.");
|
|
if (a.Label.Length == 0) throw new ArgumentException("Die Bezeichnung darf nicht leer sein.");
|
|
// Ein Schlüssel muss innerhalb dessen, was für eine Gruppe tatsächlich gilt, eindeutig
|
|
// sein — das sind die globalen Standardaspekte UND die gruppenspezifischen zusammen
|
|
// (siehe ParticipationTabViewModel.LoadAspects, das beide konkateniert). Sonst entstünde
|
|
// in der Bewertungsübersicht eine mehrdeutige Spalte mit identischem Schlüssel.
|
|
var relevant = db.ParticipationAspects.Find(x => x.GroupId == null || x.GroupId == a.GroupId);
|
|
var duplicate = relevant.FirstOrDefault(x =>
|
|
x.Id != a.Id && string.Equals(x.Key, a.Key, StringComparison.OrdinalIgnoreCase));
|
|
if (duplicate is not null)
|
|
throw new InvalidOperationException("Ein Aspekt mit diesem Schlüssel existiert für diese Gruppe bereits.");
|
|
a.UpdatedAt = DateTime.UtcNow;
|
|
db.ParticipationAspects.Upsert(a);
|
|
db.OnChange?.Invoke(nameof(ParticipationAspect), a.Id.ToString(), "Save", a);
|
|
}
|
|
public void Delete(Guid id)
|
|
{
|
|
if (db.ParticipationAspects.FindById(id)?.GroupId is Guid groupId)
|
|
ArchivedGroupWriteGuard.EnsureActive(db, groupId);
|
|
db.ParticipationAspects.Delete(id);
|
|
db.OnChange?.Invoke(nameof(ParticipationAspect), id.ToString(), "Delete", null);
|
|
}
|
|
}
|
|
|
|
public class ParticipationSectionRepository(LiteDbContext db) : IParticipationSectionRepository
|
|
{
|
|
public List<ParticipationSection> GetByGroup(Guid groupId) =>
|
|
db.ParticipationSections.Find(s => s.GroupId == groupId).OrderBy(s => s.StartDate).ToList();
|
|
public void Save(ParticipationSection s)
|
|
{
|
|
ArchivedGroupWriteGuard.EnsureActive(db, s.GroupId);
|
|
db.ParticipationSections.Upsert(s);
|
|
db.OnChange?.Invoke(nameof(ParticipationSection), s.Id.ToString(), "Save", s);
|
|
}
|
|
public void Delete(Guid id)
|
|
{
|
|
if (db.ParticipationSections.FindById(id) is { } section)
|
|
ArchivedGroupWriteGuard.EnsureActive(db, section.GroupId);
|
|
db.ParticipationSections.Delete(id);
|
|
db.OnChange?.Invoke(nameof(ParticipationSection), id.ToString(), "Delete", null);
|
|
}
|
|
}
|
|
|
|
public class SubjectRepository(LiteDbContext db) : ISubjectRepository
|
|
{
|
|
public List<Subject> GetAll() => db.Subjects.FindAll().OrderBy(s => s.Name).ToList();
|
|
public Subject? GetById(Guid id) => db.Subjects.FindById(id);
|
|
public Subject? GetByName(string name) => db.Subjects.FindAll().FirstOrDefault(s =>
|
|
string.Equals(s.Name.Trim(), name.Trim(), StringComparison.OrdinalIgnoreCase));
|
|
public void Save(Subject s)
|
|
{
|
|
s.Name = s.Name.Trim();
|
|
s.ShortName = s.ShortName.Trim();
|
|
if (s.Name.Length == 0) throw new ArgumentException("Der Fachname darf nicht leer sein.");
|
|
var duplicate = GetByName(s.Name);
|
|
if (duplicate is not null && duplicate.Id != s.Id)
|
|
throw new InvalidOperationException("Ein Fach mit diesem Namen existiert bereits.");
|
|
s.UpdatedAt = DateTime.UtcNow;
|
|
db.Subjects.Upsert(s);
|
|
db.OnChange?.Invoke(nameof(Subject), s.Id.ToString(), "Save", s);
|
|
}
|
|
public void Delete(Guid id)
|
|
{
|
|
if (db.Groups.Exists(g => g.SubjectId == id) || db.CompetencyDomains.Exists(d => d.SubjectId == id))
|
|
throw new InvalidOperationException("Das Fach wird noch von einer Lerngruppe oder einem Kompetenzkatalog verwendet.");
|
|
db.Subjects.Delete(id);
|
|
db.OnChange?.Invoke(nameof(Subject), id.ToString(), "Delete", null);
|
|
}
|
|
}
|
|
|
|
public class ShorthandCodeRepository(LiteDbContext db) : IShorthandCodeRepository
|
|
{
|
|
public List<ShorthandCode> GetAll() => db.ShorthandCodes.FindAll().OrderBy(c => c.Code).ToList();
|
|
public void Save(ShorthandCode c)
|
|
{
|
|
c.Code = c.Code.Trim();
|
|
c.Label = c.Label.Trim();
|
|
if (c.Code.Length == 0) throw new ArgumentException("Das Kürzel darf nicht leer sein.");
|
|
var duplicate = db.ShorthandCodes.FindAll()
|
|
.FirstOrDefault(x => string.Equals(x.Code, c.Code, StringComparison.OrdinalIgnoreCase));
|
|
if (duplicate is not null && duplicate.Id != c.Id)
|
|
throw new InvalidOperationException("Ein Kürzel mit diesem Code existiert bereits.");
|
|
c.UpdatedAt = DateTime.UtcNow;
|
|
db.ShorthandCodes.Upsert(c);
|
|
db.OnChange?.Invoke(nameof(ShorthandCode), c.Id.ToString(), "Save", c);
|
|
}
|
|
public void Delete(Guid id)
|
|
{
|
|
db.ShorthandCodes.Delete(id);
|
|
db.OnChange?.Invoke(nameof(ShorthandCode), id.ToString(), "Delete", null);
|
|
}
|
|
}
|
|
|
|
public class AlternativeLessonPathRepository(LiteDbContext db) : IAlternativeLessonPathRepository
|
|
{
|
|
public List<AlternativeLessonPath> GetAll() => db.AlternativeLessonPaths.FindAll().OrderBy(p => p.Name).ToList();
|
|
public AlternativeLessonPath? GetById(Guid id) => db.AlternativeLessonPaths.FindById(id);
|
|
public void Save(AlternativeLessonPath p)
|
|
{
|
|
p.Name = p.Name.Trim();
|
|
p.Description = string.IsNullOrWhiteSpace(p.Description) ? null : p.Description.Trim();
|
|
if (p.Name.Length == 0) throw new ArgumentException("Der Name darf nicht leer sein.");
|
|
var duplicate = db.AlternativeLessonPaths.FindAll()
|
|
.FirstOrDefault(x => string.Equals(x.Name, p.Name, StringComparison.OrdinalIgnoreCase));
|
|
if (duplicate is not null && duplicate.Id != p.Id)
|
|
throw new InvalidOperationException("Ein alternativer Ablauf mit diesem Namen existiert bereits.");
|
|
p.UpdatedAt = DateTime.UtcNow;
|
|
db.AlternativeLessonPaths.Upsert(p);
|
|
db.OnChange?.Invoke(nameof(AlternativeLessonPath), p.Id.ToString(), "Save", p);
|
|
}
|
|
public void Delete(Guid id)
|
|
{
|
|
db.AlternativeLessonPaths.Delete(id);
|
|
db.OnChange?.Invoke(nameof(AlternativeLessonPath), id.ToString(), "Delete", null);
|
|
}
|
|
}
|
|
|
|
public class TimetableSlotRepository(LiteDbContext db) : ITimetableSlotRepository
|
|
{
|
|
public List<TimetableSlot> GetAll() =>
|
|
db.TimetableSlots.FindAll().OrderBy(s => s.Weekday).ThenBy(s => s.PeriodNumber).ToList();
|
|
public List<TimetableSlot> GetByGroup(Guid groupId) =>
|
|
db.TimetableSlots.Find(s => s.GroupId == groupId).OrderBy(s => s.Weekday).ThenBy(s => s.PeriodNumber).ToList();
|
|
public void Save(TimetableSlot slot)
|
|
{
|
|
ArchivedGroupWriteGuard.EnsureActive(db, slot.GroupId);
|
|
var occupied = db.TimetableSlots.FindAll()
|
|
.FirstOrDefault(s => s.Weekday == slot.Weekday && s.PeriodNumber == slot.PeriodNumber);
|
|
if (occupied is not null && occupied.Id != slot.Id)
|
|
throw new InvalidOperationException("Diese Stunde ist bereits belegt.");
|
|
db.TimetableSlots.Upsert(slot);
|
|
db.OnChange?.Invoke(nameof(TimetableSlot), slot.Id.ToString(), "Save", slot);
|
|
}
|
|
public void Delete(Guid id)
|
|
{
|
|
if (db.TimetableSlots.FindById(id) is { } slot)
|
|
ArchivedGroupWriteGuard.EnsureActive(db, slot.GroupId);
|
|
db.TimetableSlots.Delete(id);
|
|
db.OnChange?.Invoke(nameof(TimetableSlot), id.ToString(), "Delete", null);
|
|
}
|
|
}
|
|
|
|
public class SchoolHolidayRepository(LiteDbContext db) : ISchoolHolidayRepository
|
|
{
|
|
public List<SchoolHoliday> GetAll() => db.SchoolHolidays.FindAll().OrderBy(h => h.StartDate).ToList();
|
|
public void Save(SchoolHoliday holiday)
|
|
{
|
|
db.SchoolHolidays.Upsert(holiday);
|
|
db.OnChange?.Invoke(nameof(SchoolHoliday), holiday.Id.ToString(), "Save", holiday);
|
|
}
|
|
public void Delete(Guid id)
|
|
{
|
|
db.SchoolHolidays.Delete(id);
|
|
db.OnChange?.Invoke(nameof(SchoolHoliday), id.ToString(), "Delete", null);
|
|
}
|
|
}
|
|
|
|
public class SupervisionDutyRepository(LiteDbContext db) : ISupervisionDutyRepository
|
|
{
|
|
public List<SupervisionDuty> GetAll() =>
|
|
db.SupervisionDuties.FindAll().OrderBy(d => d.Weekday).ThenBy(d => d.AfterPeriod).ToList();
|
|
public void Save(SupervisionDuty duty)
|
|
{
|
|
var occupied = db.SupervisionDuties.FindAll()
|
|
.FirstOrDefault(d => d.Weekday == duty.Weekday && d.AfterPeriod == duty.AfterPeriod);
|
|
if (occupied is not null && occupied.Id != duty.Id)
|
|
throw new InvalidOperationException("Für diese Pause ist bereits eine Aufsicht eingetragen.");
|
|
db.SupervisionDuties.Upsert(duty);
|
|
db.OnChange?.Invoke(nameof(SupervisionDuty), duty.Id.ToString(), "Save", duty);
|
|
}
|
|
public void Delete(Guid id)
|
|
{
|
|
db.SupervisionDuties.Delete(id);
|
|
db.OnChange?.Invoke(nameof(SupervisionDuty), id.ToString(), "Delete", null);
|
|
}
|
|
}
|
|
|
|
public class SubstitutionEntryRepository(LiteDbContext db) : ISubstitutionEntryRepository
|
|
{
|
|
public List<SubstitutionEntry> GetAll() => db.SubstitutionEntries.FindAll().OrderBy(e => e.Date).ToList();
|
|
public List<SubstitutionEntry> GetByDate(DateOnly date) =>
|
|
db.SubstitutionEntries.Find(e => e.Date == date).ToList();
|
|
public void Save(SubstitutionEntry entry)
|
|
{
|
|
db.SubstitutionEntries.Upsert(entry);
|
|
db.OnChange?.Invoke(nameof(SubstitutionEntry), entry.Id.ToString(), "Save", entry);
|
|
}
|
|
public void Delete(Guid id)
|
|
{
|
|
db.SubstitutionEntries.Delete(id);
|
|
db.OnChange?.Invoke(nameof(SubstitutionEntry), id.ToString(), "Delete", null);
|
|
}
|
|
}
|
|
|
|
public class CompetencyDomainRepository(LiteDbContext db) : ICompetencyDomainRepository
|
|
{
|
|
public List<CompetencyDomain> GetBySubjectAndGrade(Guid subjectId, int gradeLevel) =>
|
|
db.CompetencyDomains
|
|
.Find(d => d.SubjectId == subjectId && d.GradeLevel == gradeLevel)
|
|
.OrderBy(d => d.SortOrder)
|
|
.ToList();
|
|
public CompetencyDomain? GetById(Guid id) => db.CompetencyDomains.FindById(id);
|
|
public void Save(CompetencyDomain d) { d.UpdatedAt = DateTime.UtcNow; db.CompetencyDomains.Upsert(d); }
|
|
public void Delete(Guid id) => db.CompetencyDomains.Delete(id);
|
|
public void DeleteBySubjectAndGrade(Guid subjectId, int gradeLevel)
|
|
{
|
|
foreach (var d in db.CompetencyDomains
|
|
.Find(d => d.SubjectId == subjectId && d.GradeLevel == gradeLevel)
|
|
.ToList())
|
|
db.CompetencyDomains.Delete(d.Id);
|
|
}
|
|
public void ReplaceForSubjectAndGrade(Guid subjectId, int gradeLevel, List<CompetencyDomain> domains)
|
|
{
|
|
db.ExecuteInTransaction(() =>
|
|
{
|
|
foreach (var domain in db.CompetencyDomains
|
|
.Find(d => d.SubjectId == subjectId && d.GradeLevel == gradeLevel).ToList())
|
|
db.CompetencyDomains.Delete(domain.Id);
|
|
var now = DateTime.UtcNow;
|
|
foreach (var domain in domains)
|
|
{
|
|
domain.SubjectId = subjectId;
|
|
domain.GradeLevel = gradeLevel;
|
|
domain.UpdatedAt = now;
|
|
db.CompetencyDomains.Upsert(domain);
|
|
}
|
|
});
|
|
}
|
|
}
|