MainWindowViewModel.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using Avalonia.Controls;
  2. using AvaloniaApplication5.Models;
  3. //using AvaloniaApplication5.Models;
  4. using AvaloniaApplication5.Views;
  5. using Microsoft.EntityFrameworkCore;
  6. using ReactiveUI;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using Tmds.DBus.Protocol;
  10. namespace AvaloniaApplication5.ViewModels
  11. {
  12. public class MainWindowViewModel : ViewModelBase
  13. {
  14. static _1234Context DBConnect = new _1234Context();
  15. private UserControl uc = new PageLogin();
  16. public UserControl UC { get => uc; set => this.RaiseAndSetIfChanged(ref uc, value); }
  17. #region VMLogin
  18. PageLoginViewModel pageLoginVM = new PageLoginViewModel();
  19. public PageLoginViewModel PageLoginVM { get => pageLoginVM; set => pageLoginVM = value; }
  20. public void LoadPageList()
  21. {
  22. Logintable? currentUser = DBConnect.Logintables.FirstOrDefault(x => x.Login == PageLoginVM.Login && x.Password == PageLoginVM.Password);
  23. if (currentUser != null)
  24. {
  25. switch (currentUser.IdRole)
  26. {
  27. case 1:
  28. UC = new PageUserList();
  29. break;
  30. case 2:
  31. PageUCabinetVM = new PageUserCabinetViewModel(DBConnect, currentUser.Id);
  32. UC = new PageUserCabinet();
  33. break;
  34. }
  35. }
  36. else PageLoginVM.Message = "Пользователь отсутствует в системе, либо указанные данные введены неверно";
  37. }
  38. #endregion
  39. #region VMUserCabinet
  40. private PageUserCabinetViewModel pageUCabinetVM;
  41. public PageUserCabinetViewModel PageUCabinetVM { get => pageUCabinetVM; set => pageUCabinetVM = value; }
  42. #endregion
  43. #region VMadmin
  44. private PageUserListViewModel pageUserListViewVM = new PageUserListViewModel(DBConnect);
  45. public PageUserListViewModel PageUserListViewVM { get => pageUserListViewVM; set => pageUserListViewVM = value; }
  46. UserControl ucAdmin;
  47. public UserControl UCAdmin
  48. {
  49. get => ucAdmin;
  50. set => this.RaiseAndSetIfChanged(ref ucAdmin, value);
  51. }
  52. public void EditUser(int id)
  53. {
  54. PageUCabinetVM = id == 0 ? new PageUserCabinetViewModel(DBConnect) : new PageUserCabinetViewModel(DBConnect, id);
  55. UCAdmin = new PageUserCabinet();
  56. }
  57. public void DeleteUser(int id)
  58. {
  59. Logintable? delUser = DBConnect.Logintables.FirstOrDefault(x => x.Id == id);
  60. DBConnect.Logintables.Remove(delUser);
  61. DBConnect.SaveChanges();
  62. UC = new PageUserList();//для обновления списка на странице
  63. }
  64. #endregion
  65. }
  66. }