AddTeacherViewModel.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Data.Common;
  5. using System.Linq;
  6. using MsBox.Avalonia;
  7. using ReactiveUI;
  8. using scool.Models;
  9. namespace scool.ViewModels
  10. {
  11. public class AddTeacherViewModel : ViewModelBase
  12. {
  13. public static _43pErmolinContext DB = new _43pErmolinContext();
  14. private string _name;
  15. private string _surname;
  16. private string _patronymic;
  17. private string _mail;
  18. private string _phone;
  19. private DateTimeOffset _birthday = new DateTimeOffset(DateTime.Now);
  20. private Subject _selectSubject;
  21. private string _exp;
  22. private bool _genderM = true;
  23. private bool _enableBTN = false;
  24. private ObservableCollection<Subject> _collSubSelect = new ObservableCollection<Subject>();
  25. public string TName { get => _name; set => this.RaiseAndSetIfChanged(ref _name, value); }
  26. public string TSurname { get => _surname; set => this.RaiseAndSetIfChanged(ref _surname, value); }
  27. public string TPatronymic { get => _patronymic; set => this.RaiseAndSetIfChanged(ref _patronymic, value); }
  28. public DateTimeOffset TBirthday { get => _birthday; set => this.RaiseAndSetIfChanged(ref _birthday, value); }
  29. public string TMail { get => _mail; set => this.RaiseAndSetIfChanged(ref _mail, value); }
  30. public string TPhone { get => _phone; set => this.RaiseAndSetIfChanged(ref _phone, value); }
  31. public string TExp { get => _exp; set => this.RaiseAndSetIfChanged(ref _exp, value); }
  32. public bool TGenderM { get => _genderM; set => this.RaiseAndSetIfChanged(ref _genderM, value); }
  33. public bool EnableBTN { get => _enableBTN; set => this.RaiseAndSetIfChanged(ref _enableBTN, value); }
  34. public ObservableCollection<Subject> CollSubSelect { get => _collSubSelect; set => this.RaiseAndSetIfChanged(ref _collSubSelect, value); }
  35. public List<Subject> CollSub => DB.Subjects.ToList();
  36. private bool CheckNotNull()
  37. {
  38. if (TName == null && TSurname == null && TPatronymic == null)
  39. {
  40. var box = MessageBoxManager.GetMessageBoxStandard("Íå-à", "ÔÈÎ íàäî ââåñòè");
  41. box.ShowAsync();
  42. return false;
  43. }
  44. if (TMail == null)
  45. {
  46. var box = MessageBoxManager.GetMessageBoxStandard("Íå-à", "Ýëåêòðîííóþ ïî÷òó æåëàòåëüíî (îáÿçàòåëüíî) çàïîëíèòü");
  47. box.ShowAsync();
  48. return false;
  49. }
  50. if (CollSubSelect.Count <= 0)
  51. {
  52. var box = MessageBoxManager.GetMessageBoxStandard("Íå-à", "Ïðåäïîäàâàòåëü áåç ïðåäìåòîâ?");
  53. box.ShowAsync();
  54. return false;
  55. }
  56. return true;
  57. }
  58. public Subject SelectSubject
  59. {
  60. get => _selectSubject;
  61. set
  62. {
  63. int f = 0;
  64. foreach (var item in CollSubSelect)
  65. {
  66. if (item == value)
  67. {
  68. f = 1;
  69. }
  70. }
  71. if (f == 0)
  72. {
  73. CollSubSelect.Add(value);
  74. }
  75. }
  76. }
  77. public bool AddNew()
  78. {
  79. if (CheckNotNull())
  80. {
  81. int IdGen = 1;
  82. int exp;
  83. string birtdate = TBirthday.Year + "-" + TBirthday.Month + "-" + TBirthday.Day;
  84. if (TGenderM == false)
  85. {
  86. IdGen = 2;
  87. }
  88. if (TExp == null)
  89. {
  90. exp = 0;
  91. }
  92. else
  93. {
  94. exp = Convert.ToInt32(TExp);
  95. }
  96. Teacher nT = new Teacher()
  97. {
  98. Name = TName,
  99. Surname = TSurname,
  100. Patronymic = TPatronymic,
  101. Birthdate = Convert.ToDateTime(birtdate),
  102. Email = TMail,
  103. Phone = TPhone,
  104. Expirience = exp,
  105. IdGender = IdGen
  106. };
  107. DB.Teachers.Add(nT);
  108. DB.SaveChanges();
  109. foreach (var item in CollSubSelect)
  110. {
  111. TeachersSubject ts = new TeachersSubject()
  112. {
  113. IdTeacher = nT.Id,
  114. IdSubject = item.Id
  115. };
  116. DB.TeachersSubjects.Add(ts);
  117. }
  118. DB.SaveChanges();
  119. CollSubSelect.Clear();
  120. return true;
  121. }
  122. else
  123. {
  124. return false;
  125. }
  126. }
  127. }
  128. }