kursovaya/AddDiscipline.axaml.cs
2025-06-09 19:11:36 +03:00

85 lines
3.0 KiB
C#
Raw Permalink 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 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<Group> _groups = new List<Group>();
List<FormAttest> _formAttests = new List<FormAttest>();
List<Student> _students = new List<Student>();
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 (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<int>();
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();
}
}