using System.Collections.Generic; using System.Linq; using Avalonia; using Avalonia.Controls; using Avalonia.Interactivity; using Avalonia.Markup.Xaml; using kursovaya.Models; namespace kursovaya; public partial class AddDiscipline : Window { List _groups = new List(); List _formAttests = new List(); List _students = new List(); Discipline _discipline = new Discipline(); Attestation _attestation = new Attestation(); CalculateGrade calculateGrade = new CalculateGrade(); public AddDiscipline() { InitializeComponent(); using var ctx = new DatabaseContext(); _groups = ctx.Groups.ToList(); _formAttests = ctx.FormAttests.ToList(); _students = ctx.Students.ToList(); ListBoxGroups.ItemsSource = _groups.Select(it => it.Id); FormAttestComboBox.ItemsSource = _formAttests.Select(g => g.Name).ToList(); } private void ButtonAdd_OnClick(object? sender, RoutedEventArgs e) { // if (string.IsNullOrWhiteSpace(ListBoxGroups.SelectedItem.ToString()) || string.IsNullOrWhiteSpace(DisciplineNameTextBox.Text) || string.IsNullOrWhiteSpace(FormAttestComboBox.SelectedItem.ToString())) // { // ErrorMessage.Text = "Поля не должны быть пустыми!"; // return; // } if (FormAttestComboBox.SelectedItem == null || ListBoxGroups.SelectedItems == null || ListBoxGroups.SelectedItems.Count == 0 || string.IsNullOrWhiteSpace(DisciplineNameTextBox?.Text)) { ErrorMessage.Text = "Поля не должны быть пустыми!"; return; } var ctx = new DatabaseContext(); var disciplineName = DisciplineNameTextBox.Text; var selectedGroupIds = ListBoxGroups.SelectedItems.Cast(); var selectedFormAttestId = ctx.FormAttests.FirstOrDefault(f => f.Name == FormAttestComboBox.SelectedItem.ToString()).Id; int lastDisciplineId = ctx.Disciplines.Max(u => u.Id); _discipline = new Discipline(){ Id = lastDisciplineId + 1, Name = disciplineName, IdFormAttest = selectedFormAttestId }; ctx.Disciplines.Add(_discipline); foreach (var groupId in selectedGroupIds) { _students = ctx.Students.Where(s => s.IdGroup == groupId).ToList(); foreach (var student in _students) { _attestation = new Attestation(){ IdUser = student.Id, IdDiscipline = lastDisciplineId+1, AttestFirst = 0, AttestSecond = 0, AttestThird = 0, Total = 0, Grade = calculateGrade.CalculateGradeMethod(0, selectedFormAttestId)}; ctx.Attestations.Add(_attestation); } } var changes = ctx.SaveChanges(); if (changes > 0) { SuccessMessage.Text = "дисциплина успешно добавлена!"; if (ErrorMessage != null) { ErrorMessage.Text = ""; } } else { ErrorMessage.Text = "Не удалось добавить дисциплину."; SuccessMessage.Text = ""; } } private void ButtonBack_OnClick(object? sender, RoutedEventArgs e) { Close(); } }