/// <summary> /// The addUser method adds a new user to the database /// </summary> /// <param name='userName' /// type='string' /// desc='name of new user'> /// </param> /// <param name='password' /// type='string' /// desc='password of new user'> /// </param> /// <returns>true</returns> protected internal bool addUser(string userName, string password) { try { string retCode; SqlCommand sqlCommand = new SqlCommand(); createSqlManageUser( userName, password, "false", "add", sqlCommand); databaseAccess myDatabase = new databaseAccess(); sqlCommand.Connection = myDatabase.getConnection(); sqlCommand.Connection.Open(); sqlCommand.ExecuteNonQuery(); sqlCommand.Connection.Close(); retCode = sqlCommand.Parameters["@@return"].Value.ToString(); // catch problems within the stored procedure if (retCode == "S_OK") { return(true); } else { throw new jokeException(retCode); } // catch problems with the database } catch (Exception e) { throw e; } }
/// <summary> /// The deleteUnmoderated method deletes a previously /// submitted joke (unmoderated) joke /// (for moderators only) /// </summary> /// <param name='userName' /// type='string' /// desc='name of moderator'> /// </param> /// <param name='password' /// type='string' /// desc='password of moderator'> /// </param> /// <param name='jokeID' /// type='int' /// desc='joke ID of joke'> /// </param> /// <returns>true</returns> protected internal bool deleteUnmoderated( string userName, string password, int jokeID) { string retCode; try { // check if user is a moderator userAdminImplement myUser = new userAdminImplement(); SqlCommand sqlCommand = new SqlCommand(); myUser.createSqlCheckUser( userName, password, "true", sqlCommand); databaseAccess myDatabase = new databaseAccess(); sqlCommand.Connection = myDatabase.getConnection(); sqlCommand.Connection.Open(); sqlCommand.ExecuteNonQuery(); retCode = sqlCommand.Parameters["@@return"].Value.ToString(); // exit, if user not a moderator if (retCode != "S_OK") { sqlCommand.Connection.Close(); throw new jokeException(retCode); } // delete the joke sqlCommand.Parameters.Clear(); createSqlManageJoke( userName, "", "", jokeID.ToString(), "delete", sqlCommand); sqlCommand.ExecuteNonQuery(); sqlCommand.Connection.Close(); retCode = sqlCommand.Parameters["@@return"].Value.ToString(); // catch problems within the stored procedure if (retCode == "S_OK") { return(true); } else { throw new jokeException(retCode); } // catch problems with the database } catch (Exception e) { throw e; } }
/// <summary> /// The addModerator method sets a previously added user to become /// a moderator /// </summary> /// <param name='userName' /// type='string' /// desc='name of moderator making the call'> /// </param> /// <param name='password' /// type='string' /// desc='password of moderator making the call'> /// </param> /// <param name='newModerator' /// type='string' /// desc='user name of registered user who will become /// a moderator'> /// </param> /// <returns>true</returns> protected internal bool addModerator( string userName, string password, string newModerator) { string retCode; try { // check if user is a moderator SqlCommand sqlCommand = new SqlCommand(); createSqlCheckUser(userName, password, "true", sqlCommand); databaseAccess myDatabase = new databaseAccess(); sqlCommand.Connection = myDatabase.getConnection(); sqlCommand.Connection.Open(); sqlCommand.ExecuteNonQuery(); retCode = sqlCommand.Parameters["@@return"].Value.ToString(); // catch problems within the stored procedure if (retCode != "S_OK") { sqlCommand.Connection.Close(); throw new jokeException(retCode); } // make newModerator a moderator sqlCommand.Parameters.Clear(); createSqlManageUser( newModerator, "", "true", "modify", sqlCommand); sqlCommand.ExecuteNonQuery(); sqlCommand.Connection.Close(); retCode = sqlCommand.Parameters["@@return"].Value.ToString(); // catch problems within the stored procedure if (retCode == "S_OK") { return(true); } else { throw new jokeException(retCode); } // catch problems with the database } catch (Exception e) { throw e; } }
/// <summary> /// The checkUser method checks if a user or moderator is /// already defined in the database /// </summary> /// <param name='userName' /// type='string' /// desc='name of user or moderator'> /// </param> /// <param name='password' /// type='string' /// desc='password of user or moderator'> /// </param> /// <param name='isModerator' /// type='bool' /// desc='check for moderator status (if false, /// we do not check)'> /// </param> /// <returns>nothing</returns> protected internal bool checkUser( string userName, string password, bool isModerator) { string retCode; try { SqlCommand sqlCommand = new SqlCommand(); if (isModerator) { // check if user is a moderator... createSqlCheckUser(userName, password, "true", sqlCommand); } else { // ... or a registered user createSqlCheckUser(userName, password, "", sqlCommand); } databaseAccess myDatabase = new databaseAccess(); sqlCommand.Connection = myDatabase.getConnection(); sqlCommand.Connection.Open(); sqlCommand.ExecuteNonQuery(); retCode = sqlCommand.Parameters["@@return"].Value.ToString(); // catch problems within the stored procedure if (retCode == "S_OK") { return(true); } else { throw new jokeException(retCode); } // catch problems with the database } catch (Exception e) { throw e; } }
/// <summary> /// The getUnmoderated method retrieves howMany jokes from /// the database /// (for moderators only) /// </summary> /// <param name='userName' /// type='string' /// desc='name of moderator'> /// </param> /// <param name='password' /// type='string' /// desc='password of moderator'> /// </param> /// <param name='howMany' /// type='int' /// desc='number of jokes to return'> /// </param> /// <returns>an XML representation (xmlJokesReturn) /// of a single joke</returns> protected internal xmlJokesReturn[] getUnmoderated( string userName, string password, int howMany) { string retCode; try { // check if user is a moderator userAdminImplement myUser = new userAdminImplement(); SqlCommand sqlCommand = new SqlCommand(); myUser.createSqlCheckUser( userName, password, "true", sqlCommand); databaseAccess myDatabase = new databaseAccess(); sqlCommand.Connection = myDatabase.getConnection(); sqlCommand.Connection.Open(); sqlCommand.ExecuteNonQuery(); retCode = sqlCommand.Parameters["@@return"].Value.ToString(); // exit, if user not a moderator if (retCode != "S_OK") { sqlCommand.Connection.Close(); throw new jokeException(retCode); } // retrieve the first <howMany> unmoderated jokes // maximum is 10 jokes if ((howMany < 1) || (howMany > 10)) { throw new jokeException("F_10JokesMax"); } sqlCommand.Parameters.Clear(); createSqlReturnJokes( howMany.ToString(), "false", "false", sqlCommand); sqlCommand.ExecuteNonQuery(); sqlCommand.Connection.Close(); SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand); DataTable dataTable = new DataTable("sqlReturn"); sqlDataAdapter.Fill(dataTable); // convert SQL table into xmlJokesReturn class int rowCount = dataTable.Rows.Count; xmlJokesReturn[] myJokes = new xmlJokesReturn[rowCount]; for (int i = 0; i < rowCount; i++) { myJokes[i] = new xmlJokesReturn(); myJokes[i].jokeID = dataTable.Rows[i][0].ToString(); myJokes[i].joke = dataTable.Rows[i][1].ToString(); myJokes[i].rating = dataTable.Rows[i][2].ToString(); } // catch problems within the stored procedure if (rowCount > 0) { return(myJokes); } else { throw new jokeException("F_noJokes"); } // catch problems with the database } catch (Exception e) { throw e; } }