private int checkAndChangeVote(int userID, int songID, bool vote)
{
int currentVote;
MySqlConnection connect = new MySqlConnection(connString);
MySqlCommand command = connect.CreateCommand();
command.CommandText = "Select iduser" + userID + " from user" + userID + " where songID like '%" + songID + "%'";
int returnId = 0;
try
{
connect.Open();
MySqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
returnId = (int)reader["iduser"+userID];
}
}
catch (MySqlException ex)
{
throw new DatabaseException("SQL Exception: " + ex.Message, ex); // Propagate the exception upwards after handling the finally block
}
finally
{
connect.Close();
}
string userTable = "user" + userID;
if (returnItem(returnId, "voteUpOrDown", userTable) == null)
{
addVoteToUserTable(userID, songID, vote);
if (vote == true)
{
return 1;
}
else
{
return -1;
}
}
else
{
currentVote = (int)returnItem(returnId, "voteUpOrDown", userTable);
}
if ((currentVote == 1) && (vote == true)) {
return 0; //user already upvoted
}
else if ((currentVote == 1) && (vote == false)) //user changes upvote to downvote, returns true to show that vote was changed
{
MySqlConnection conn = new MySqlConnection(connString);
SQLNonQuery(conn, "Update " + userTable + " SET voteUpOrDown= '-1' WHERE songID='" + songID + "'");
return -2;
}
else if ((currentVote == -1) && (vote == false))
{
return 0; //user already downvoted
}
else if ((currentVote == -1) && (vote == true)) //user changes downvote to upvote, returns true to show that vote was changed
{
MySqlConnection conn = new MySqlConnection(connString);
SQLNonQuery(conn, "Update " + userTable + " SET voteUpOrDown= '1' WHERE songID='" + songID + "'");
return 2;
}
else
{
addVoteToUserTable(userID, songID, vote);
return vote ? 1 : -1;
}
}