TaskWIndowVM.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Collections.ObjectModel;
  7. using System.Windows;
  8. namespace Task1.ViewModel
  9. {
  10. class TaskWIndowVM : BaseViewModel
  11. {
  12. private ObservableCollection<Task> _tasks;
  13. private Task _task;
  14. private ObservableCollection<User> _users;
  15. private User _user;
  16. private string _LoginUser;
  17. private RelayCommand _poisk;
  18. private RelayCommand _change;
  19. private RelayCommand _end;
  20. private RelayCommand _update;
  21. private RelayCommand _dostup;
  22. private RelayCommand _prinat;
  23. private RelayCommand _nazad;
  24. public RelayCommand Nazad
  25. {
  26. get
  27. {
  28. return _nazad ??
  29. (_nazad = new RelayCommand((x) =>
  30. {
  31. new UserWindow().Show();
  32. Helper.CloseWindowsAtType<TaskWindow>();
  33. }));
  34. }
  35. }
  36. public RelayCommand Poisk
  37. {
  38. get => _poisk ??
  39. (_poisk = new RelayCommand((x) =>
  40. {
  41. TaskContext taskContext = new TaskContext();
  42. Tasks = new ObservableCollection<Task>(taskContext.Tasks.Where(a => a.UserCreatedTask.Login == LoginUser));
  43. }));
  44. }
  45. public RelayCommand Dostup
  46. {
  47. get => _dostup ??
  48. (_dostup = new RelayCommand((x) =>
  49. {
  50. TaskContext taskContext = new TaskContext();
  51. Tasks = new ObservableCollection<Task>(taskContext.Tasks.Where(a => a.StatusTaskId == 1));
  52. }));
  53. }
  54. public RelayCommand Update
  55. {
  56. get => _update ??
  57. (_update = new RelayCommand((x) =>
  58. {
  59. TaskContext taskContext = new TaskContext();
  60. Tasks = new ObservableCollection<Task>(taskContext.Tasks);
  61. }));
  62. }
  63. public RelayCommand End
  64. {
  65. get => _end ??
  66. (_end = new RelayCommand((x) =>
  67. {
  68. TaskContext taskContext = new TaskContext();
  69. Tasks = new ObservableCollection<Task>(taskContext.Tasks.Where(a => a.StatusTaskId == 3));
  70. }));
  71. }
  72. public RelayCommand Prinat
  73. {
  74. get => _prinat ??
  75. (_prinat = new RelayCommand((x) =>
  76. {
  77. if(x is Task task)
  78. {
  79. if(task.StatusTaskId == 1)
  80. {
  81. TaskContext taskContext = new TaskContext();
  82. task.UserAcceptedTaskId = User.AUser.UserId;
  83. taskContext.Entry(task).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
  84. task.StatusTaskId = 2;
  85. task.StatusTask1 = taskContext.StatusTasks.Find(2);
  86. taskContext.Tasks.Update(task);
  87. taskContext.SaveChanges();
  88. }
  89. else
  90. {
  91. MessageBox.Show("Задача занята");
  92. }
  93. }
  94. }));
  95. }
  96. public RelayCommand Change
  97. {
  98. get => _change ??
  99. (_change = new RelayCommand((x) =>
  100. {
  101. if (x is Task task)
  102. {
  103. if (task.UserCreatedTaskId == User.AUser.UserId)
  104. {
  105. TaskContext taskContext = new TaskContext();
  106. taskContext.Entry(task).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
  107. if (task.StatusTaskId == 1)
  108. {
  109. task.StatusTaskId = 2;
  110. task.StatusTask1 = taskContext.StatusTasks.Find(2);
  111. taskContext.Tasks.Update(task);
  112. taskContext.SaveChanges();
  113. }
  114. else if (task.StatusTaskId == 2)
  115. {
  116. task.StatusTaskId = 3;
  117. task.StatusTask1 = taskContext.StatusTasks.Find(3);
  118. taskContext.Tasks.Update(task);
  119. taskContext.SaveChanges();
  120. }
  121. else if (task.StatusTaskId == 3)
  122. {
  123. task.StatusTaskId = 1;
  124. task.StatusTask1 = taskContext.StatusTasks.Find(1);
  125. taskContext.Tasks.Update(task);
  126. taskContext.SaveChanges();
  127. }
  128. }
  129. else
  130. {
  131. MessageBox.Show("Задача пренадлежит не вам");
  132. }
  133. }
  134. }));
  135. }
  136. public string LoginUser
  137. {
  138. get => _LoginUser;
  139. set
  140. {
  141. _LoginUser = value;
  142. OnPropertyChanged();
  143. }
  144. }
  145. public ObservableCollection<Task> Tasks
  146. {
  147. get => _tasks;
  148. set
  149. {
  150. _tasks = value;
  151. OnPropertyChanged();
  152. }
  153. }
  154. public Task Task
  155. {
  156. get => _task;
  157. set
  158. {
  159. _task = value;
  160. OnPropertyChanged();
  161. }
  162. }
  163. public ObservableCollection<User> Users
  164. {
  165. get => _users;
  166. set
  167. {
  168. _users = value;
  169. OnPropertyChanged();
  170. }
  171. }
  172. public User User
  173. {
  174. get => _user;
  175. set
  176. {
  177. _user = value;
  178. OnPropertyChanged();
  179. }
  180. }
  181. public TaskWIndowVM()
  182. {
  183. TaskContext taskContext = new TaskContext();
  184. _tasks = new ObservableCollection<Task>(taskContext.Tasks);
  185. _task = new Task();
  186. _users = new ObservableCollection<User>(taskContext.Users);
  187. _user = new User();
  188. }
  189. }
  190. }