CabinetViewModel.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using System;
  2. using System.Collections.Generic;
  3. using MsBox.Avalonia;
  4. using System.Reactive;
  5. using System.Text.RegularExpressions;
  6. using ReactiveUI;
  7. using Microsoft.EntityFrameworkCore;
  8. using System.Linq;
  9. using System.Numerics;
  10. using System.Text;
  11. namespace prakt1314.ViewModels
  12. {
  13. public class CabinetViewModel : ReactiveObject
  14. {
  15. public CabinetViewModel(int userID)
  16. {
  17. ConfirmChanges = ReactiveCommand.Create(ConfirmChangesCommand);
  18. using (AvaloniaPrakt13DbContext _dbContext = new AvaloniaPrakt13DbContext()) _genderList = _dbContext.UserGenders.ToList();
  19. using (AvaloniaPrakt13DbContext _dbContext = new AvaloniaPrakt13DbContext()) UserData = _dbContext.UserData
  20. .Include(a => a.IdUserNavigation)
  21. .Include(a => a.IdGenderNavigation)
  22. .Where(a => a.IdUser == userID)
  23. .FirstOrDefault();
  24. WorkTimeInYear = ((double)UserData.Worktime / 12).ToString();
  25. foreach (UserGender gender in GenderList)
  26. {
  27. _userData.IdGenderNavigation = (gender.IdGender == _userData.IdGender) ? gender : _userData.IdGenderNavigation;
  28. }
  29. this.RaisePropertyChanged(nameof(UserData));
  30. }
  31. public UserDatum UserData
  32. {
  33. get => _userData;
  34. set => this.RaiseAndSetIfChanged(ref _userData, value);
  35. }
  36. public ICollection<UserGender> GenderList => _genderList;
  37. public string NewPassword
  38. {
  39. get => _newPassword;
  40. set => this.RaiseAndSetIfChanged(ref _newPassword, value);
  41. }
  42. public string NewPasswordConfirm
  43. {
  44. get => _newPasswordConfirm;
  45. set => this.RaiseAndSetIfChanged(ref _newPasswordConfirm, value);
  46. }
  47. public ReactiveCommand<Unit, Unit> ConfirmChanges { get; }
  48. public bool IsChecked
  49. {
  50. get => _isChecked;
  51. set => this.RaiseAndSetIfChanged(ref _isChecked, value);
  52. }
  53. public string? WorkTimeInYear
  54. {
  55. get => _workTimeInYear;
  56. set => this.RaiseAndSetIfChanged(ref _workTimeInYear, value);
  57. }
  58. public void ConfirmChangesCommand()
  59. {
  60. try
  61. {
  62. double year = Convert.ToDouble(WorkTimeInYear);
  63. UserData.Worktime = (int)(year * 12);
  64. WorkTimeInYear = ((double)UserData.Worktime / 12).ToString();
  65. }
  66. catch
  67. {
  68. UserData.Worktime = 0;
  69. WorkTimeInYear = ((double)UserData.Worktime / 12).ToString();
  70. }
  71. if (UserData.Surname.Length == 0 || UserData.Surname is null)
  72. {
  73. MessageBoxManager.GetMessageBoxStandard("Èçìåíåíèÿ íå ñîõðàíåíû!"
  74. , "Ôàìèëèÿ ïóñòà!"
  75. , MsBox.Avalonia.Enums.ButtonEnum.Ok)
  76. .ShowAsync();
  77. return;
  78. }
  79. if (UserData.Name.Length == 0 || UserData.Name is null)
  80. {
  81. MessageBoxManager.GetMessageBoxStandard("Èçìåíåíèÿ íå ñîõðàíåíû!"
  82. , "Èìÿ ïóñòîå!"
  83. , MsBox.Avalonia.Enums.ButtonEnum.Ok)
  84. .ShowAsync();
  85. return;
  86. }
  87. if (UserData.Patronymic is null || UserData.Patronymic.Length == 0)
  88. {
  89. MessageBoxManager.GetMessageBoxStandard("Èçìåíåíèÿ íå ñîõðàíåíû!"
  90. , "Îò÷åñòâî ïóñòîå!"
  91. , MsBox.Avalonia.Enums.ButtonEnum.Ok)
  92. .ShowAsync();
  93. return;
  94. }
  95. if (UserData.IdUserNavigation.Login.Length < 3 || UserData.IdUserNavigation.Login is null)
  96. {
  97. MessageBoxManager.GetMessageBoxStandard("Èçìåíåíèÿ íå ñîõðàíåíû!"
  98. , "Ëîãèí íå ìåíåå 3 ñèìâîëîâ!"
  99. , MsBox.Avalonia.Enums.ButtonEnum.Ok)
  100. .ShowAsync();
  101. return;
  102. }
  103. if (UserData.Birthdate >= DateTime.Now)
  104. {
  105. MessageBoxManager.GetMessageBoxStandard("Èçìåíåíèÿ íå ñîõðàíåíû!"
  106. , "Äàòà ðàæäåíèÿ â áóäóùåì!"
  107. , MsBox.Avalonia.Enums.ButtonEnum.Ok)
  108. .ShowAsync();
  109. return;
  110. }
  111. if (UserData.Email is null || UserData.Email.Length == 0)
  112. {
  113. MessageBoxManager.GetMessageBoxStandard("Èçìåíåíèÿ íå ñîõðàíåíû!"
  114. , "Ïî÷òà ïóñòà!"
  115. , MsBox.Avalonia.Enums.ButtonEnum.Ok)
  116. .ShowAsync();
  117. return;
  118. }
  119. if (UserData.Email.Length > 0 && !Regex.IsMatch(UserData.Email,
  120. "(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])"))
  121. {
  122. MessageBoxManager.GetMessageBoxStandard("Èçìåíåíèÿ íå ñîõðàíåíû!"
  123. , "Ïî÷òà íåêîððåêòíà!"
  124. , MsBox.Avalonia.Enums.ButtonEnum.Ok)
  125. .ShowAsync();
  126. return;
  127. }
  128. if (UserData.PhoneNumber is null || UserData.PhoneNumber.Length == 0)
  129. {
  130. MessageBoxManager.GetMessageBoxStandard("Èçìåíåíèÿ íå ñîõðàíåíû!"
  131. , "Íîìåð òåëåôîíà ïóñò!"
  132. , MsBox.Avalonia.Enums.ButtonEnum.Ok)
  133. .ShowAsync();
  134. return;
  135. }
  136. if (!(UserData.PhoneNumber is null || UserData.PhoneNumber.Length == 0))
  137. {
  138. if (!Regex.IsMatch(UserData.PhoneNumber, "^((8|\\+7)[\\- ]?)?(\\(?\\d{3}\\)?[\\- ]?)?[\\d\\- ]{7,10}$"))
  139. {
  140. MessageBoxManager.GetMessageBoxStandard("Èçìåíåíèÿ íå ñîõðàíåíû!"
  141. , "Òåëåôîí íåêîððåêòåí!"
  142. , MsBox.Avalonia.Enums.ButtonEnum.Ok)
  143. .ShowAsync();
  144. return;
  145. }
  146. StringBuilder ConvertedPhone = new StringBuilder("89");
  147. int NumberOfDigits = 0;
  148. foreach (char ch in UserData.PhoneNumber) if (char.IsDigit(ch)) NumberOfDigits++;
  149. int counter = -9 + NumberOfDigits;
  150. foreach (char ch in UserData.PhoneNumber) if (char.IsDigit(ch)) if (counter-- <= 0) ConvertedPhone.Append(ch);
  151. UserData.PhoneNumber = ConvertedPhone.ToString();
  152. }
  153. if (IsChecked)
  154. {
  155. if (NewPassword is null || NewPassword.Length == 0)
  156. {
  157. MessageBoxManager.GetMessageBoxStandard("Èçìåíåíèÿ íå ñîõðàíåíû!"
  158. , "Ïàðîëü ïóñò!"
  159. , MsBox.Avalonia.Enums.ButtonEnum.Ok)
  160. .ShowAsync();
  161. return;
  162. }
  163. if (NewPassword != NewPasswordConfirm)
  164. {
  165. MessageBoxManager.GetMessageBoxStandard("Èçìåíåíèÿ íå ñîõðàíåíû!"
  166. , "Ïàðîëè ðàçíûå!"
  167. , MsBox.Avalonia.Enums.ButtonEnum.Ok)
  168. .ShowAsync();
  169. return;
  170. }
  171. UserData.IdUserNavigation.Password = NewPassword;
  172. }
  173. using (AvaloniaPrakt13DbContext db = new AvaloniaPrakt13DbContext())
  174. {
  175. db.Entry(UserData).State = EntityState.Modified;
  176. db.Entry(UserData.IdUserNavigation).State = EntityState.Modified;
  177. db.Entry(UserData.IdGenderNavigation).State = EntityState.Modified;
  178. db.SaveChanges();
  179. }
  180. MessageBoxManager.GetMessageBoxStandard("Èçìåíåíèÿ ñîõðàíåíû!"
  181. , "Èçìåíåíèÿ óñïåøíî ñîõðàíåíû!"
  182. , MsBox.Avalonia.Enums.ButtonEnum.Ok)
  183. .ShowAsync();
  184. return;
  185. }
  186. private UserDatum _userData;
  187. private List<UserGender> _genderList;
  188. private string _newPassword;
  189. private string _newPasswordConfirm;
  190. private bool _isChecked = false;
  191. private string? _workTimeInYear;
  192. }
  193. }