127 lines
4.3 KiB
C#
127 lines
4.3 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using Avalonia;
|
||
using Avalonia.Controls;
|
||
using Avalonia.Interactivity;
|
||
using Avalonia.Markup.Xaml;
|
||
using kursovaya.Models;
|
||
using kursovaya.ModelsLocal;
|
||
|
||
namespace kursovaya;
|
||
|
||
public partial class AddTeacher : Window
|
||
{
|
||
List<DisciplinePresenter> _disciplinePresenters = new List<DisciplinePresenter>();
|
||
Teacher _teacher = new Teacher();
|
||
List<Teacher> _teachers = new List<Teacher>();
|
||
|
||
List<DisciplineGroupTeacher> _disciplineGroupTeachers = new List<DisciplineGroupTeacher>();
|
||
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)
|
||
{
|
||
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
|
||
});
|
||
}
|
||
}
|
||
|
||
var disc_teach = new DisciplineTeacher() { IdDiscipline = item.Discipline.Id, IdTeacher = lastTeacherId+1 };
|
||
ctx.DisciplineTeachers.Add(disc_teach);
|
||
}
|
||
|
||
foreach (var item in _disciplineGroupTeachers)
|
||
{
|
||
Console.WriteLine($"Discipline: {item.Discipline.Name} Teacher: {item.Teacher.Fio} Group: {item.Group.Id}");
|
||
}
|
||
|
||
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)
|
||
{
|
||
Result res = new Result(){Teachers = _teachers, DisciplineGroupTeachers = _disciplineGroupTeachers};
|
||
|
||
Close(res);
|
||
}
|
||
|
||
public class DisciplinePresenter()
|
||
{
|
||
public Discipline Discipline { get; set; }
|
||
public string DisciplineName
|
||
{
|
||
get => Discipline.Name;
|
||
}
|
||
}
|
||
} |