/// <summary> /// Delete a task (and its decomposition) from the database /// </summary> /// <param name="t"></param> /// <param name="ac"></param> /// <returns></returns> public (bool Succeeded, string ErrorMessage) DeleteTask(Task t, AuthorizationCookie ac) { if (!checkValidAuthCookie(t.UserID, ac)) { return(false, "Invalid login!"); } const string query = @"DELETE FROM Task WHERE TaskID = @id"; SQLiteCommand cmd = new SQLiteCommand(query, connection); cmd.Parameters.AddWithValue("@id", t.TaskID); SQLiteDataReader sdr = cmd.ExecuteReader(); deleteGraph(t.GraphID); if (sdr.RecordsAffected == 1) { return(true, "Successfully deleted your task!"); } return(false, "Failed to delete the task"); }
/// <summary> /// Login the user with the username and password /// </summary> /// <param name="username"></param> /// <param name="password"></param> /// <returns>The User and an accompanying Authorization Cookie</returns> public (User user, AuthorizationCookie?ac) GetUser(string username, string password) { //scrub the username username = username.ToLowerInvariant().Trim(); const string query = @"SELECT * FROM UserTable WHERE Username = @user AND HashedPassword = @hash"; SQLiteCommand cmd = new SQLiteCommand(query, connection); cmd.Parameters.AddWithValue("@user", username); //use the encryption here to verify the password cmd.Parameters.AddWithValue("@hash", Encrypter.EncryptStringToBytes_Aes(password)); SQLiteDataReader sdr = cmd.ExecuteReader(); //Failed to log the user in if (!sdr.HasRows) { return(null, null); } sdr.Read(); User user = new User(sdr.GetInt32(0), "", sdr.GetString(2), sdr.GetString(3), sdr.GetString(4), sdr.GetString(5)); AuthorizationCookie ac = getAuthCookie(); activeLogins.Add(user.UserID, ac); //retrieve the tasks for the user getTasks(user); return(user, ac); }
/// <summary> /// Updates the completion status of a given node /// </summary> /// <param name="ac"></param> /// <param name="userID"></param> /// <param name="nodeID"></param> /// <param name="complete"></param> /// <returns></returns> public (bool Succeeded, string ErrorMessage) UpdateComplete(AuthorizationCookie ac, int userID, int nodeID, bool complete) { if (!checkValidAuthCookie(userID, ac)) { return(false, "Invalid login!"); } const string query = @"UPDATE NODE SET Complete = @complete WHERE NodeID = @id"; SQLiteCommand cmd = new SQLiteCommand(query, connection); cmd.Parameters.AddWithValue("@id", nodeID); cmd.Parameters.AddWithValue("@complete", complete ? 1 : 0); SQLiteDataReader sdr = cmd.ExecuteReader(); if (sdr.RecordsAffected == 1) { return(true, "Successfully updated value."); } return(false, "Failed to update nodes"); }
/// <summary> /// Write a task into the database. Returns a new task with the decomposition. /// </summary> /// <param name="task"></param> /// <param name="ac"></param> /// <returns></returns> public (bool Succeeded, string FailureString, Task fullTask) WriteTask(Task task, AuthorizationCookie ac) { if (!checkValidAuthCookie(task.UserID, ac)) { return(false, "Invalid login!", null); } string query = @"INSERT INTO Task (AssignmentName, GraphID, SubmissionDate, Category, UserID) VALUES (@AssignmentName, @GraphID, @SubmissionDate, @Category, @UserID)"; //Retrieve the last graph ID and increment by 1. int graphID = getID("Graph") + 1; //create a graph first... Graph new_graph = new Graph(); //some pre-defined scenarios... if (task.Category.Equals("Research Paper")) { //vertex set Node header = new Node(task.Category, 0, false, task.SubmissionDate, "", graphID, 0); Node n1 = new Node("Collect Sources", 0, false, task.SubmissionDate.AddDays(-11), "Research", graphID, 1); Node n2 = new Node("Write Thesis", 0, false, task.SubmissionDate.AddDays(-10), "", graphID, 2); Node n3 = new Node("Introduction", 0, false, task.SubmissionDate.AddDays(-9), "", graphID, 3); Node n4 = new Node("Body Paragraph 1", 0, false, task.SubmissionDate.AddDays(-7), "", graphID, 4); Node n5 = new Node("Body Paragraph 2", 0, false, task.SubmissionDate.AddDays(-5), "", graphID, 5); Node n6 = new Node("Body Paragraph 3", 0, false, task.SubmissionDate.AddDays(-3), "", graphID, 6); Node n7 = new Node("Conclusion", 0, false, task.SubmissionDate.AddDays(-2), "", graphID, 7); Node n8 = new Node("Citations", 0, false, task.SubmissionDate.AddDays(-1), "", graphID, 8); new_graph.AddNodes(header, n1, n2, n3, n4, n5, n6, n7, n8); //edge set new_graph.CreateEdge(header, n1); new_graph.CreateEdge(header, n2); new_graph.CreateEdge(header, n3); new_graph.CreateEdge(header, n4); new_graph.CreateEdge(header, n5); new_graph.CreateEdge(header, n6); new_graph.CreateEdge(header, n7); new_graph.CreateEdge(header, n8); //write the graph. writeGraph(new_graph); } else if (task.Category.Equals("Agile Software Project")) { //vertex set Node header = new Node(task.Category, 0, false, task.SubmissionDate, "", graphID, 0); Node n1 = new Node("Sprint 1", 0, false, task.SubmissionDate.AddDays(-13), "Deliverable", graphID, 1); Node n1_1 = new Node("Create Wireframe", 0, false, task.SubmissionDate.AddDays(-14), "Lucidchart", graphID, 2); Node n2 = new Node("Sprint 2", 0, false, task.SubmissionDate.AddDays(-6), "Deliverable", graphID, 3); Node n2_1 = new Node("Create SQLite Database", 0, false, task.SubmissionDate.AddDays(-11), "", graphID, 4); Node n2_2 = new Node("Write Web API", 0, false, task.SubmissionDate.AddDays(-9), "REST API", graphID, 5); Node n2_3 = new Node("Code Review", 0, false, task.SubmissionDate.AddDays(-7), "Pair Programming", graphID, 6); Node n3 = new Node("Sprint 3", 0, false, task.SubmissionDate, "Deliverable", graphID, 7); Node n3_1 = new Node("Unit Test", 0, false, task.SubmissionDate.AddDays(-2), "Ryan and Kyle", graphID, 8); Node n3_2 = new Node("Release Version 1", 0, false, task.SubmissionDate.AddDays(-1), "GitHub Release", graphID, 9); new_graph.AddNodes(header, n1, n1_1, n2, n2_1, n2_2, n2_3, n3, n3_1, n3_2); //edge set new_graph.CreateEdge(header, n1); new_graph.CreateEdge(header, n2); new_graph.CreateEdge(header, n3); new_graph.CreateEdge(n1, n1_1); new_graph.CreateEdge(n2, n2_1); new_graph.CreateEdge(n2, n2_2); new_graph.CreateEdge(n2, n2_3); new_graph.CreateEdge(n3, n3_1); new_graph.CreateEdge(n3, n3_2); //write the graph. writeGraph(new_graph); } //Write all the new nodes for (int i = 0; i < new_graph.Nodes.Count; i++) { Node n = new_graph.Nodes[i]; (bool Succeeded, _, Node UpdatedNode) = writeNode(n); //make sure to pass the updated node with the correct ID information. if (Succeeded && UpdatedNode != null) { new_graph.Nodes[i] = UpdatedNode; } } SQLiteCommand myCommand = new SQLiteCommand(query, connection); myCommand.Parameters.AddWithValue("@AssignmentName", task.AssignmentName); myCommand.Parameters.AddWithValue("@GraphID", graphID); myCommand.Parameters.AddWithValue("@SubmissionDate", task.SubmissionDate); myCommand.Parameters.AddWithValue("@Category", task.Category); myCommand.Parameters.AddWithValue("@UserID", task.UserID); int rowsUpdated = myCommand.ExecuteNonQuery(); myCommand.Dispose(); if (rowsUpdated == 0) { return(false, "Failed", null); } var id = getID("Task"); //generate an updated task with this decomposition info to return to the user Task return_task = new Task(id, task.AssignmentName, graphID, task.SubmissionDate, task.Category, task.UserID); return_task.AddGraph(new_graph); return(true, "Success", return_task); }
/// <summary> /// Checks whether or not the provided auth cookie is valid for the given user id. /// </summary> /// <param name="UserID"></param> /// <param name="ac"></param> /// <returns></returns> private bool checkValidAuthCookie(int UserID, AuthorizationCookie ac) => activeLogins.ContainsKey(UserID) && activeLogins[UserID].GetBitString().Equals(ac.GetBitString());