PageUserCabinetViewModel.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using AvaloniaApplication5.Models;
  5. using Microsoft.EntityFrameworkCore;
  6. using ReactiveUI;
  7. namespace AvaloniaApplication5.ViewModels
  8. {
  9. public class PageUserCabinetViewModel : ReactiveObject
  10. {
  11. Logintable? currentUser;
  12. _1234Context dBConnect;
  13. string oldPass;
  14. public PageUserCabinetViewModel(_1234Context dBConnect, int idUser)
  15. {
  16. this.dBConnect = dBConnect;
  17. CurrentUser = dBConnect.Logintables.Include(x=>x.IdRoleNavigation).Include(x=>x.User.IdGenderNavigation).FirstOrDefault(x => x.Id == idUser);
  18. oldPass = currentUser.Password;
  19. }
  20. public PageUserCabinetViewModel(_1234Context dBConnect)
  21. {
  22. this.dBConnect = dBConnect;
  23. int maxid = dBConnect.Logintables.Select(x => x.Id).Max();
  24. currentUser = new Logintable() { User = new User() { IdLogin = ++maxid }, IdRole =2 };
  25. changePass = true;//÷òîáû èçìåíåíèå ïàðîëÿ áûëî àêèâíî
  26. }
  27. public Logintable? CurrentUser { get => currentUser; set => currentUser = value; }
  28. public List<Gender> Genders => dBConnect.Genders.ToList();
  29. public string DR { get => CurrentUser.User.BirthDate.ToString(); set => CurrentUser.User.BirthDate = DateTime.Parse(value); }
  30. public bool ChangePass { get => changePass; set =>this.RaiseAndSetIfChanged(ref changePass, value); }
  31. public string PasswordMatch { get => passwordMatch; set => passwordMatch = value; }
  32. public string Message { get => message; set =>this.RaiseAndSetIfChanged(ref message,value); }
  33. bool changePass = false;
  34. string passwordMatch;
  35. string message;
  36. public void SaveData()
  37. {
  38. if (CurrentUser.Password == null || CurrentUser.Password =="") CurrentUser.Password = oldPass;
  39. if (changePass==(currentUser.Password == PasswordMatch))
  40. {
  41. if (currentUser.Id == 0) dBConnect.Add(currentUser);
  42. dBConnect.SaveChanges();
  43. Message = "Ñîõðàíåíî óñïåøíî";
  44. }
  45. else
  46. {
  47. Message = "Ïàðîëè íå ñîâïàäàþò";
  48. }
  49. }
  50. }
  51. }