TasksCod.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using Rkis29.ViewModel;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Rkis29.Command;
  8. using Rkis29.Windows;
  9. using System.Collections.ObjectModel;
  10. using System.Windows;
  11. namespace Rkis29.Command
  12. {
  13. public class TasksCod : BaseViewModel
  14. {
  15. private RelayCommand _YourFind;
  16. private RelayCommand _StatusUpdate;
  17. private RelayCommand _TakeTask;
  18. private RelayCommand _HistoryTask;
  19. private RelayCommand _FreeTask;
  20. private ObservableCollection<Task> _tasks = new ObservableCollection<Task>();
  21. private Task _task;
  22. private int _count = 0;
  23. private int _count1 = 0;
  24. private int _count2 = 0;
  25. private string _YourLogin;
  26. public RelayCommand YourFind
  27. {
  28. get => _YourFind ??
  29. (_YourFind = new RelayCommand((x) =>
  30. {
  31. _count2++;
  32. loferlContext loferlcontext = new loferlContext();
  33. Tasks = new ObservableCollection<Task>(loferlcontext.Tasks.Where(p => p.UserCreater.Login == YourLogin));
  34. if (_count2 % 2 == 0)
  35. {
  36. Tasks = new ObservableCollection<Task>(loferlcontext.Tasks);
  37. }
  38. }));
  39. }
  40. public RelayCommand FreeTask
  41. {
  42. get => _FreeTask ??
  43. (_FreeTask = new RelayCommand((x) =>
  44. {
  45. _count1++;
  46. loferlContext loferlcontext = new loferlContext();
  47. Tasks = new ObservableCollection<Task>(loferlcontext.Tasks.Where(p => p.StatusTaskId == 1));
  48. if (_count1 % 2 == 0)
  49. {
  50. Tasks = new ObservableCollection<Task>(loferlcontext.Tasks);
  51. }
  52. }));
  53. }
  54. public RelayCommand HistoryTasks
  55. {
  56. get => _HistoryTask ??
  57. (_HistoryTask = new RelayCommand((x) =>
  58. {
  59. _count++;
  60. loferlContext loferlcontext = new loferlContext();
  61. Tasks = new ObservableCollection<Task>(loferlcontext.Tasks.Where(p => p.StatusTaskId == 3));
  62. if (_count % 2 == 0)
  63. {
  64. Tasks = new ObservableCollection<Task>(loferlcontext.Tasks);
  65. }
  66. }));
  67. }
  68. public RelayCommand TakeTask
  69. {
  70. get => _TakeTask ??
  71. (_TakeTask = new RelayCommand((x) =>
  72. {
  73. if (x is Task task)
  74. {
  75. if (task.StatusTaskId == 1)
  76. {
  77. task.UserTakerId = User.AutoUser.UsersId;
  78. task.StatusTaskId = 2;
  79. loferlContext loferlcontext = new loferlContext();
  80. loferlcontext.Entry(task).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
  81. task.StatusTask = loferlcontext.Statuses.Find(2);
  82. loferlcontext.Tasks.Update(task);
  83. loferlcontext.SaveChanges();
  84. Tasks = new ObservableCollection<Task>(loferlcontext.Tasks);
  85. }
  86. }
  87. else
  88. {
  89. MessageBox.Show("The task is already taken, choose another one.");
  90. }
  91. }));
  92. }
  93. public RelayCommand StatusUpdate
  94. {
  95. get => _StatusUpdate ??
  96. (_StatusUpdate = new RelayCommand((x) =>
  97. {
  98. if (x is Task task)
  99. {
  100. if (task.UserCreaterId == User.AutoUser.UsersId)
  101. {
  102. loferlContext loferlcontext = new loferlContext();
  103. loferlcontext.Entry(task).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
  104. if (task.StatusTaskId == 1)
  105. {
  106. task.StatusTaskId = 2;
  107. task.StatusTask = loferlcontext.Statuses.Find(2);
  108. loferlcontext.Tasks.Update(task);
  109. loferlcontext.SaveChanges();
  110. }
  111. else if (task.StatusTaskId == 2)
  112. {
  113. task.StatusTaskId = 3;
  114. task.StatusTask = loferlcontext.Statuses.Find(3);
  115. loferlcontext.Tasks.Update(task);
  116. loferlcontext.SaveChanges();
  117. }
  118. else if (task.StatusTaskId == 3)
  119. {
  120. task.StatusTaskId = 1;
  121. task.StatusTask = loferlcontext.Statuses.Find(1);
  122. loferlcontext.Tasks.Update(task);
  123. loferlcontext.SaveChanges();
  124. }
  125. }
  126. else
  127. {
  128. MessageBox.Show("You can only change your tasks.");
  129. }
  130. }
  131. }
  132. ));
  133. }
  134. public string YourLogin
  135. {
  136. get => _YourLogin;
  137. set
  138. {
  139. _YourLogin = value;
  140. OnPropertyChanged();
  141. }
  142. }
  143. public ObservableCollection<Task> Tasks
  144. {
  145. get => _tasks;
  146. set
  147. {
  148. _tasks = value;
  149. OnPropertyChanged();
  150. }
  151. }
  152. public Task Task
  153. {
  154. get => _task;
  155. set
  156. {
  157. _task = value;
  158. OnPropertyChanged();
  159. }
  160. }
  161. public TasksCod()
  162. {
  163. loferlContext loferlcontext = new loferlContext();
  164. _tasks = new ObservableCollection<Task>(loferlcontext.Tasks);
  165. _task = new Task();
  166. }
  167. }
  168. }