RegistrationWindowViewModel.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using WpfApp29.Models;
  8. namespace WpfApp29.ViewModels
  9. {
  10. class RegistrationWindowViewModel : BaseViewModel
  11. {
  12. private User? _user;
  13. public User User
  14. {
  15. get { return _user ??= new User(); }
  16. set { _user = value; OnPropertyChanged(); }
  17. }
  18. public RelayCommand? _commandRegister;
  19. public RelayCommand? CommandRegister
  20. {
  21. get
  22. {
  23. return _commandRegister ??= new RelayCommand(
  24. x =>
  25. {
  26. MainContext ctx = new MainContext();
  27. if (ctx.Users.Any(u => u.Login == User.Login))
  28. {
  29. MessageBox.Show("Такой логин уже занят");
  30. return;
  31. }
  32. if (ctx.Users.Any(u => u.Phone == User.Phone))
  33. {
  34. MessageBox.Show("Такой номер телефона уже занят");
  35. return;
  36. }
  37. ctx.Users.Add(User);
  38. ctx.SaveChanges();
  39. MainWindow mainWindow = new MainWindow();
  40. MessageBox.Show(mainWindow, "Вы успешно зарегистрировались");
  41. mainWindow.Show();
  42. AppHelper.GetWindowAtType<RegistrationWindow>()?.Close();
  43. }
  44. );
  45. }
  46. }
  47. public RelayCommand? _commandGoLogin;
  48. public RelayCommand? CommandGoLogin
  49. {
  50. get
  51. {
  52. return _commandGoLogin ??= new RelayCommand(
  53. x =>
  54. {
  55. new MainWindow().Show();
  56. AppHelper.GetWindowAtType<RegistrationWindow>()?.Close();
  57. });
  58. }
  59. }
  60. }
  61. }