AuthPageViewModel.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Collections.Generic;
  3. using MsBox.Avalonia.Enums;
  4. using MsBox.Avalonia;
  5. using System.Reactive;
  6. using ReactiveUI;
  7. using Microsoft.EntityFrameworkCore;
  8. using System.Threading.Tasks;
  9. using System.Linq;
  10. namespace prakt1314.ViewModels
  11. {
  12. public class AuthPageViewModel : ReactiveObject
  13. {
  14. public AuthPageViewModel()
  15. {
  16. AuthorizeCommand = ReactiveCommand.Create<string>(AuthorizeInSystemCommandMethod);
  17. }
  18. public event Action<int> NotifyUserWasSuccessfulAuthorize;
  19. public string Login
  20. {
  21. get => _login;
  22. set => this.RaiseAndSetIfChanged(ref _login, value);
  23. }
  24. public string Password
  25. {
  26. get => _password;
  27. set => this.RaiseAndSetIfChanged(ref _password, value);
  28. }
  29. public ReactiveCommand<string, Unit> AuthorizeCommand { get; }
  30. private string _login;
  31. private string _password;
  32. private async void AuthorizeInSystemCommandMethod(string param)
  33. {
  34. UserLogin? user;
  35. using (AvaloniaPrakt13DbContext db = new AvaloniaPrakt13DbContext())
  36. {
  37. user = db.UserLogins.FirstOrDefault(a => a.Login == Login && a.Password == Password);
  38. }
  39. if (user is null)
  40. {
  41. MessageBoxManager.GetMessageBoxStandard("Îøèáêà àâòîðèçàöèè!"
  42. , "Ïîëüçîâàòåëü íå ñóùåñòâóåò"
  43. , ButtonEnum.Ok).ShowAsync();
  44. return;
  45. }
  46. NotifyUserWasSuccessfulAuthorize(user.IdUser);
  47. }
  48. }
  49. }