2025-06-09 20:05:33 +00:00
|
|
|
|
using System;
|
2025-06-09 15:55:31 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Avalonia;
|
|
|
|
|
using Avalonia.Controls;
|
|
|
|
|
using Avalonia.Interactivity;
|
|
|
|
|
using Avalonia.Markup.Xaml;
|
|
|
|
|
using kursovaya.Models;
|
2025-06-09 20:05:33 +00:00
|
|
|
|
using kursovaya.ModelsLocal;
|
2025-06-09 15:55:31 +00:00
|
|
|
|
|
|
|
|
|
namespace kursovaya;
|
|
|
|
|
|
|
|
|
|
public partial class AddTeacher : Window
|
|
|
|
|
{
|
|
|
|
|
List<DisciplinePresenter> _disciplinePresenters = new List<DisciplinePresenter>();
|
|
|
|
|
Teacher _teacher = new Teacher();
|
|
|
|
|
List<Teacher> _teachers = new List<Teacher>();
|
|
|
|
|
|
2025-06-09 20:05:33 +00:00
|
|
|
|
List<DisciplineGroupTeacher> _disciplineGroupTeachers = new List<DisciplineGroupTeacher>();
|
2025-06-09 15:55:31 +00:00
|
|
|
|
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 = _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)
|
|
|
|
|
{
|
2025-06-09 20:05:33 +00:00
|
|
|
|
var studentsId = ctx.Attestations.Where(it => it.IdDiscipline == item.Discipline.Id).Select(it => it.IdUser).ToList();
|
|
|
|
|
foreach (var id in studentsId)
|
|
|
|
|
{
|
|
|
|
|
var groupId = ctx.Students
|
|
|
|
|
.Where(s => s.Id == id)
|
|
|
|
|
.Select(s => s.IdGroup)
|
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
var group = ctx.Groups.FirstOrDefault(g => g.Id == groupId);
|
|
|
|
|
|
|
|
|
|
bool alreadyExists = _disciplineGroupTeachers.Any(dgt =>
|
|
|
|
|
dgt.Group.Id == group.Id &&
|
|
|
|
|
dgt.Discipline.Id == item.Discipline.Id &&
|
|
|
|
|
dgt.Teacher.Id == _teacher.Id);
|
|
|
|
|
|
|
|
|
|
if (!alreadyExists)
|
|
|
|
|
{
|
|
|
|
|
_disciplineGroupTeachers.Add(new DisciplineGroupTeacher()
|
|
|
|
|
{
|
|
|
|
|
Group = group,
|
|
|
|
|
Teacher = _teacher,
|
|
|
|
|
Discipline = item.Discipline
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-09 15:55:31 +00:00
|
|
|
|
var disc_teach = new DisciplineTeacher() { IdDiscipline = item.Discipline.Id, IdTeacher = lastTeacherId+1 };
|
|
|
|
|
ctx.DisciplineTeachers.Add(disc_teach);
|
|
|
|
|
}
|
2025-06-09 20:05:33 +00:00
|
|
|
|
|
|
|
|
|
foreach (var item in _disciplineGroupTeachers)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Discipline: {item.Discipline.Name} Teacher: {item.Teacher.Fio} Group: {item.Group.Id}");
|
|
|
|
|
}
|
2025-06-09 15:55:31 +00:00
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2025-06-09 20:05:33 +00:00
|
|
|
|
Result res = new Result(){Teachers = _teachers, DisciplineGroupTeachers = _disciplineGroupTeachers};
|
|
|
|
|
|
|
|
|
|
Close(res);
|
2025-06-09 15:55:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class DisciplinePresenter()
|
|
|
|
|
{
|
|
|
|
|
public Discipline Discipline { get; set; }
|
|
|
|
|
public string DisciplineName
|
|
|
|
|
{
|
|
|
|
|
get => Discipline.Name;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|