MainWindowViewModel.cs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using Avalonia.Controls;
  2. using ReactiveUI;
  3. using AvaloniaApplicationTestNew.Views;
  4. using AvaloniaApplicationTestNew.ViewModels;
  5. using AvaloniaApplicationTestNew.Models;
  6. using Microsoft.EntityFrameworkCore;
  7. using System.Linq;
  8. using System;
  9. namespace AvaloniaApplicationTestNew.ViewModels
  10. {
  11. public class MainWindowViewModel : ViewModelBase
  12. {
  13. UserControl uc = new Markup();
  14. UserControl page = new Authorization();
  15. Logined? current_user;
  16. AuthorizationViewModel authorizationViewModel = new AuthorizationViewModel();
  17. UserPageViewModel userPageViewModel;
  18. AdminPageViewModel adminPageViewModel;
  19. AddUserPageViewModel addUserPageViewModel;
  20. bool isButtonVisible;
  21. public Logined? Current_user { get => current_user; set => this.RaiseAndSetIfChanged(ref current_user, value); }
  22. public UserControl UC { get => uc; set => this.RaiseAndSetIfChanged(ref uc, value); }
  23. public UserControl Page { get => page; set => this.RaiseAndSetIfChanged(ref page,value); }
  24. public AuthorizationViewModel AuthorizationVM { get => authorizationViewModel; set => authorizationViewModel = value; }
  25. public UserPageViewModel UserPageVM { get => userPageViewModel; set => userPageViewModel = value; }
  26. public AdminPageViewModel AdminPageVM { get => adminPageViewModel; set => adminPageViewModel = value; }
  27. public AddUserPageViewModel AddUserPageVM { get => addUserPageViewModel; set => addUserPageViewModel = value; }
  28. public bool IsButtonVisible { get => isButtonVisible; set => this.RaiseAndSetIfChanged(ref isButtonVisible, value); }
  29. public static MainWindowViewModel Self;
  30. public MainWindowViewModel()
  31. {
  32. Self = this;
  33. }
  34. public void Loged()
  35. {
  36. IsButtonVisible = false;
  37. Current_user = myContext.Logineds.Include(x => x.User.IdGenderNavigation).FirstOrDefault(x => x.Login == AuthorizationVM.Login && x.Password == AuthorizationVM.Password);
  38. if (Current_user != null)
  39. {
  40. switch(Current_user.IdRole)
  41. {
  42. case 1:
  43. Page = new AdminPage();
  44. AdminPageVM = new AdminPageViewModel();
  45. GetAccess();
  46. break;
  47. case 2:
  48. UserPageVM = new UserPageViewModel(current_user.User.IdUser);
  49. Page = new UserPage();
  50. break;
  51. }
  52. }
  53. else
  54. {
  55. AuthorizationVM.Message = "Неверные данные";
  56. AuthorizationVM.Error();
  57. Page = new Authorization();
  58. }
  59. }
  60. public void GetAccess()
  61. {
  62. IsButtonVisible = true;
  63. }
  64. public void BackToList()
  65. {
  66. if (IsButtonVisible)
  67. {
  68. Page = new AdminPage();
  69. }
  70. }
  71. public void DeleteUser()
  72. {
  73. int id = UserPageVM.Current_user.IdUser;
  74. if(myContext.Logineds.FirstOrDefault(x => x.Id == id).IdRole == 1)
  75. {
  76. UserPageVM.Message = "Пользователя нельзя удалить, так как он администратор";
  77. }
  78. else
  79. {
  80. myContext.Remove(myContext.Users.FirstOrDefault(x => x.IdUser == id));
  81. myContext.Remove(myContext.Logineds.FirstOrDefault(x => x.Id == id));
  82. myContext.SaveChanges();
  83. Page = new AdminPage();
  84. }
  85. }
  86. }
  87. }