MainWindowViewModel.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using Avalonia.Controls;
  2. using AvaloniaApplication1.Models;
  3. using AvaloniaApplication1.Views;
  4. using Microsoft.EntityFrameworkCore;
  5. using ReactiveUI;
  6. using System;
  7. using System.Linq;
  8. using System.Net.Http;
  9. using System.Text.Json.Serialization;
  10. namespace AvaloniaApplication1.ViewModels
  11. {
  12. public class MainWindowViewModel : ViewModelBase
  13. {
  14. private readonly ApiService _apiService;
  15. string login;
  16. string password;
  17. string info;
  18. UserControl uc ;
  19. int id;
  20. User user = new User();
  21. public static MainWindowViewModel Self;
  22. HttpClient client;
  23. public MainWindowViewModel()
  24. {
  25. _apiService = new ApiService();
  26. Self = this;
  27. Uc = new UserControl1();
  28. }
  29. public string Login { get => login; set => this.RaiseAndSetIfChanged(ref login, value); }
  30. public int Id { get => id; set => this.RaiseAndSetIfChanged(ref id, value); }
  31. public string Password { get => password; set => this.RaiseAndSetIfChanged(ref password, value); }
  32. public string Info { get => info; set => this.RaiseAndSetIfChanged(ref info, value); }
  33. public UserControl Uc { get => uc; set => this.RaiseAndSetIfChanged(ref uc, value); }
  34. public void Enter() => Uc = new Enter();
  35. public void Registrainon() => Uc = new RedistrationPage();
  36. public async void SignIn()
  37. {
  38. User user = await _apiService.GetAsync<User>($"signIn?login={login}&password={password}");
  39. if (user != null)
  40. {
  41. if(user.IdRole == 2)
  42. {
  43. Id = user.Id;
  44. Uc = new AdminPage();
  45. }
  46. else
  47. {
  48. Id = user.Id;
  49. Uc = new UserPage();
  50. }
  51. }
  52. else
  53. {
  54. Info = "Неверный логин или пароль";
  55. Password = "";
  56. }
  57. }
  58. }
  59. }