/// <summary> /// This method gets called when the Server detects a new question, add this question to the database /// And generate a new UNIQUE id for this question and return it. /// </summary> /// <param name="openQuestion">The new question to store.</param> /// <returns>The new unique ID of the question to store or -1 of the program was unable to add the /// id to the database</returns> internal static int assignQuestionIdToNewQuestion(NewOpenQuestion openQuestion) { int res = -1; DBManager manager = new DBManager(true); /****/ String sqlSafeQuestion = ServerUtilities.UserInputToSQLSafe(openQuestion.question); /****/ StringBuilder sb = new StringBuilder(); sb.Append("INSERT INTO dbo.Questions (question, answer_id) "); sb.Append($"VALUES ('{sqlSafeQuestion}', NULL); "); String sqlCommand = sb.ToString(); manager.Read(sqlCommand); manager.Close(); manager = new DBManager(true); sb = new StringBuilder(); sb.Append("SELECT question_id "); sb.Append("FROM dbo.Questions "); sb.Append($"WHERE question = '{sqlSafeQuestion}'"); sqlCommand = sb.ToString(); var reader = manager.Read(sqlCommand); // Get the new unique id if (reader.Read()) // We only expect one result { res = reader.GetInt32(0); } manager.Close(); return(res); }
/// <summary> /// This function gets called when the server wants to add a new, unanswered question to the database. /// </summary> /// <param name="newQuestionNonsenseCheck">The model containing all the information about the question to add.</param> /// <returns>The id of the question that was just added.</returns> public static int SaveQuestionToDatabase(NewOpenQuestion serverData) { return(assignQuestionIdToNewQuestion(serverData)); }