using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Reactive; using System.Text.RegularExpressions; using MsBox.Avalonia; using ReactiveUI; namespace Prak12.ViewModels { public class AddTeachersViewModel : ReactiveObject { public AddTeachersViewModel() { AddNewTeacherCommand = ReactiveCommand.Create(AddNewTeacherCommandRun); CancelAddNewTeacherCommand = ReactiveCommand.Create(CancelAddNewTeacherCommandRun); using (AvaloniaPrakt12DbContext DB = new AvaloniaPrakt12DbContext()) { UnselectedSubjects = new ObservableCollection(DB.Subjects.ToList()); } } public event Action AddNewTeacherPageWasClosed; public Teacher Teacher { get => _teacher; set => this.RaiseAndSetIfChanged(ref _teacher, value); } public DateTimeOffset Date { get => _date; set => this.RaiseAndSetIfChanged(ref _date, value); } public int GenderComboboxId { get => _genderComboboxId; set => this.RaiseAndSetIfChanged(ref _genderComboboxId, value); } public string TeacherWorkTime { get => _teacherWorkTime; set => this.RaiseAndSetIfChanged(ref _teacherWorkTime, value); } public int SelectedComboBoxSubjectId { get => _selectedComboBoxSubjectId; set { this.RaiseAndSetIfChanged(ref _selectedComboBoxSubjectId, value); if (value != -1) { _selectedSubjects.Add(UnselectedSubjects[value]); _unselectedSubjects.RemoveAt(value); SelectedComboBoxSubjectId = -1; } } } public int SelectedListBoxSubjectId { get => _selectedListBoxSubjectId; set { this.RaiseAndSetIfChanged(ref _selectedListBoxSubjectId, value); if (value != -1) { _unselectedSubjects.Add(_selectedSubjects[value]); _selectedSubjects.RemoveAt(value); SelectedListBoxSubjectId = -1; } } } public ObservableCollection UnselectedSubjects { get => _unselectedSubjects; set => this.RaiseAndSetIfChanged(ref _unselectedSubjects, value); } public ObservableCollection SelectedSubjects { get => _selectedSubjects; set => this.RaiseAndSetIfChanged(ref _selectedSubjects, value); } public ReactiveCommand AddNewTeacherCommand { get; } public ReactiveCommand CancelAddNewTeacherCommand { get; } private Teacher _teacher = new Teacher() {TeacherSurname = "", TeacherName="", TeacherPatronymic="", TeacherEmail="", TeacherPhoneNumber=""}; private DateTimeOffset _date = DateTimeOffset.Now; private int _genderComboboxId = 0; private string _teacherWorkTime = "0"; private int _selectedComboBoxSubjectId = -1; private int _selectedListBoxSubjectId = -1; private ObservableCollection _unselectedSubjects = new ObservableCollection(); private ObservableCollection _selectedSubjects = new ObservableCollection(); private void AddNewTeacherCommandRun(string param) { if (Teacher.TeacherSurname.Length == 0 || Teacher.TeacherName.Length == 0 || Teacher.TeacherPatronymic.Length == 0) { MessageBoxManager.GetMessageBoxStandard("Внимание!", "ФИО не заполненно!", MsBox.Avalonia.Enums.ButtonEnum.Ok).ShowAsync(); return; } if (Date > DateTimeOffset.Now) { MessageBoxManager.GetMessageBoxStandard("Внимание!", "Дата ещё не наступила!", MsBox.Avalonia.Enums.ButtonEnum.Ok).ShowAsync(); return; } if (!Regex.IsMatch(Teacher.TeacherEmail, "^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$")) { MessageBoxManager.GetMessageBoxStandard("Внимание!", "Email некорректен", MsBox.Avalonia.Enums.ButtonEnum.Ok).ShowAsync(); return; } if (SelectedSubjects.Count == 0) { MessageBoxManager.GetMessageBoxStandard("Внимание!", "Преподаватель не ведёт предметов!", MsBox.Avalonia.Enums.ButtonEnum.Ok).ShowAsync(); return; } int TeacherWorkTimeInt; try { TeacherWorkTimeInt = Convert.ToInt32(TeacherWorkTime); } catch { MessageBoxManager.GetMessageBoxStandard("Внимание!", "Стаж не является числом!", MsBox.Avalonia.Enums.ButtonEnum.Ok).ShowAsync(); TeacherWorkTime = "0"; return; } using (AvaloniaPrakt12DbContext DB = new()) { List list = new(); foreach (var item in SelectedSubjects) { list.Add(new TeachersSubject() { IdSubject = item.IdSubjects }); } DB.Teachers.Add(new Teacher() { TeacherSurname = Teacher.TeacherSurname, TeacherName = Teacher.TeacherName, TeacherEmail = Teacher.TeacherEmail, TeacherPatronymic = Teacher.TeacherPatronymic, TeacherPhoneNumber = Teacher.TeacherPhoneNumber, IdGender = GenderComboboxId + 1, TeacherWorkTime = TeacherWorkTimeInt, TeacherBirthdate = DateOnly.FromDateTime((Date.Date)), TeachersSubjects = list }); DB.SaveChanges(); MessageBoxManager.GetMessageBoxStandard("Успех!", "Преподаватель добавлен в базу!", MsBox.Avalonia.Enums.ButtonEnum.Ok).ShowAsync(); } } private void CancelAddNewTeacherCommandRun(string param) => AddNewTeacherPageWasClosed(); } }