MainWindowViewModel.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using AvaloniaApplication2.Models;
  2. using Avalonia.Controls;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using CommunityToolkit.Mvvm.ComponentModel;
  6. using CommunityToolkit.Mvvm.Input;
  7. using Microsoft.EntityFrameworkCore;
  8. using System;
  9. using System.Collections.Generic;
  10. using AvaloniaPractic;
  11. using AvaloniaPractic.Models;
  12. namespace AvaloniaApplication2.ViewModels
  13. {
  14. public partial class MainWindowViewModel: ViewModelBase
  15. {
  16. Pavlov41pContext _db = new Pavlov41pContext();
  17. [ObservableProperty] string login;
  18. [ObservableProperty] string password;
  19. [ObservableProperty] string info;
  20. [ObservableProperty] Logined? currentUser;
  21. [ObservableProperty] UserControl page = new Autorization();
  22. public static MainWindowViewModel Self;
  23. public MainWindowViewModel()
  24. {
  25. Self = this;
  26. }
  27. //Метод для авторизации
  28. public void Enter()
  29. {
  30. currentUser = _db.Logineds
  31. .Include(x => x.Role)
  32. .FirstOrDefault(x => x.Login == login && x.Password == password);
  33. if (currentUser != null)
  34. {
  35. Info = "Добро пожаловать!";
  36. Password = "";
  37. //Переход на страницу в зависимости от роли
  38. switch (currentUser.Role.Name)
  39. {
  40. case "admin":
  41. Page = new AdminWindow();
  42. break;
  43. case "user":
  44. Page = new UserWindow();
  45. break;
  46. }
  47. } else
  48. {
  49. Info = "Неверный логин или пароль";
  50. Password = "";
  51. }
  52. }
  53. public void Save() =>_db.SaveChanges();
  54. public void GoBack() => Page = new Autorization();
  55. }
  56. }