Ejemplo n.º 1
0
        /// <summary>
        /// Insert the <paramref name="newAnswer"/> in database
        /// </summary>
        /// <param name="newAnswer">The answer object to insert</param>
        /// <param name="ID">The ID of the strawpoll linked with this answer</param>
        public void sendAnswerInDatabase(Models.Answer newAnswer, int ID)
        {
            String          queryAddAnswer;
            ConnectionQuery newDataBaseTask = new ConnectionQuery();

            queryAddAnswer = "INSERT INTO Answer(Answer, NbrVotes, NumStrawPoll) " +
                             "VALUES(@Answer, @NbrVotes, @NumStrawPoll)";

            newDataBaseTask.OpenConnection();

            SqlCommand cmd = new SqlCommand(queryAddAnswer, newDataBaseTask.getSqlConnection());

            cmd.Parameters.AddWithValue("@Answer", newAnswer.getAnswer());
            cmd.Parameters.AddWithValue("@NbrVotes", newAnswer.getNbrVotesByAnswer());
            cmd.Parameters.AddWithValue("@NumStrawPoll", ID);

            newDataBaseTask.setMySqlCommand(cmd);
            newDataBaseTask.ExecuteNonQuery();
            newDataBaseTask.CloseConnection();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Increment the number of votes of the strawpoll and the answers chosen.
        /// Display the result page then (multiple answers only).
        /// </summary>
        /// <param name="ID">The ID of the strawpoll</param>
        /// <param name="checkbox">A list of String holding the label of each chosen answer</param>
        /// <returns>View Result.cshtml</returns>
        public ActionResult VoteCheckBoxes(int ID, List <String> checkbox)
        {
            //Increment database
            String          queryIncrementNbrVotes;
            ConnectionQuery newDataBaseTask = new ConnectionQuery();

            queryIncrementNbrVotes = "UPDATE StrawPoll " +
                                     "SET NbrVotes = NbrVotes + 1 " +
                                     "WHERE NumStrawPoll=@ID; " +
                                     "UPDATE Answer " +
                                     "SET NbrVotes = NbrVotes + 1 " +
                                     "WHERE NumStrawPoll = @IDAnswer AND Answer = @LabelAnswer;";

            newDataBaseTask.OpenConnection();

            foreach (String answerChosen in checkbox)
            {
                SqlCommand newcmd = new SqlCommand(queryIncrementNbrVotes, newDataBaseTask.getSqlConnection());

                newcmd.Parameters.AddWithValue("@ID", ID);
                newcmd.Parameters.AddWithValue("@IDAnswer", ID);
                newcmd.Parameters.AddWithValue("@LabelAnswer", answerChosen);

                newDataBaseTask.setMySqlCommand(newcmd);
                newDataBaseTask.ExecuteNonQuery();
            }

            newDataBaseTask.CloseConnection();

            createCookies(ID);

            //RETURN RESULT
            Models.StrawPoll myStrawPoll   = new Models.StrawPoll();
            Models.Answer    myAnswerOne   = new Models.Answer();
            Models.Answer    myAnswerTwo   = new Models.Answer();
            Models.Answer    myAnswerThree = new Models.Answer();
            Models.Answer    myAnswerFour  = new Models.Answer();

            //Get strawpoll in database
            String queryGetStrawPoll = "SELECT * FROM StrawPoll WHERE NumStrawPoll = @ID;";

            newDataBaseTask.OpenConnection();

            SqlCommand cmd = new SqlCommand(queryGetStrawPoll, newDataBaseTask.getSqlConnection());

            cmd.Parameters.AddWithValue("@ID", ID);

            SqlDataReader newReader = cmd.ExecuteReader();

            while (newReader.Read())
            {
                myStrawPoll.setStrawPollQuestion((String)newReader["Question"]);
                myStrawPoll.setMultipleChoices((bool)newReader["MultipleChoices"]);
                myStrawPoll.setNbrVotesStrawPoll((int)newReader["NbrVotes"]);
                myStrawPoll.setURLStrawPoll((String)newReader["URLStrawPoll"]);
                myStrawPoll.setURLDeletion((String)newReader["URLDeletion"]);
                myStrawPoll.setURLResults((String)newReader["URLResult"]);
                myStrawPoll.setIsActive((bool)newReader["IsActive"]);
            }

            newReader.Close();

            String queryGetAnswers = "SELECT * FROM Answer WHERE NumStrawPoll = @ID;";

            SqlCommand cmdAnswer = new SqlCommand(queryGetAnswers, newDataBaseTask.getSqlConnection());

            cmdAnswer.Parameters.AddWithValue("@ID", ID);

            SqlDataReader newReaderAnswer = cmdAnswer.ExecuteReader();

            List <Models.Answer> answers = new List <Models.Answer>();

            while (newReaderAnswer.HasRows)
            {
                while (newReaderAnswer.Read())
                {
                    Models.Answer newAnswer = new Models.Answer((String)newReaderAnswer["Answer"], (int)newReaderAnswer["NbrVotes"]);
                    answers.Add(newAnswer);
                }
                newReaderAnswer.NextResult();
            }

            newReaderAnswer.Close();
            newDataBaseTask.CloseConnection();

            //Set answers with database datas
            myAnswerOne   = answers.ElementAt(0);
            myAnswerTwo   = answers.ElementAt(1);
            myAnswerThree = answers.ElementAt(2);
            myAnswerFour  = answers.ElementAt(3);

            //SEND TO THE VIEW
            ViewBag.StrawPoll = myStrawPoll;

            ViewBag.aOne   = myAnswerOne.getAnswer();
            ViewBag.vOne   = myAnswerOne.getNbrVotesByAnswer();
            ViewBag.aTwo   = myAnswerTwo.getAnswer();
            ViewBag.vTwo   = myAnswerTwo.getNbrVotesByAnswer();
            ViewBag.aThree = myAnswerThree.getAnswer();
            ViewBag.vThree = myAnswerThree.getNbrVotesByAnswer();
            ViewBag.aFour  = myAnswerFour.getAnswer();
            ViewBag.vFour  = myAnswerFour.getNbrVotesByAnswer();

            return(View("Result"));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Display the result page of a specific strawpoll using its <paramref name="ID"/>
        /// </summary>
        /// <param name="ID">The ID of the strawpoll</param>
        /// <returns>View Result.cshtml</returns>
        public ActionResult ResultPage(int ID)
        {
            Models.StrawPoll myStrawPoll   = new Models.StrawPoll();
            Models.Answer    myAnswerOne   = new Models.Answer();
            Models.Answer    myAnswerTwo   = new Models.Answer();
            Models.Answer    myAnswerThree = new Models.Answer();
            Models.Answer    myAnswerFour  = new Models.Answer();

            //Get strawpoll in database
            String          queryGetStrawPoll;
            ConnectionQuery newDataBaseTask = new ConnectionQuery();

            queryGetStrawPoll = "SELECT * FROM StrawPoll WHERE NumStrawPoll = @ID;";

            newDataBaseTask.OpenConnection();

            SqlCommand cmd = new SqlCommand(queryGetStrawPoll, newDataBaseTask.getSqlConnection());

            cmd.Parameters.AddWithValue("@ID", ID);

            SqlDataReader newReader = cmd.ExecuteReader();

            while (newReader.Read())
            {
                myStrawPoll.setStrawPollQuestion((String)newReader["Question"]);
                myStrawPoll.setMultipleChoices((bool)newReader["MultipleChoices"]);
                myStrawPoll.setNbrVotesStrawPoll((int)newReader["NbrVotes"]);
                myStrawPoll.setURLStrawPoll((String)newReader["URLStrawPoll"]);
                myStrawPoll.setURLDeletion((String)newReader["URLDeletion"]);
                myStrawPoll.setURLResults((String)newReader["URLResult"]);
                myStrawPoll.setIsActive((bool)newReader["IsActive"]);
            }

            newReader.Close();

            String queryGetAnswers = "SELECT * FROM Answer WHERE NumStrawPoll = @ID;";

            SqlCommand cmdAnswer = new SqlCommand(queryGetAnswers, newDataBaseTask.getSqlConnection());

            cmdAnswer.Parameters.AddWithValue("@ID", ID);

            SqlDataReader newReaderAnswer = cmdAnswer.ExecuteReader();

            List <Models.Answer> answers = new List <Models.Answer>();

            while (newReaderAnswer.HasRows)
            {
                while (newReaderAnswer.Read())
                {
                    Models.Answer newAnswer = new Models.Answer((String)newReaderAnswer["Answer"], (int)newReaderAnswer["NbrVotes"]);
                    answers.Add(newAnswer);
                }
                newReaderAnswer.NextResult();
            }

            newReaderAnswer.Close();
            newDataBaseTask.CloseConnection();

            //Set answers with database datas
            myAnswerOne   = answers.ElementAt(0);
            myAnswerTwo   = answers.ElementAt(1);
            myAnswerThree = answers.ElementAt(2);
            myAnswerFour  = answers.ElementAt(3);

            //Send data to the views
            ViewBag.StrawPoll = myStrawPoll;

            ViewBag.aOne   = myAnswerOne.getAnswer();
            ViewBag.vOne   = myAnswerOne.getNbrVotesByAnswer();
            ViewBag.aTwo   = myAnswerTwo.getAnswer();
            ViewBag.vTwo   = myAnswerTwo.getNbrVotesByAnswer();
            ViewBag.aThree = myAnswerThree.getAnswer();
            ViewBag.vThree = myAnswerThree.getNbrVotesByAnswer();
            ViewBag.aFour  = myAnswerFour.getAnswer();
            ViewBag.vFour  = myAnswerFour.getNbrVotesByAnswer();

            return(View("Result"));
        }