Beispiel #1
0
 public void saveVoteMajority(Poll poll, int optionId, string userId, string ipAdress)
 {
     PollDAO.saveVoteMajority(poll, optionId, userId, ipAdress);
     var hubContext = GlobalHost.ConnectionManager.GetHubContext<PollHub>();
     poll = PollDAO.getPollResultsByUrl(poll.url);
     hubContext.Clients.Group(poll.url).updatePoll(poll);
 }
Beispiel #2
0
 public string savePoll(Poll newPoll)
 {
     while (checkUniqueUrl(newPoll.url) == false)
     {
         newPoll.url = GuidEncoder.Encode(Guid.NewGuid().ToString());
     }
     PollDAO.savePoll(newPoll);
     return newPoll.url;
 }
Beispiel #3
0
        public void savePoll(Poll newPoll)
        {
            using (SqlConnection conn = DAOConnections.GetDatabaseConnection())
            {
                using (SqlTransaction tran = conn.BeginTransaction())
                {
                    try
                    {
                        using (SqlCommand command = conn.CreateCommand())
                        {
                            command.Transaction = tran;
                            string cmdText = String.Format("INSERT INTO poll (url, topic, type) VALUES (@url, @topic, @type);");

                            command.CommandText = cmdText;
                            command.Parameters.AddWithValue("@url", newPoll.url);
                            command.Parameters.AddWithValue("@topic", newPoll.topic);
                            command.Parameters.AddWithValue("@type", newPoll.type);
                            command.ExecuteNonQuery();
                        }

                        newPoll.id = conn.Query<int>("SELECT id FROM poll WHERE url = @url", new { url = newPoll.url }, tran).Single();

                        using (SqlCommand command = conn.CreateCommand())
                        {
                            command.Transaction = tran;
                            string cmdText = "INSERT INTO options (poll_id, option_id, name) VALUES ";
                            for (int i = 0; i < newPoll.options.Count; i++)
                            {
                                cmdText += "(" + newPoll.id + ", " + i + ", @pollName" + i + "),";
                            }
                            cmdText = cmdText.TrimEnd(',') + ";";
                            command.CommandText = cmdText;
                            for (int i = 0; i < newPoll.options.Count; i++)
                            {
                                command.Parameters.AddWithValue("@pollName" + i, newPoll.options[i].name);
                            }
                            command.ExecuteNonQuery();
                        }
                        tran.Commit();
                    }
                    catch (Exception)
                    {
                        tran.Rollback();
                        throw;
                    }
                }
            }
        }
Beispiel #4
0
 public void saveVoteMajority(Poll poll, int optionId, string userId, string ipAddress)
 {
     using (SqlConnection conn = DAOConnections.GetDatabaseConnection())
     {
         using (SqlTransaction tran = conn.BeginTransaction())
         {
             try
             {
                 using (SqlCommand command = conn.CreateCommand())
                 {
                     command.Transaction = tran;
                     string cmdText = String.Format("INSERT INTO user_votes (u_id, poll_id, ip_address) VALUES (@userId, @pollId, @ipAddress);");
                     command.CommandText = cmdText;
                     command.Parameters.AddWithValue("@userId", userId);
                     command.Parameters.AddWithValue("@pollId", poll.id);
                     command.Parameters.AddWithValue("@ipAddress", ipAddress);
                     command.ExecuteNonQuery();
                 }
                 using (SqlCommand command = conn.CreateCommand())
                 {
                     command.Transaction = tran;
                     string cmdText = String.Format("UPDATE poll SET votes = votes + 1 WHERE id = @pollId;");
                     command.CommandText = cmdText;
                     command.Parameters.AddWithValue("@pollId", poll.id);
                     command.ExecuteNonQuery();
                 }
                 using (SqlCommand command = conn.CreateCommand())
                 {
                     command.Transaction = tran;
                     command.CommandText = "UPDATE options SET points = points + 1 WHERE poll_id = @pollId AND option_id = @optionId;";
                     command.Parameters.AddWithValue("@pollId", poll.id);
                     command.Parameters.AddWithValue("@optionId", optionId);
                     command.ExecuteNonQuery();
                 }
                 tran.Commit();
             }
             catch (Exception)
             {
                 tran.Rollback();
                 throw;
             }
         }
     }
 }