using Rkis29.ViewModel; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Rkis29.Command; using Rkis29.Windows; using System.Collections.ObjectModel; using System.Windows; namespace Rkis29.Command { public class TasksCod : BaseViewModel { private RelayCommand _YourFind; private RelayCommand _StatusUpdate; private RelayCommand _TakeTask; private RelayCommand _HistoryTask; private RelayCommand _FreeTask; private ObservableCollection _tasks = new ObservableCollection(); private Task _task; private int _count = 0; private int _count1 = 0; private int _count2 = 0; private string _YourLogin; public RelayCommand YourFind { get => _YourFind ?? (_YourFind = new RelayCommand((x) => { _count2++; loferlContext loferlcontext = new loferlContext(); Tasks = new ObservableCollection(loferlcontext.Tasks.Where(p => p.UserCreater.Login == YourLogin)); if (_count2 % 2 == 0) { Tasks = new ObservableCollection(loferlcontext.Tasks); } })); } public RelayCommand FreeTask { get => _FreeTask ?? (_FreeTask = new RelayCommand((x) => { _count1++; loferlContext loferlcontext = new loferlContext(); Tasks = new ObservableCollection(loferlcontext.Tasks.Where(p => p.StatusTaskId == 1)); if (_count1 % 2 == 0) { Tasks = new ObservableCollection(loferlcontext.Tasks); } })); } public RelayCommand HistoryTasks { get => _HistoryTask ?? (_HistoryTask = new RelayCommand((x) => { _count++; loferlContext loferlcontext = new loferlContext(); Tasks = new ObservableCollection(loferlcontext.Tasks.Where(p => p.StatusTaskId == 3)); if (_count % 2 == 0) { Tasks = new ObservableCollection(loferlcontext.Tasks); } })); } public RelayCommand TakeTask { get => _TakeTask ?? (_TakeTask = new RelayCommand((x) => { if (x is Task task) { if (task.StatusTaskId == 1) { task.UserTakerId = User.AutoUser.UsersId; task.StatusTaskId = 2; loferlContext loferlcontext = new loferlContext(); loferlcontext.Entry(task).State = Microsoft.EntityFrameworkCore.EntityState.Modified; task.StatusTask = loferlcontext.Statuses.Find(2); loferlcontext.Tasks.Update(task); loferlcontext.SaveChanges(); Tasks = new ObservableCollection(loferlcontext.Tasks); } } else { MessageBox.Show("The task is already taken, choose another one."); } })); } public RelayCommand StatusUpdate { get => _StatusUpdate ?? (_StatusUpdate = new RelayCommand((x) => { if (x is Task task) { if (task.UserCreaterId == User.AutoUser.UsersId) { loferlContext loferlcontext = new loferlContext(); loferlcontext.Entry(task).State = Microsoft.EntityFrameworkCore.EntityState.Modified; if (task.StatusTaskId == 1) { task.StatusTaskId = 2; task.StatusTask = loferlcontext.Statuses.Find(2); loferlcontext.Tasks.Update(task); loferlcontext.SaveChanges(); } else if (task.StatusTaskId == 2) { task.StatusTaskId = 3; task.StatusTask = loferlcontext.Statuses.Find(3); loferlcontext.Tasks.Update(task); loferlcontext.SaveChanges(); } else if (task.StatusTaskId == 3) { task.StatusTaskId = 1; task.StatusTask = loferlcontext.Statuses.Find(1); loferlcontext.Tasks.Update(task); loferlcontext.SaveChanges(); } } else { MessageBox.Show("You can only change your tasks."); } } } )); } public string YourLogin { get => _YourLogin; set { _YourLogin = value; OnPropertyChanged(); } } public ObservableCollection Tasks { get => _tasks; set { _tasks = value; OnPropertyChanged(); } } public Task Task { get => _task; set { _task = value; OnPropertyChanged(); } } public TasksCod() { loferlContext loferlcontext = new loferlContext(); _tasks = new ObservableCollection(loferlcontext.Tasks); _task = new Task(); } } }