AddTeacherViewModel.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Avalonia.Controls;
  5. using AvaloniaTeachersDB.Models;
  6. using AvaloniaTeachersDB.Views;
  7. using MsBox.Avalonia.Enums;
  8. using MsBox.Avalonia;
  9. using ReactiveUI;
  10. using Tmds.DBus.Protocol;
  11. using System.Collections.ObjectModel;
  12. namespace AvaloniaTeachersDB.ViewModels
  13. {
  14. public class AddTeacherViewModel : ReactiveObject
  15. {
  16. public static _43pToropovaContext DBConnection = new _43pToropovaContext();
  17. private string _name;
  18. private string _surname;
  19. private string _patronymic;
  20. private string _mail;
  21. private string _phone;
  22. private DateTimeOffset _birthday = new DateTimeOffset(DateTime.Now);
  23. private SchoolSubject _selectSubject;
  24. private int _selectIndexSubject;
  25. private string _exp;
  26. private bool _genderM = true;
  27. private bool _enableBTN = false;
  28. //private List<SchoolSubject> _collSubSelect = new List<SchoolSubject>();
  29. private ObservableCollection<SchoolSubject> _collSubSelect = new ObservableCollection<SchoolSubject>();
  30. public string TName { get => _name; set => this.RaiseAndSetIfChanged(ref _name, value); }
  31. public string TSurname { get => _surname; set => this.RaiseAndSetIfChanged(ref _surname, value); }
  32. public string TPatronymic { get => _patronymic; set => this.RaiseAndSetIfChanged(ref _patronymic, value); }
  33. public DateTimeOffset TBirthday { get => _birthday; set => this.RaiseAndSetIfChanged(ref _birthday, value); }
  34. public string TMail { get => _mail; set => this.RaiseAndSetIfChanged(ref _mail, value); }
  35. public string TPhone { get => _phone; set => this.RaiseAndSetIfChanged(ref _phone, value); }
  36. public string TExp { get => _exp; set => this.RaiseAndSetIfChanged(ref _exp, value); }
  37. public bool TGenderM { get => _genderM; set => this.RaiseAndSetIfChanged(ref _genderM, value); }
  38. public bool EnableBTN { get => _enableBTN; set => this.RaiseAndSetIfChanged(ref _enableBTN, value); }
  39. public ObservableCollection<SchoolSubject> CollSubSelect { get => _collSubSelect; set => this.RaiseAndSetIfChanged(ref _collSubSelect, value); }
  40. public List<SchoolSubject> CollSub => DBConnection.SchoolSubjects.ToList();
  41. private bool CheckNotNull()
  42. {
  43. if (TName == null && TSurname == null && TPatronymic == null)
  44. {
  45. var box = MessageBoxManager.GetMessageBoxStandard("Error", "íåò ôèî");
  46. box.ShowAsync();
  47. return false;
  48. }
  49. if (TMail == null)
  50. {
  51. var box = MessageBoxManager.GetMessageBoxStandard("Error", "íåò ïî÷òû");
  52. box.ShowAsync();
  53. return false;
  54. }
  55. if(CollSubSelect.Count<=0)
  56. {
  57. var box = MessageBoxManager.GetMessageBoxStandard("Error", "íåò ïðåïîäàâàåìûõ ïðåäìåòîâ");
  58. box.ShowAsync();
  59. return false;
  60. }
  61. return true;
  62. }
  63. public SchoolSubject SelectSubject
  64. {
  65. get => _selectSubject;
  66. set
  67. {
  68. int f = 0;
  69. foreach (var item in CollSubSelect)
  70. {
  71. if (item == value)
  72. {
  73. f = 1;
  74. }
  75. }
  76. if (f == 0)
  77. CollSubSelect.Add(value);
  78. }
  79. }
  80. /*
  81. public int SelectIndexSubject
  82. {
  83. get => _selectIndexSubject;
  84. set
  85. {
  86. var mews = MessageBoxManager.GetMessageBoxStandard("ff", value.ToString());
  87. mews.ShowAsync();
  88. SchoolSubject tmp;
  89. if (CollSubSelect.Count>0)
  90. {
  91. int ind= 0;
  92. foreach(var item in CollSubSelect)
  93. {
  94. if (value == ind)
  95. {
  96. tmp = item;
  97. break;
  98. }
  99. ind++;
  100. }
  101. CollSubSelect.RemoveAt(ind);
  102. }
  103. }
  104. }
  105. */
  106. public bool AddNewTeacher()
  107. {
  108. if (CheckNotNull())
  109. {
  110. int indG = 1;
  111. int exp;
  112. //DateTime tmp = new DateTime(TBirthday.Year, TBirthday.Month, TBirthday.Day);
  113. string birtdate = TBirthday.Year +"-"+ TBirthday.Month +"-"+ TBirthday.Day;
  114. if (TGenderM == false)
  115. indG = 2;
  116. if (TExp == null)
  117. exp = 0;
  118. else
  119. exp = Convert.ToInt32(TExp);
  120. Teacher nT = new Teacher()
  121. {
  122. NameTeacher = TName,
  123. SurnameTeacher = TSurname,
  124. PatronymicTeacher = TPatronymic,
  125. BirthdayTeacher = DateOnly.Parse(birtdate),
  126. Mail = TMail,
  127. Phone = TPhone,
  128. ExperienceTeacher = exp,
  129. IdGender = indG
  130. };
  131. DBConnection.Teachers.Add(nT);
  132. DBConnection.SaveChanges();
  133. foreach(var item in CollSubSelect)
  134. {
  135. TeachersSubject ts = new TeachersSubject()
  136. {
  137. IdTeacher = nT.IdTeachers,
  138. IdSubjects = item.IdSubjects
  139. };
  140. DBConnection.TeachersSubjects.Add(ts);
  141. }
  142. DBConnection.SaveChanges();
  143. CollSubSelect.Clear();
  144. return true;
  145. }
  146. return false;
  147. }
  148. }
  149. }