//Update or Save Task public void UpdateTask(DisposableTask disposableTask) { if (CheckTaskExistsInDB(disposableTask.getTaskId())) { MySqlConnection conn = null; try { conn = new MySqlConnection(connectionString); conn.Open(); MySqlCommand cmd = new MySqlCommand("UPDATE `Task` SET `title` = @title, `notes` = @notes," + "`allowNotifications` = @allowNotifications, `isComplete` = @isComplete, `isRepeatable` = @isRepeatable, `taskFKey` = @taskFKey" + " WHERE `taskId` = @taskId", conn); cmd.Parameters.AddWithValue("taskId", disposableTask.getTaskId()); cmd.Parameters.AddWithValue("title", disposableTask.getTitle()); cmd.Parameters.AddWithValue("notes", disposableTask.getDescription()); cmd.Parameters.AddWithValue("allowNotifications", disposableTask.getAllowNotifications()); cmd.Parameters.AddWithValue("isComplete", disposableTask.getIsComplete()); cmd.Parameters.AddWithValue("isRepeatable", false); cmd.Parameters.AddWithValue("taskFKey", disposableTask.getTaskFKey()); cmd.ExecuteNonQuery(); } catch (Exception ex) { Console.WriteLine("UpdateTask Failure!" + ex); } finally { if (conn != null) { conn.Close(); } } } }
//Fetch existing DT from Database based on taskId and update this instance with its info public DisposableTask FetchDisposableTask(int taskId) { MySqlConnection conn = null; DisposableTask myDisposableTask = new DisposableTask(); try { conn = new MySqlConnection(connectionString); conn.Open(); MySqlCommand cmd = new MySqlCommand("SELECT* from `Task` WHERE `TaskId` = @taskId", conn); cmd.Parameters.Add(new MySqlParameter("taskId", taskId)); MySqlDataReader reader = cmd.ExecuteReader(); reader.Read(); myDisposableTask.setTaskId((int)reader.GetValue(0)); //TaskId myDisposableTask.setTitle((String)reader.GetValue(1)); //Title myDisposableTask.setDescription((String)reader.GetValue(2)); //Notes myDisposableTask.setAllowNotifications((Boolean)reader.GetValue(3)); //allowNotifications myDisposableTask.setIsComplete((Boolean)reader.GetValue(4)); //isComplete myDisposableTask.setRepeatability((Boolean)reader.GetValue(5)); //isRepeatable myDisposableTask.setTaskFKey((int)reader.GetValue(6)); //taskFKey } catch (Exception ex) { Console.WriteLine("FetchDisposableTask Failure!" + ex); } finally { if (conn != null) { conn.Close(); } } myDisposableTask.setSubTasks(FetchAllSubTasks(taskId)); return(myDisposableTask); }
public List <Task> LoadList(int userId) { List <Task> taskList = new List <Task>(); MySqlConnection conn = null; MySqlCommand command = null; try { conn = new MySqlConnection(connectionString); conn.Open(); command = new MySqlCommand( "SELECT * FROM `Task` WHERE `taskFKey` = @userId;", conn ); command.Parameters.AddWithValue("@userId", userId); MySqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { int Rindex = 0; int Dindex = 0; if (reader.GetValue(5).Equals(1)) { RepeatableTask currentTask = new RepeatableTask((String)reader.GetValue(1), (String)reader.GetValue(3), new Dictionary <int, SubTask>(Rindex, reader.GetValue(4))); taskList.Add(currentTask); } else { DisposableTask currentTask = new DisposableTask((String)reader.GetValue(1), (String)reader.GetValue(3), new Dictionary <int, SubTask>(Dindex, reader.GetValue(4))); taskList.Add(currentTask); } Rindex++; Dindex++; } return(taskList); } catch (Exception ex) { Console.WriteLine("Something went horribly wrong! " + ex); return(new List <Task>()); } finally { if (conn != null) { conn.Close(); } } }
//if createTask is clicked private void backToDash(object sender, RoutedEventArgs e) { taskTitle = this.taskTitleText.Text; taskDesc = this.descText.Text; if (this.yesRadio.IsChecked == true) { this.repeatable = true; //create repeatableTask RepeatableTask rt = new RepeatableTask(taskTitle, taskDesc, dict); rt.setTaskFKey(user.UserId); rt.SaveTask(); //save task to db list.Add(rt); Dashboard dash = new Dashboard(user); NavigationService.Navigate(dash); } else if (this.noRadio.IsChecked == true) { repeatable = false; //create disposableTask DisposableTask dt = new DisposableTask(taskTitle, taskDesc, dict); dt.setTaskFKey(user.UserId); //dt.setTaskFKey(55); //TODO user.UserId does not return the correct ID dt.SaveTask(); //save task to db list.Add(dt); Dashboard dash = new Dashboard(user); NavigationService.Navigate(dash); } //NavigationService nav = NavigationService.GetNavigationService(this); // nav.Navigate(new Uri("/GUI/Dashboard.xaml", UriKind.RelativeOrAbsolute)); }
//Inserts the new row into the TASK table and returns the new Task ID it gets auto assigned public int InsertDisposableTask(DisposableTask disposableTask) { MySqlConnection conn = null; MySqlCommand command = null; try { conn = new MySqlConnection(connectionString); conn.Open(); command = new MySqlCommand("INSERT INTO `Task` (`title`,`notes`,`allowNotifications`, `isComplete`,`isRepeatable`, `taskFKey`) " + "VALUES(@title, @notes, @allowNotifications, @isComplete, @isRepeatable, @taskFKey);", conn); command.Parameters.AddWithValue("@title", disposableTask.getTitle()); command.Parameters.AddWithValue("@notes", disposableTask.getDescription()); command.Parameters.AddWithValue("@allowNotifications", disposableTask.getAllowNotifications()); command.Parameters.AddWithValue("@isComplete", disposableTask.getIsComplete()); command.Parameters.AddWithValue("@isRepeatable", 0); //0 for not repeatable command.Parameters.AddWithValue("@taskFKey", disposableTask.getTaskFKey()); command.ExecuteNonQuery(); } catch (Exception ex) { Console.WriteLine("InsertDisposableTask: Failure!" + ex); } finally { if (conn != null) { conn.Close(); } } int highestId = GetNextTaskId(); disposableTask.setTaskId(highestId); UpdateTask(disposableTask); return(highestId); }