예제 #1
0
        public void AddQuestions(Dictionary <string, string> quizzData)
        {
            var quizzId = quizzData["quizz_id"];

            using (var dbWrapper = new DbWrapper("studybuddyquizzes"))
            {
                if (!dbWrapper.DoesDbExist())
                {
                    InstantiateQuizzDatabase();
                }
                using (var command = new SQLiteCommand("SELECT QuizzID from Quizzes WHERE QuizzID = @QuizzID"))
                {
                    command.Parameters.AddWithValue("@QuizzID", quizzId);
                    var found = dbWrapper.ExecuteScalar(command);
                }
            }
        }
예제 #2
0
        public void CreateNewQuizz(Quizz quizzData)
        {
            using (var dbWrapper = new DbWrapper("studybuddyquizzes"))
            {
                var quizzId = 0;
                if (!dbWrapper.DoesDbExist())
                {
                    InstantiateQuizzDatabase();
                }
                using (
                    var command =
                        new SQLiteCommand(
                            "INSERT INTO Quizzes (QuizzName, Description) values (@QuizzName, @Description)")
                    )
                {
                    command.Parameters.AddWithValue("@QuizzName", quizzData.name);
                    command.Parameters.AddWithValue("@Description", quizzData.description);

                    dbWrapper.ExecuteNonQuery(command);
                    command.CommandText =
                        "SELECT QuizzID FROM Quizzes WHERE QuizzName = @QuizzName";
                    quizzId = int.Parse(dbWrapper.ExecuteScalar(command).ToString());
                }
                foreach (var question in quizzData.questions)
                {
                    var questionId = -1;
                    using (
                        var command =
                            new SQLiteCommand(
                                "INSERT INTO Questions (Question, QuizzID) values (@Question, @QuizzID)")
                        )
                    {
                        command.Parameters.AddWithValue("@QuizzID", quizzId);
                        command.Parameters.AddWithValue("@Question", question.question);
                        dbWrapper.ExecuteNonQuery(command);
                        command.CommandText =
                            "SELECT QuestionID FROM Questions WHERE Question = @Question AND QuizzID = @QuizzID";
                        questionId = int.Parse(dbWrapper.ExecuteScalar(command).ToString());
                    }
                    foreach (var option in question.options)
                    {
                        using (
                            var command =
                                new SQLiteCommand(
                                    "INSERT INTO Answers (QuestionID, Answer, Is_Correct) values (@QuestionID, @Answer, @Is_Correct)")
                            )
                        {
                            command.Parameters.AddWithValue("@QuestionID", questionId);
                            command.Parameters.AddWithValue("@Answer", option.answer);
                            if (question.rightAnswerIds.Contains(option.id))
                            {
                                command.Parameters.AddWithValue("@Is_Correct", 1);
                            }
                            else
                            {
                                command.Parameters.AddWithValue("@Is_Correct", 0);
                            }
                            dbWrapper.ExecuteNonQuery(command);
                        }
                    }
                }
            }
        }
        public UserAuthorizationController(ITokenizer tokenizer)
        {
            Post["/login/"] = x =>
            {
                using (var dbWrapper = new DbWrapper("AuthenticationDbCore"))
                {
                    if (!dbWrapper.DoesDbExist())
                    {
                        StudyBuddyDbAssistant.CreateDatabase("AuthenticationDbCore");
                        StudyBuddyDbAssistant.CreateAuthenticationTables("AuthenticationDbCore");
                    }
                }
                var loginData = ParseAuthData(Request.Body);
                var identity  = AuthenticationSingleton.AuthenticateUser(loginData["username"],
                                                                         loginData["password"]);
                if (identity == null)
                {
                    var response = (Response)JsonConvert.SerializeObject(FormErrorResponse(-1));
                    response.ContentType = "application/json";
                    response.StatusCode  = HttpStatusCode.NotAcceptable;
                    return(response);
                }
                else
                {
                    var token = tokenizer.Tokenize(identity, Context);
                    return(new
                    {
                        Token = token
                    });
                }
            };

            Post["/register/"] = x =>
            {
                var regData = ParseAuthData(Request.Body);
                var authenticationStatus = AuthenticationSingleton.RegisterUser(regData["username"], regData["password"]);
                if (authenticationStatus != 0)
                {
                    var response = (Response)JsonConvert.SerializeObject(FormErrorResponse(authenticationStatus));
                    response.ContentType = "application/json";
                    response.StatusCode  = HttpStatusCode.NotAcceptable;
                    return(response);
                }
                using (var dbWrapper = new DbWrapper("AuthenticationDbCore"))
                {
                    if (!dbWrapper.DoesDbExist())
                    {
                        StudyBuddyDbAssistant.CreateDatabase("AuthenticationDbCore");
                        StudyBuddyDbAssistant.CreateAuthenticationTables("AuthenticationDbCore");
                    }
                }

                var identity = AuthenticationSingleton.AuthenticateUser(regData["username"],
                                                                        regData["password"]);
                if (identity == null)
                {
                    var response = (Response)JsonConvert.SerializeObject(FormErrorResponse(-1));
                    response.ContentType = "application/json";
                    response.StatusCode  = HttpStatusCode.NotAcceptable;
                    return(response);
                }
                else
                {
                    var token = tokenizer.Tokenize(identity, Context);
                    return(new
                    {
                        Token = token
                    });
                }
            };
        }