AddEditPage.xaml.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. using System.Linq.Expressions;
  16. using System.Data.Entity.Migrations;
  17. using Esoft;
  18. namespace Esoft.Pages
  19. {
  20. /// <summary>
  21. /// Логика взаимодействия для AddEditPage.xaml
  22. /// </summary>
  23. public partial class AddEditPage : Page
  24. {
  25. private user6Entities _context = new user6Entities();
  26. private Task _currentTask = null;
  27. public AddEditPage()
  28. {
  29. InitializeComponent();
  30. DataContext = _currentTask;
  31. CmbStatus.ItemsSource = Status.FillStatus();
  32. CmbTaskType.ItemsSource = WorkType.FillWorkType();
  33. CmbExecutor.ItemsSource = _context.Executor.ToList();
  34. CmbExecutor.ItemsSource = _context.User.Where(u => u.Executor != null).ToList();
  35. }
  36. public AddEditPage(Task selectedTask)
  37. {
  38. InitializeComponent();
  39. _currentTask = selectedTask;
  40. CmbStatus.ItemsSource = Status.FillStatus();
  41. CmbExecutor.ItemsSource = _context.Executor.ToList();
  42. CmbExecutor.ItemsSource = _context.User.Where(u => u.Executor != null).ToList();
  43. CmbTaskType.ItemsSource = WorkType.FillWorkType();
  44. if (selectedTask != null)
  45. _currentTask = selectedTask;
  46. DataContext = _currentTask;
  47. }
  48. private void BtnCancel_Click(object sender, RoutedEventArgs e)
  49. {
  50. NavigationService.GoBack();
  51. }
  52. private string CheckErrors()
  53. {
  54. var errorBuilder = new StringBuilder();
  55. if (string.IsNullOrWhiteSpace(TaskNameTB.Text))
  56. errorBuilder.AppendLine("Укажите название заголовка!");
  57. if (CmbStatus.Text == null)
  58. errorBuilder.AppendLine("Выберите статус!");
  59. if (CmbTaskType.Text == null)
  60. errorBuilder.AppendLine("Выберите характеристику!");
  61. if (errorBuilder.Length > 0) //проверка на ошибку
  62. {
  63. MessageBox.Show(errorBuilder.ToString());
  64. return errorBuilder.ToString();
  65. }
  66. else return null;
  67. }
  68. private void BtnSaveTask_Click(object sender, RoutedEventArgs e)
  69. {
  70. if (CheckErrors() == null)
  71. {
  72. if (_currentTask == null)
  73. {
  74. try
  75. {
  76. var selectedTask = new Task
  77. {
  78. //ExecutorID = int.Parse(CmbExecutor.SelectedItem.ToString()),
  79. ExecutorID = (CmbExecutor.SelectedItem as User)?.ID ?? 0,
  80. //ExecutorID =int.Parse(CmbExecutor.Text),
  81. Title = TaskNameTB.Text,
  82. Description = TaskDescriptionTB.Text,
  83. CreateDateTime = DateTime.Parse(DPCreateDateTime.Text),
  84. Deadline = DateTime.Parse(DPDeadLine.Text),
  85. Difficulty = Double.Parse(DifficultyTB.Text),
  86. Time = int.Parse(TimeTB.Text),
  87. Status = CmbStatus.Text,
  88. WorkType = CmbTaskType.Text,
  89. IsDeleted = false
  90. };
  91. _context.Task.Add(selectedTask);
  92. _context.SaveChanges();
  93. MessageBox.Show("Задача добавлена");
  94. NavigationService.GoBack();
  95. }
  96. catch
  97. {
  98. MessageBox.Show(CheckErrors(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  99. }
  100. }
  101. else
  102. {
  103. var context = _context;
  104. context.SaveChanges();
  105. MessageBox.Show("Задача отредактирована");
  106. NavigationService.GoBack();
  107. }
  108. }
  109. }
  110. private void DifficultyTB_TextInput(object sender, TextCompositionEventArgs e)
  111. {
  112. if (!char.IsDigit(e.Text, e.Text.Length - 1))
  113. {
  114. e.Handled = true;
  115. }
  116. }
  117. private void DifficultyTB_TextChanged(object sender, TextChangedEventArgs e)
  118. {
  119. if (sender is TextBox diffTB)
  120. {
  121. if (string.IsNullOrEmpty(diffTB.Text)) { return; }
  122. if (!int.TryParse(diffTB.Text, out int number) || number < 1 || number > 50)
  123. {
  124. DifficultyTB.Clear();
  125. MessageBox.Show("Пожалуйста, введите число от 1 до 50.");
  126. }
  127. }
  128. }
  129. }
  130. }