MainWindowViewModel.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 MainWindowViewModel()
  23. {
  24. }
  25. //Метод для авторизации
  26. public void Enter()
  27. {
  28. currentUser = _db.Logineds
  29. .Include(x => x.Role)
  30. .FirstOrDefault(x => x.Login == login && x.Password == password);
  31. if (currentUser != null)
  32. {
  33. Info = $"Добро пожаловать, {currentUser.User.Username}!";
  34. Password = "";
  35. //Переход на страницу в зависимости от роли
  36. switch (currentUser.Role.Name)
  37. {
  38. case "admin":
  39. Page = new AdminWindow();
  40. break;
  41. case "user":
  42. Page = new UserWindow();
  43. break;
  44. }
  45. } else
  46. {
  47. Info = "Неверный логин или пароль";
  48. Password = "";
  49. }
  50. }
  51. public void Save() =>_db.SaveChanges();
  52. public void GoBack() => Page = new Autorization();
  53. }
  54. }