92 lines
3.0 KiB
C#
92 lines
3.0 KiB
C#
![]() |
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 AddTeacher : Window
|
|||
|
{
|
|||
|
List<DisciplinePresenter> _disciplinePresenters = new List<DisciplinePresenter>();
|
|||
|
Teacher _teacher = new Teacher();
|
|||
|
List<Teacher> _teachers = new List<Teacher>();
|
|||
|
|
|||
|
public AddTeacher()
|
|||
|
{
|
|||
|
InitializeComponent();
|
|||
|
using var ctx = new DatabaseContext();
|
|||
|
|
|||
|
var disciplineTeachers = ctx.DisciplineTeachers.ToList();
|
|||
|
_disciplinePresenters = ctx.Disciplines.Select(it => new DisciplinePresenter()
|
|||
|
{
|
|||
|
Discipline = it,
|
|||
|
}).ToList();
|
|||
|
|
|||
|
// ListBoxDisciplineGroup.ItemsSource = _disciplineGroups.Select(it => it.NameComboBox);
|
|||
|
ListBoxDisciplineGroup.ItemsSource = _disciplinePresenters;
|
|||
|
}
|
|||
|
|
|||
|
private void ButtonAdd_OnClick(object? sender, RoutedEventArgs e)
|
|||
|
{
|
|||
|
if (ListBoxDisciplineGroup.SelectedItems == null ||
|
|||
|
ListBoxDisciplineGroup.SelectedItems.Count == 0 ||
|
|||
|
string.IsNullOrWhiteSpace(TeacherNameTextBox?.Text))
|
|||
|
{
|
|||
|
ErrorMessage.Text = "Поля не должны быть пустыми!";
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
var ctx = new DatabaseContext();
|
|||
|
|
|||
|
var teacherName = TeacherNameTextBox.Text;
|
|||
|
var selectedItems = ListBoxDisciplineGroup.SelectedItems.Cast<DisciplinePresenter>().ToList();
|
|||
|
int lastUserId = ctx.Users.Max(u => u.Id);
|
|||
|
int lastTeacherId = ctx.Teachers.Max(u => u.Id);
|
|||
|
|
|||
|
var user = new User(){ Id = lastUserId + 1, Login = $"user{lastUserId+1}", Password = "qwerty", Email = $"user{lastUserId+1}@mail.ru", RoleId = 2};
|
|||
|
ctx.Users.Add(user);
|
|||
|
|
|||
|
_teacher = new Teacher(){Id = lastTeacherId+1, Fio = teacherName, Login = $"user{lastUserId+1}"};
|
|||
|
_teachers.Add(_teacher);
|
|||
|
ctx.Teachers.Add(_teacher);
|
|||
|
|
|||
|
foreach (var item in selectedItems)
|
|||
|
{
|
|||
|
var disc_teach = new DisciplineTeacher() { IdDiscipline = item.Discipline.Id, IdTeacher = lastTeacherId+1 };
|
|||
|
ctx.DisciplineTeachers.Add(disc_teach);
|
|||
|
}
|
|||
|
|
|||
|
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(_teachers);
|
|||
|
}
|
|||
|
|
|||
|
public class DisciplinePresenter()
|
|||
|
{
|
|||
|
public Discipline Discipline { get; set; }
|
|||
|
public string DisciplineName
|
|||
|
{
|
|||
|
get => Discipline.Name;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|