PersonalAccountViewModel.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Avalonia.Controls;
  5. using AvaloniaApplication3.Models;
  6. using Microsoft.EntityFrameworkCore;
  7. using ReactiveUI;
  8. namespace AvaloniaApplication3.ViewModels
  9. {
  10. public class PersonalAccountViewModel : ViewModelBase
  11. {
  12. User? currentUser;
  13. UsersLogin? user;
  14. TestDataBaseCheskinaContext db = new TestDataBaseCheskinaContext();
  15. public PersonalAccountViewModel(User? currentUser)
  16. {
  17. this.currentUser = currentUser;
  18. GetData();
  19. }
  20. private void GetData()
  21. {
  22. user = db.UsersLogins.Include(u => u.IdUserNavigation).FirstOrDefault(x => x.IdUser == currentUser.IdUser);
  23. Name = user.IdUserNavigation.Name;
  24. Surname = user.IdUserNavigation.Surname;
  25. Patronumic = user.IdUserNavigation.Patronymic;
  26. Login = user.Login;
  27. Password = user.Password;
  28. }
  29. private string? name;
  30. private string? surname;
  31. private string? patronumic;
  32. private string? login;
  33. private string? password;
  34. private string? newPassword;
  35. private string? newPasswordAgain;
  36. private string message = "";
  37. private bool isChangedPass = false;
  38. public void SaveChanges()
  39. {
  40. if (Name != null && Name !="" && Surname != null && Surname != "" && Patronumic != null && Patronumic != "" && Login != null && Login != "")
  41. {
  42. user.IdUserNavigation.Name = Name;
  43. user.IdUserNavigation.Surname = Surname;
  44. user.IdUserNavigation.Patronymic = Patronumic;
  45. user.Login = Login;
  46. if (IsChangedPass)
  47. {
  48. if (NewPassword != NewPasswordAgain) Message = "Ïàðîëè íå ñîâïàäàþò";
  49. else if (NewPassword != null && NewPassword != "" && NewPasswordAgain != null && NewPasswordAgain != "") Password = NewPasswordAgain;
  50. else Message = "Çàïîëíåíû íå âñå äàííûå";
  51. }
  52. user.Password = Password;
  53. db.SaveChanges();
  54. }
  55. else Message = "Çàïîëíåíû íå âñå äàííûå";
  56. }
  57. public User? CurrentUser { get => currentUser; set => currentUser = value; }
  58. public string? Name { get => name; set => name = value; }
  59. public string? Surname { get => surname; set => surname = value; }
  60. public string? Patronumic { get => patronumic; set => patronumic = value; }
  61. public string? Login { get => login; set => login = value; }
  62. public string? Password { get => password; set => password = value; }
  63. public string? NewPassword { get => newPassword; set => newPassword = value; }
  64. public string? NewPasswordAgain { get => newPasswordAgain; set => newPasswordAgain = value; }
  65. public string Message { get => message; set => this.RaiseAndSetIfChanged(ref message, value); }
  66. public bool IsChangedPass { get => isChangedPass; set => this.RaiseAndSetIfChanged(ref isChangedPass, value); }
  67. }
  68. }