UserProfileViewModel.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using FinancialManagement.Models;
  2. using ReactiveUI;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.Threading.Tasks;
  9. namespace FinancialManagement.ViewModels
  10. {
  11. public class UserProfileViewModel : ReactiveObject
  12. {
  13. string phone;
  14. string login;
  15. string password;
  16. string userName;
  17. User currentUser;
  18. ShmelevTeamContext context;
  19. string message;
  20. char? passwordChar = '*';
  21. public UserProfileViewModel(User currentUser, ShmelevTeamContext ppContext)
  22. {
  23. this.context = ppContext;
  24. this.currentUser = currentUser;
  25. this.phone = currentUser.Phone;
  26. this.login = currentUser.Login;
  27. this.password = currentUser.Password;
  28. this.UserName = currentUser.Username;
  29. }
  30. public string Message { get => message; set => this.RaiseAndSetIfChanged(ref message, value); }
  31. public User CurrentUser { get => currentUser; set => currentUser = value; }
  32. public string Phone { get => phone; set => this.RaiseAndSetIfChanged(ref phone, value); }
  33. public string Login { get => login; set => this.RaiseAndSetIfChanged(ref login, value); }
  34. public string Password { get => password; set => this.RaiseAndSetIfChanged(ref password, value); }
  35. public string UserName { get => userName; set => this.RaiseAndSetIfChanged(ref userName, value); }
  36. public char? PasswordChar { get => passwordChar; set => this.RaiseAndSetIfChanged(ref passwordChar, value); }
  37. public User SaveChanges()
  38. {
  39. if (string.IsNullOrEmpty(Login) || string.IsNullOrEmpty(UserName) || string.IsNullOrEmpty(Password) || string.IsNullOrEmpty(Phone))
  40. {
  41. Message = "Заполните все поля";
  42. return null;
  43. }
  44. else if (!Regex.IsMatch(Phone, @"^(?:\+7|8)\d{10}$"))
  45. {
  46. Message = "Введите корректный телефон";
  47. return null;
  48. }
  49. else if (!Regex.IsMatch(Password, @"\S{6,}"))
  50. {
  51. Message = "Ваш пароль не должен быть \nменьше 6 символов";
  52. return null;
  53. }
  54. else if (!Regex.IsMatch(Password, @"\w*[0-9]\w*"))
  55. {
  56. Message = "У вашего пароля нет цифры";
  57. return null;
  58. }
  59. else if (Login == CurrentUser.Login)
  60. {
  61. return EditUserData();
  62. }
  63. else if (context.Users.FirstOrDefault(x => x.Login == Login) != null)
  64. {
  65. Message = "Такой пользователь существует\nВведите другой логин";
  66. return null;
  67. }
  68. else
  69. {
  70. return EditUserData();
  71. }
  72. }
  73. private User EditUserData()
  74. {
  75. CurrentUser.Login = Login;
  76. CurrentUser.Password = Password;
  77. CurrentUser.Username = UserName;
  78. CurrentUser.Phone = Phone;
  79. context.SaveChanges();
  80. return CurrentUser;
  81. }
  82. }
  83. }