91 lines
3.0 KiB
C#
91 lines
3.0 KiB
C#
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 1–6 (Sek I)"),
|
||
new(GradingSystem.Points0To15, "Punkte 0–15 (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);
|