Fix: ArgumentException im Stundenplan bei gleichnamigen Lerngruppen

TimetableSlotDialogViewModel baute die Gruppenauswahl als Dictionary,
geschlüsselt nach LearningGroup.Name. Bei zwei Lerngruppen mit demselben
Namen (dieselbe Klasse in zwei Fächern unterrichtet, z.B. zwei "10c") warf
ToDictionary eine ArgumentException, der Zuweisen-Dialog ließ sich gar
nicht mehr öffnen.

Behoben durch eindeutige Anzeige-Labels statt des rohen Namens: bei einem
Namenskonflikt wird das Fach angehängt ("10c (Chemie)" vs. "10c
(Mathematik)"), mit nummeriertem Fallback für den Restfall gleicher Name
und gleiches Fach.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-17 00:27:14 +02:00
co-authored by Claude Sonnet 5
parent 16ff4875d8
commit 888df39f37
4 changed files with 92 additions and 10 deletions
@@ -10,7 +10,7 @@ namespace LehrerApp.Desktop.ViewModels.Planning;
public partial class TimetableSlotDialogViewModel : ObservableObject
{
private readonly ITimetableSlotRepository _slots;
private readonly Dictionary<string, Guid> _groupIdsByName;
private readonly Dictionary<string, Guid> _groupIdsByLabel;
private readonly TimetableSlot? _editing;
public DayOfWeek Weekday { get; }
@@ -30,7 +30,8 @@ public partial class TimetableSlotDialogViewModel : ObservableObject
public bool Deleted { get; private set; }
public TimetableSlotDialogViewModel(ITimetableSlotRepository slots, IGroupRepository groups,
SchoolYearService schoolYear, DayOfWeek weekday, int periodNumber, TimetableSlot? editing)
ISubjectRepository subjects, SchoolYearService schoolYear, DayOfWeek weekday, int periodNumber,
TimetableSlot? editing)
{
_slots = slots;
_editing = editing;
@@ -45,12 +46,31 @@ public partial class TimetableSlotDialogViewModel : ObservableObject
DialogTitle = $"{WeekdayLabel}, {periodNumber}. Stunde";
var availableGroups = groups.GetBySchoolYear(schoolYear.CurrentSchoolYear()).OrderBy(g => g.Name).ToList();
_groupIdsByName = availableGroups.ToDictionary(g => g.Name, g => g.Id);
GroupOptions = availableGroups.Select(g => g.Name).ToArray();
var subjectNames = subjects.GetAll().ToDictionary(s => s.Id, s => s.Name);
var nameCounts = availableGroups.GroupBy(g => g.Name).ToDictionary(g => g.Key, g => g.Count());
// Der Gruppenname allein ist nicht eindeutig: dieselbe Klasse in mehreren Fächern hat
// mehrere LearningGroup-Datensätze mit demselben Namen. Bei einem Namenskonflikt wird
// deshalb das Fach angehängt ("10c (Chemie)") — ohne diese Absicherung würde die
// Dictionary-Befüllung unten mit einer ArgumentException abstürzen (echter Nutzer-Fehler).
_groupIdsByLabel = new Dictionary<string, Guid>();
var labeledGroups = new List<(string Label, Guid Id)>();
foreach (var g in availableGroups)
{
var label = nameCounts[g.Name] > 1
? $"{g.Name} ({(g.SubjectId is { } sid && subjectNames.TryGetValue(sid, out var sn) ? sn : "ohne Fach")})"
: g.Name;
var uniqueLabel = label;
var suffix = 2;
while (_groupIdsByLabel.ContainsKey(uniqueLabel)) uniqueLabel = $"{label} ({suffix++})";
_groupIdsByLabel[uniqueLabel] = g.Id;
labeledGroups.Add((uniqueLabel, g.Id));
}
GroupOptions = labeledGroups.Select(x => x.Label).ToArray();
if (editing is not null)
{
SelectedGroupName = availableGroups.FirstOrDefault(g => g.Id == editing.GroupId)?.Name ?? "";
SelectedGroupName = labeledGroups.FirstOrDefault(x => x.Id == editing.GroupId).Label ?? "";
Room = editing.Room ?? "";
}
}
@@ -59,7 +79,7 @@ public partial class TimetableSlotDialogViewModel : ObservableObject
private void Save()
{
GroupError = "";
if (string.IsNullOrWhiteSpace(SelectedGroupName) || !_groupIdsByName.TryGetValue(SelectedGroupName, out var groupId))
if (string.IsNullOrWhiteSpace(SelectedGroupName) || !_groupIdsByLabel.TryGetValue(SelectedGroupName, out var groupId))
{
GroupError = "Bitte eine Gruppe auswählen.";
return;
@@ -28,6 +28,7 @@ public partial class TimetableView : UserControl
var vm = new TimetableSlotDialogViewModel(
App.Services.GetRequiredService<ITimetableSlotRepository>(),
App.Services.GetRequiredService<IGroupRepository>(),
App.Services.GetRequiredService<ISubjectRepository>(),
App.Services.GetRequiredService<SchoolYearService>(),
cell.Weekday.Value, cell.PeriodNumber, cell.Slot);