Files
LehrerApp/LehrerApp.Desktop/ViewModels/Groups/AddGroupDialogViewModel.cs
2026-03-29 23:47:31 +02:00

91 lines
3.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using LehrerApp.Core.Interfaces;
using LehrerApp.Core.Models;
using LehrerApp.Core.Services;
namespace LehrerApp.Desktop.ViewModels.Groups;
public partial class AddGroupDialogViewModel : ObservableObject
{
private readonly IGroupRepository _groups;
private readonly SchoolYearService _schoolYearService;
[ObservableProperty] private string _name = "";
[ObservableProperty] private string _subject = "";
[ObservableProperty] private GroupType _type = GroupType.Course;
[ObservableProperty] private int _gradeLevel = 10;
[ObservableProperty] private GradingSystem _gradingSystem = GradingSystem.Grades1To6;
[ObservableProperty] private int? _hoursPerWeek;
[ObservableProperty] private string _selectedSchoolYear = "";
[ObservableProperty] private string _validationMessage = "";
public List<string> SchoolYears { get; }
public List<GroupTypeItem> GroupTypes { get; } =
[
new(GroupType.Course, "Fachkurs"),
new(GroupType.Class, "Klassenverband"),
];
public List<GradingSystemItem> GradingSystems { get; } =
[
new(GradingSystem.Grades1To6, "Noten 16 (Sek I)"),
new(GradingSystem.Points0To15, "Punkte 015 (Oberstufe)"),
];
// Wird vom Dialog aufgerufen wenn OK geklickt wurde
public LearningGroup? Result { get; private set; }
public AddGroupDialogViewModel(
IGroupRepository groups,
SchoolYearService schoolYearService)
{
_groups = groups;
_schoolYearService = schoolYearService;
SchoolYears = schoolYearService.RecentSchoolYears(3);
SelectedSchoolYear = schoolYearService.CurrentSchoolYear();
// Automatisch Notensystem vorschlagen wenn Klassenstufe sich ändert
PropertyChanged += (_, e) =>
{
if (e.PropertyName == nameof(GradeLevel))
GradingSystem = GradeLevel >= 11
? GradingSystem.Points0To15
: GradingSystem.Grades1To6;
};
}
[RelayCommand]
private bool Save()
{
// Validierung
if (string.IsNullOrWhiteSpace(Name))
{
ValidationMessage = "Bitte einen Namen eingeben.";
return false;
}
if (GradeLevel is < 1 or > 13)
{
ValidationMessage = "Klassenstufe muss zwischen 1 und 13 liegen.";
return false;
}
Result = new LearningGroup
{
Name = Name.Trim(),
Subject = string.IsNullOrWhiteSpace(Subject) ? null : Subject.Trim(),
Type = Type,
GradeLevel = GradeLevel,
GradingSystem = GradingSystem,
SchoolYear = SelectedSchoolYear,
HoursPerWeek = HoursPerWeek,
};
_groups.Save(Result);
ValidationMessage = "";
return true;
}
}
public record GroupTypeItem(GroupType Value, string Label);
public record GradingSystemItem(GradingSystem Value, string Label);