NewEmploeeViewModel.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System;
  2. using Prism.Mvvm;
  3. using Prism.Commands;
  4. using System.Collections.ObjectModel;
  5. using WpfAppUI.Model;
  6. using System.Windows;
  7. namespace WpfAppUI.ViewModel
  8. {
  9. internal class NewEmploeeViewModel : BindableBase
  10. {
  11. #region Поля
  12. private Window myWindow;
  13. private string _surname;
  14. private string _name;
  15. private string _patronymic;
  16. private DateTime _birthday;
  17. private string _phone;
  18. private string _mail;
  19. private Post _post;
  20. private DelegateCommand _create;
  21. #endregion
  22. #region Свойства
  23. public string surnameInput { get => _surname; set => _surname = value; }
  24. public string nameInput { get => _name; set => _name = value; }
  25. public string patronymicInput { get => _patronymic; set => _patronymic = value; }
  26. public DateTime birthdayInput { get => _birthday; set => _birthday = (DateTime)value; }
  27. public string phoneInput { get => _phone; set => _phone = value; }
  28. public string mailInput { get => _mail; set => _mail = value; }
  29. public Post SelectedPost { get => _post; set => _post = value; }
  30. public ObservableCollection<Post> Posts { get; set; }
  31. public DelegateCommand Create { get => _create; }
  32. #endregion
  33. #region Конструктор
  34. public NewEmploeeViewModel(Window window)
  35. {
  36. Posts = new ObservableCollection<Post>();
  37. PostDataUpdate();
  38. this.myWindow = window;
  39. _create = new DelegateCommand(CreateRealization);
  40. }
  41. #endregion
  42. #region Методы
  43. private void PostDataUpdate()
  44. {
  45. var post = DBRequests.PostRequest();
  46. foreach (var item in post)
  47. {
  48. Posts.Add(item);
  49. }
  50. }
  51. #endregion
  52. #region Реализация методов для связей
  53. private void CreateRealization()
  54. {
  55. if(Emploee.RegexCheckEmail(mailInput))
  56. {
  57. if (!string.IsNullOrWhiteSpace(surnameInput) && !string.IsNullOrWhiteSpace(nameInput) && birthdayInput != DateTime.MinValue ||
  58. !string.IsNullOrWhiteSpace(phoneInput) && !string.IsNullOrWhiteSpace(mailInput) && SelectedPost != null)
  59. {
  60. if (DBRequests.AddNewEmploee(surnameInput, nameInput, SelectedPost, birthdayInput, phoneInput, mailInput, patronymicInput))
  61. {
  62. MessageBox.Show("Сотрудник создан успешно", "Создано");
  63. myWindow.Close();
  64. }
  65. else
  66. MessageBox.Show("Произошла непредвиденная ошибка", "Ошибка");
  67. }
  68. }
  69. else
  70. MessageBox.Show("Неправильно заполнена почта", "Ошибка");
  71. }
  72. #endregion
  73. }
  74. }