PersonalPageViewModel.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5. using AvaloniaApplication1.Models;
  6. using Microsoft.EntityFrameworkCore;
  7. using MsBox.Avalonia;
  8. using ReactiveUI;
  9. namespace AvaloniaApplication1.ViewModels
  10. {
  11. public class PersonalPageViewModel : ReactiveObject
  12. {
  13. ScoolContext DB = new ScoolContext();
  14. User? userData;
  15. public User? UserData { get => userData; set => userData = value; }
  16. public PersonalPageViewModel(int id)
  17. {
  18. UserData = DB.Users.Where(x => x.IdUser == id).Include(x => x.Teacher).ThenInclude(x => x.IdGenderNavigation).FirstOrDefault();
  19. }
  20. public string SelectedGender
  21. {
  22. get => UserData.Teacher.IdGenderNavigation.Gender1;
  23. set => UserData.Teacher.IdGender = DB.Genders.FirstOrDefault(x => x.Gender1 == value).Id;
  24. }
  25. public string DR
  26. {
  27. get => UserData.Teacher.Birthdate.ToString();
  28. set => UserData.Teacher.Birthdate = DateTime.Parse(value);
  29. }
  30. public List<string> Genders => DB.Genders.Select(x => x.Gender1).ToList();
  31. bool _enable = false;
  32. public bool Enable
  33. {
  34. get => _enable;
  35. set => this.RaiseAndSetIfChanged(ref _enable, value);
  36. }
  37. public void Checked()
  38. {
  39. if(!Enable)
  40. {
  41. Enable = true;
  42. }
  43. else
  44. {
  45. Enable = false;
  46. }
  47. }
  48. string _newPass;
  49. string _newPassRepeat;
  50. public string NewPass
  51. {
  52. get => _newPass;
  53. set => this.RaiseAndSetIfChanged(ref _newPass, value);
  54. }
  55. public string NewPassRepeat
  56. {
  57. get => _newPassRepeat;
  58. set => this.RaiseAndSetIfChanged(ref _newPassRepeat, value);
  59. }
  60. public void FixChanges()
  61. {
  62. if(!CheckOnEmpty() || !CheckPhone())
  63. {
  64. return;
  65. }
  66. if(NewPass == NewPassRepeat)
  67. {
  68. UserData.Password = NewPassRepeat;
  69. DB.SaveChanges();
  70. var box = MessageBoxManager.GetMessageBoxStandard("Èçìåíåíèÿ", "Èçìåíåíèÿ ñîõðàíåíû)");
  71. box.ShowAsync();
  72. }
  73. else
  74. {
  75. var box = MessageBoxManager.GetMessageBoxStandard("Èçìåíåíèÿ", "Èçìåíåíèÿ íå ñîõðàíåíû (ïàðîëè íå ñîâïàäàþò)");
  76. box.ShowAsync();
  77. }
  78. }
  79. public bool CheckOnEmpty()
  80. {
  81. if (!Enable)
  82. {
  83. if (UserData.Teacher.Surname.ToString() == null || UserData.Teacher.Surname.ToString() == "" ||
  84. UserData.Teacher.Name.ToString() == null || UserData.Teacher.Name.ToString() == "" ||
  85. UserData.Teacher.Patronymic.ToString() == null || UserData.Teacher.Patronymic.ToString() == "" ||
  86. UserData.Login.ToString() == null || UserData.Login.ToString() == "" ||
  87. UserData.Teacher.Email.ToString() == null || UserData.Teacher.Email.ToString() == "" ||
  88. UserData.Teacher.Phone.ToString() == null || UserData.Teacher.Phone.ToString() == "")
  89. {
  90. var box = MessageBoxManager.GetMessageBoxStandard("Ïîëÿ", "Âñå ïîëÿ äîëæíû áûòü çàïîëíåíû");
  91. box.ShowAsync();
  92. return false;
  93. }
  94. else
  95. {
  96. return true;
  97. }
  98. }
  99. else
  100. {
  101. if (UserData.Teacher.Surname.ToString() == null || UserData.Teacher.Surname.ToString() == "" ||
  102. UserData.Teacher.Name.ToString() == null || UserData.Teacher.Name.ToString() == "" ||
  103. UserData.Teacher.Patronymic.ToString() == null || UserData.Teacher.Patronymic.ToString() == "" ||
  104. UserData.Login.ToString() == null || UserData.Login.ToString() == "" ||
  105. UserData.Teacher.Email.ToString() == null || UserData.Teacher.Email.ToString() == "" ||
  106. UserData.Teacher.Phone.ToString() == null || UserData.Teacher.Phone.ToString() == "" ||
  107. NewPass == null || NewPass == "" || NewPassRepeat == null || NewPassRepeat == "")
  108. {
  109. var box = MessageBoxManager.GetMessageBoxStandard("Ïîëÿ", "Âñå ïîëÿ äîëæíû áûòü çàïîëíåíû");
  110. box.ShowAsync();
  111. return false;
  112. }
  113. else
  114. {
  115. return true;
  116. }
  117. }
  118. }
  119. public bool CheckPhone()
  120. {
  121. if(UserData.Teacher.Phone.Length == 11)
  122. {
  123. Regex PhoneRegex = new Regex("^89\\d{9}$");
  124. if(PhoneRegex.IsMatch(UserData.Teacher.Phone))
  125. {
  126. return true;
  127. }
  128. else
  129. {
  130. var box = MessageBoxManager.GetMessageBoxStandard("Òåëåôîí íåïðàâ", "Òåëåôîí íå ñîäåðæèò äîñòàòî÷íîå êîëè÷åñòâî öèôð");
  131. box.ShowAsync();
  132. return false;
  133. }
  134. }
  135. else if(UserData.Teacher.Phone.Length < 11)
  136. {
  137. var box = MessageBoxManager.GetMessageBoxStandard("Òåëåôîí íåïðàâ", "Òåëåôîí íå ñîäåðæèò äîñòàòî÷íîå êîëè÷åñòâî öèôð");
  138. box.ShowAsync();
  139. return false;
  140. }
  141. else
  142. {
  143. string CorrectPhone = "";
  144. CorrectPhone = UserData.Teacher.Phone.Replace("(","");
  145. CorrectPhone = CorrectPhone.Replace(")", "");
  146. CorrectPhone = CorrectPhone.Replace("-", "");
  147. CorrectPhone = CorrectPhone.Replace(" ", "");
  148. if (CorrectPhone.Length == 11 )
  149. {
  150. UserData.Teacher.Phone = CorrectPhone;
  151. return true;
  152. }
  153. else
  154. {
  155. var box = MessageBoxManager.GetMessageBoxStandard("Òåëåôîí íåïðàâ", "Òåëåôîí íå ñîäåðæèò äîñòàòî÷íîå êîëè÷åñòâî öèôð");
  156. box.ShowAsync();
  157. return false;
  158. }
  159. }
  160. }
  161. }
  162. }