123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- 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<string>(AddNewTeacherCommandRun);
- CancelAddNewTeacherCommand = ReactiveCommand.Create<string>(CancelAddNewTeacherCommandRun);
- using (AvaloniaPrakt12DbContext DB = new AvaloniaPrakt12DbContext())
- {
- UnselectedSubjects = new ObservableCollection<Subject>(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<Subject> UnselectedSubjects
- {
- get => _unselectedSubjects;
- set => this.RaiseAndSetIfChanged(ref _unselectedSubjects, value);
- }
- public ObservableCollection<Subject> SelectedSubjects
- {
- get => _selectedSubjects;
- set => this.RaiseAndSetIfChanged(ref _selectedSubjects, value);
- }
- public ReactiveCommand<string, Unit> AddNewTeacherCommand { get; }
- public ReactiveCommand<string, Unit> 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<Subject> _unselectedSubjects = new ObservableCollection<Subject>();
- private ObservableCollection<Subject> _selectedSubjects = new ObservableCollection<Subject>();
- 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<TeachersSubject> 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();
- }
- }
|