Example #1
0
        public string GetAllQuizzes()
        {
            var metaList = new List<QuizzMeta>();
            var quizzMeta = new Dictionary<string, List<QuizzMeta>> ();

            using (var dbWrapper = new DbWrapper("studybuddyquizzes"))
                {
                using (var command = new SQLiteCommand("SELECT QuizzID, QuizzName, Description FROM Quizzes"))
                    {
                    var reader = dbWrapper.ExecuteReader(command);
                    while (reader.Read())
                        {
                        var singleQuizzMeta = new QuizzMeta
                            {
                            id = reader.GetInt32(0),
                            name = reader.GetString(1),
                            description = reader.GetString(2)
                            };
                        metaList.Add(singleQuizzMeta);
                        }
                    }
                }
            quizzMeta.Add("quizzes", metaList);
            return JsonConvert.SerializeObject(quizzMeta);
        }
Example #2
0
        public string GetQuizz(int id)
        {
            var quizz = new Quizz();

            using (var dbWrapper = new DbWrapper("studybuddyquizzes"))
                {
                using (var command = new SQLiteCommand("SELECT QuizzName, Description FROM Quizzes WHERE QuizzID = @id")
                    )
                    {
                    command.Parameters.AddWithValue("@id", id);
                    var reader = dbWrapper.ExecuteReader(command);
                    while (reader.Read())
                        {
                        quizz.name = reader.GetString(0);
                        quizz.description = reader.GetString(1);
                        }
                    }
                }
            using (var dbWrapper = new DbWrapper ("studybuddyquizzes"))
                {
                using (var command = new SQLiteCommand ("SELECT QuestionID, Question FROM Questions WHERE QuizzID = @id"))
                    {
                    var questionId = 0;
                    var questions = new List<Quizz.Questions>();
                    command.Parameters.AddWithValue ("@id", id);
                    var reader = dbWrapper.ExecuteReader (command);
                    while (reader.Read ())
                        {
                        var singleQuestion = new Quizz.Questions();
                        var correctAnswersList = new List<int>();
                        questionId = reader.GetInt32 (0);
                        singleQuestion.question = reader.GetString (1);
                        var options = new List<Quizz.Options> ();
                        using (var internalWrapper = new DbWrapper("studybuddyquizzes"))
                            {
                            using (
                                var innerCommand =
                                    new SQLiteCommand(
                                        "SELECT AnswerID, Answer, Is_Correct FROM Answers WHERE QuestionID = @QId"))
                                {

                                innerCommand.Parameters.AddWithValue("@QId", questionId);
                                var innerReader = internalWrapper.ExecuteReader(innerCommand);
                                while (innerReader.Read())
                                    {
                                    var option = new Quizz.Options ();
                                    option.id = innerReader.GetInt32(0);
                                    option.answer = innerReader.GetString(1);
                                    if (innerReader.GetInt32(2) == 1)
                                        {
                                        correctAnswersList.Add(option.id);
                                        }
                                    options.Add(option);
                                    }
                                }
                            }
                        singleQuestion.options = options.ToArray();
                        singleQuestion.rightAnswerIds = correctAnswersList.ToArray();
                        questions.Add(singleQuestion);
                        }
                    quizz.questions = questions.ToArray();
                    }
                }
            return JsonConvert.SerializeObject(quizz);
        }