AddTeachersViewModel.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Reactive;
  6. using System.Text.RegularExpressions;
  7. using MsBox.Avalonia;
  8. using ReactiveUI;
  9. namespace Prak12.ViewModels
  10. {
  11. public class AddTeachersViewModel : ReactiveObject
  12. {
  13. public AddTeachersViewModel()
  14. {
  15. AddNewTeacherCommand = ReactiveCommand.Create<string>(AddNewTeacherCommandRun);
  16. CancelAddNewTeacherCommand = ReactiveCommand.Create<string>(CancelAddNewTeacherCommandRun);
  17. using (AvaloniaPrakt12DbContext DB = new AvaloniaPrakt12DbContext())
  18. {
  19. UnselectedSubjects = new ObservableCollection<Subject>(DB.Subjects.ToList());
  20. }
  21. }
  22. public event Action AddNewTeacherPageWasClosed;
  23. public Teacher Teacher
  24. {
  25. get => _teacher;
  26. set => this.RaiseAndSetIfChanged(ref _teacher, value);
  27. }
  28. public DateTimeOffset Date
  29. {
  30. get => _date;
  31. set => this.RaiseAndSetIfChanged(ref _date, value);
  32. }
  33. public int GenderComboboxId
  34. {
  35. get => _genderComboboxId;
  36. set => this.RaiseAndSetIfChanged(ref _genderComboboxId, value);
  37. }
  38. public string TeacherWorkTime
  39. {
  40. get => _teacherWorkTime;
  41. set => this.RaiseAndSetIfChanged(ref _teacherWorkTime, value);
  42. }
  43. public int SelectedComboBoxSubjectId
  44. {
  45. get => _selectedComboBoxSubjectId;
  46. set
  47. {
  48. this.RaiseAndSetIfChanged(ref _selectedComboBoxSubjectId, value);
  49. if (value != -1)
  50. {
  51. _selectedSubjects.Add(UnselectedSubjects[value]);
  52. _unselectedSubjects.RemoveAt(value);
  53. SelectedComboBoxSubjectId = -1;
  54. }
  55. }
  56. }
  57. public int SelectedListBoxSubjectId
  58. {
  59. get => _selectedListBoxSubjectId;
  60. set
  61. {
  62. this.RaiseAndSetIfChanged(ref _selectedListBoxSubjectId, value);
  63. if (value != -1)
  64. {
  65. _unselectedSubjects.Add(_selectedSubjects[value]);
  66. _selectedSubjects.RemoveAt(value);
  67. SelectedListBoxSubjectId = -1;
  68. }
  69. }
  70. }
  71. public ObservableCollection<Subject> UnselectedSubjects
  72. {
  73. get => _unselectedSubjects;
  74. set => this.RaiseAndSetIfChanged(ref _unselectedSubjects, value);
  75. }
  76. public ObservableCollection<Subject> SelectedSubjects
  77. {
  78. get => _selectedSubjects;
  79. set => this.RaiseAndSetIfChanged(ref _selectedSubjects, value);
  80. }
  81. public ReactiveCommand<string, Unit> AddNewTeacherCommand { get; }
  82. public ReactiveCommand<string, Unit> CancelAddNewTeacherCommand { get; }
  83. private Teacher _teacher = new Teacher() {TeacherSurname = "", TeacherName="", TeacherPatronymic="", TeacherEmail="", TeacherPhoneNumber=""};
  84. private DateTimeOffset _date = DateTimeOffset.Now;
  85. private int _genderComboboxId = 0;
  86. private string _teacherWorkTime = "0";
  87. private int _selectedComboBoxSubjectId = -1;
  88. private int _selectedListBoxSubjectId = -1;
  89. private ObservableCollection<Subject> _unselectedSubjects = new ObservableCollection<Subject>();
  90. private ObservableCollection<Subject> _selectedSubjects = new ObservableCollection<Subject>();
  91. private void AddNewTeacherCommandRun(string param)
  92. {
  93. if (Teacher.TeacherSurname.Length == 0 || Teacher.TeacherName.Length == 0 || Teacher.TeacherPatronymic.Length == 0)
  94. {
  95. MessageBoxManager.GetMessageBoxStandard("Âíèìàíèå!", "ÔÈÎ íå çàïîëíåííî!", MsBox.Avalonia.Enums.ButtonEnum.Ok).ShowAsync();
  96. return;
  97. }
  98. if (Date > DateTimeOffset.Now)
  99. {
  100. MessageBoxManager.GetMessageBoxStandard("Âíèìàíèå!", "Äàòà åù¸ íå íàñòóïèëà!", MsBox.Avalonia.Enums.ButtonEnum.Ok).ShowAsync();
  101. return;
  102. }
  103. 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])?$"))
  104. {
  105. MessageBoxManager.GetMessageBoxStandard("Âíèìàíèå!", "Email íåêîððåêòåí", MsBox.Avalonia.Enums.ButtonEnum.Ok).ShowAsync();
  106. return;
  107. }
  108. if (SelectedSubjects.Count == 0)
  109. {
  110. MessageBoxManager.GetMessageBoxStandard("Âíèìàíèå!", "Ïðåïîäàâàòåëü íå âåä¸ò ïðåäìåòîâ!", MsBox.Avalonia.Enums.ButtonEnum.Ok).ShowAsync();
  111. return;
  112. }
  113. int TeacherWorkTimeInt;
  114. try
  115. {
  116. TeacherWorkTimeInt = Convert.ToInt32(TeacherWorkTime);
  117. }
  118. catch
  119. {
  120. MessageBoxManager.GetMessageBoxStandard("Âíèìàíèå!", "Ñòàæ íå ÿâëÿåòñÿ ÷èñëîì!", MsBox.Avalonia.Enums.ButtonEnum.Ok).ShowAsync();
  121. TeacherWorkTime = "0";
  122. return;
  123. }
  124. using (AvaloniaPrakt12DbContext DB = new())
  125. {
  126. List<TeachersSubject> list = new();
  127. foreach (var item in SelectedSubjects)
  128. {
  129. list.Add(new TeachersSubject()
  130. {
  131. IdSubject = item.IdSubjects
  132. });
  133. }
  134. DB.Teachers.Add(new Teacher()
  135. {
  136. TeacherSurname = Teacher.TeacherSurname,
  137. TeacherName = Teacher.TeacherName,
  138. TeacherEmail = Teacher.TeacherEmail,
  139. TeacherPatronymic = Teacher.TeacherPatronymic,
  140. TeacherPhoneNumber = Teacher.TeacherPhoneNumber,
  141. IdGender = GenderComboboxId + 1,
  142. TeacherWorkTime = TeacherWorkTimeInt,
  143. TeacherBirthdate = DateOnly.FromDateTime((Date.Date)),
  144. TeachersSubjects = list
  145. });
  146. DB.SaveChanges();
  147. MessageBoxManager.GetMessageBoxStandard("Óñïåõ!", "Ïðåïîäàâàòåëü äîáàâëåí â áàçó!", MsBox.Avalonia.Enums.ButtonEnum.Ok).ShowAsync();
  148. }
  149. }
  150. private void CancelAddNewTeacherCommandRun(string param) => AddNewTeacherPageWasClosed();
  151. }
  152. }