throw new Exception("Failed to find your API key!");
}
else {
// Set up your Database Tools
DatabaseTools myTools = new DatabaseTools();
// Start by confirming / finding the user
boolean userFound = myTools.dataSearch("User", "id", userId, "password", password);
if (!userFound) {
throw new Exception ("User not found");
}
else {
// next confirm the group exists
boolean groupFound = myTools.dataSearch("Group", "id", groupId);
if (!groupFound){
throw new Exception ("Group not found");
}
//group exists
else {
//set up db tools to search for a user within a group
DatabasePro<UserRoleGroup, Integer, Integer, String> myProTools = new DatabasePro<>();
//returns a list searching for a user in a group. size will be either 1 or 0 depending on if they were found
List<UserRoleGroup> userRoleGroupList = myProTools.getTypeList(Table.UserRoleGroup, Column.groupId, groupId, Column.userId, userId);
//user was not found to be part of the group
if (userRoleGroupList.size() == 0){
throw new Exception("User is not a part of the group and can not add questions");
}
//user is part of the group
else {
Question questionObj = QuestionFactory.createQuestionTF(userId, groupId, question);
//add question to the database
int newQuestionID = myTools.dataEntry(questionObj);
// The logic to decide whether the correct answer is "True" or "False"
Answer trueAnswer, falseAnswer;
// If the answer is true then "True" is true. If false, then "True" is false.
trueAnswer = QuestionFactory.createAnswer("True", 1, newQuestionID, answer);
falseAnswer = QuestionFactory.createAnswer("False", 2, newQuestionID, !answer);
// Now adding the answers to the database
int trueAnswerId = myTools.dataEntry(trueAnswer);
int falseAnswerId = myTools.dataEntry(falseAnswer);
// Now check that the question is correctly stored in the database
boolean questionFound = myTools.dataSearch("Question", "id", newQuestionID);
// Now check that the answers are correctly stored in the database
boolean trueAnswerFound = myTools.dataSearch("Answer", "id", trueAnswerId);
boolean falseAnswerFound = myTools.dataSearch("Answer", "id", falseAnswerId);
// Now check, if not all the database checks are true, return false status
if(!(questionFound && trueAnswerFound && falseAnswerFound)){
return new QuestionStatusJson(false);
}