RegistrationPageViewModel - Копировать.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using Avalonia.Controls;
  2. using AvaloniaApplication1.Models;
  3. using AvaloniaApplication1.Views;
  4. using ReactiveUI;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Reactive;
  9. using System.Reactive.Linq;
  10. namespace AvaloniaApplication1.ViewModels
  11. {
  12. partial class RegistrationPageViewModel : ViewModelBase
  13. {
  14. string surname = "";
  15. string name = "";
  16. string patronymic= "";
  17. DateTime dateOfBirth = DateTime.UtcNow;
  18. List<Gender> genderList;
  19. List<User> users;
  20. Gender selectedGender;
  21. string login = "";
  22. string info;
  23. string password = "";
  24. string repeatedPassword = "";
  25. public string Login { get => login; set => this.RaiseAndSetIfChanged(ref login, value); }
  26. public string Password { get => password; set => this.RaiseAndSetIfChanged(ref password, value); }
  27. public string Info { get => info; set => this.RaiseAndSetIfChanged(ref info, value); }
  28. public string RepeatedPassword { get => repeatedPassword; set => this.RaiseAndSetIfChanged(ref repeatedPassword, value); }
  29. public string Name { get => name; set => this.RaiseAndSetIfChanged(ref name, value); }
  30. public string Surname { get => surname; set => this.RaiseAndSetIfChanged(ref surname, value); }
  31. public string Patronymic { get => patronymic; set => this.RaiseAndSetIfChanged(ref patronymic, value); }
  32. public DateTime DateOfBirth { get => dateOfBirth; set => this.RaiseAndSetIfChanged(ref dateOfBirth, value); }
  33. public List<Gender> GenderList { get => genderList; set => this.RaiseAndSetIfChanged(ref genderList, value); }
  34. public List<User> Users { get => users; set => this.RaiseAndSetIfChanged(ref users, value); }
  35. public Gender SelectedGender { get => selectedGender; set => this.RaiseAndSetIfChanged(ref selectedGender, value); }
  36. public RegistrationPageViewModel()
  37. {
  38. genderList = myConnection.Genders.ToList();
  39. genderList.Add(new Gender() { Id = 0, Title = "Выберете пол"});
  40. selectedGender = genderList.FirstOrDefault(x => x.Id == 0);
  41. }
  42. public DateTimeOffset DateTimeOffset
  43. {
  44. get => new DateTimeOffset(dateOfBirth, TimeSpan.Zero);
  45. set => dateOfBirth = new DateTime(value.Year, value.Month, value.Day);
  46. }
  47. public void Registration()
  48. {
  49. if (Password != RepeatedPassword)
  50. {
  51. Info = "Пароли отличаются";
  52. }
  53. else if(Password == "" || RepeatedPassword == "" || Login == "" || Name == "" || Surname =="" || SelectedGender == genderList.FirstOrDefault(x => x.Id == 0))
  54. {
  55. Info = "Заполните все поля";
  56. }
  57. else {
  58. try {
  59. Users = myConnection.Users.Where(x => x.Login == Login).ToList();
  60. if (Users.Count > 0)
  61. {
  62. Info = "Логин занят";
  63. }
  64. else
  65. {
  66. User user = new User() { FirstName = Name, LastName = Surname, Patronymic = Patronymic, DateOfBirth = DateOfBirth, Gender = SelectedGender.Id, IdRole = 1, Login = Login, Password = Password };
  67. myConnection.Users.Add(user);
  68. myConnection.SaveChanges();
  69. MainWindowViewModel.Self.Uc = new UserPage();
  70. }
  71. }
  72. catch (Exception ex)
  73. {
  74. Info = ex.Message;
  75. }
  76. }
  77. }
  78. }
  79. }