/// <summary> /// This method does all the things necessary to handle a offensive answer given by a certain user /// --> Answer is added to bad_answers table /// </summary> /// <param name="newAnswerNonsenseCheck"></param> public static void ProcessOffensiveAnswer(NewAnswerOffenseCheck newAnswerOffenseCheck) { DBManager manager = new DBManager(true); /****/ String sqlSafeAnswer = ServerUtilities.UserInputToSQLSafe(newAnswerOffenseCheck.answer); /****/ // Add a reference to the answer in the bad_answer table StringBuilder sb = new StringBuilder(); sb.Append("INSERT INTO dbo.BadAnswers (bad_answer, question_id, answer_author_id) "); sb.Append($"VALUES ('{sqlSafeAnswer}', {newAnswerOffenseCheck.question_id}, '{newAnswerOffenseCheck.user_id}') "); String sqlCommand = sb.ToString(); manager.Read(sqlCommand); manager.Close(); }
/// <summary> /// This method gets called when there is a new answer to store in the database. /// </summary> /// <param name="answer">The new answer to store.</param> /// <returns>The new unique ID of the answer to store or -1 of the program was unable to add the /// id to the database.</returns> private static int assignAnswerIdToNewAnswer(string answer, string user_id) { int res = -1; if (String.IsNullOrEmpty(answer)) { return(res); } DBManager manager = new DBManager(true); /****/ String sqlSafeAnswer = ServerUtilities.UserInputToSQLSafe(answer); /****/ StringBuilder sb = new StringBuilder(); sb.Append("INSERT INTO dbo.Answers (answer, user_id) "); sb.Append($"VALUES ('{sqlSafeAnswer}', '{user_id}'); "); String sqlCommand = sb.ToString(); manager.Read(sqlCommand); sb = new StringBuilder(); sb.Append("SELECT answer_id "); sb.Append("FROM dbo.Answers "); sb.Append($"WHERE answer = '{sqlSafeAnswer}'; "); 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 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); }