public RatingDTO getUser(int UserId) { RatingDTO result = null; RatingDBO row = getUserRow(UserId); if (row != null) { result = new RatingDTO(); result.userId = row.UserId; result.average = row.TotalScore / row.TotalMoves; result.latest = row.LatestScore; result.moves = getMoves(UserId); } return(result); }
public RatingDTO updateUser(int UserId, double LatestScore) { RatingDBO row = getUserRow(UserId); RatingDTO result = null; if (row != null) { using (SqlConnection connection = new SqlConnection(getConnectionString())) { connection.Open(); SqlCommand command = new SqlCommand(null, connection); double total = row.TotalScore + LatestScore; int moves = row.TotalMoves + 1; command.CommandText = "UPDATE RATINGS SET LatestScore = " + LatestScore + ", TotalScore = " + total + ", TotalMoves = " + moves + " WHERE UserId = " + UserId; command.ExecuteNonQuery(); connection.Close(); } result = getUser(UserId); } return(result); }
private RatingDBO getUserRow(int UserId) { RatingDBO result = null; using (SqlConnection connection = new SqlConnection(getConnectionString())) { connection.Open(); SqlCommand command = new SqlCommand(null, connection); command.CommandText = "SELECT * FROM RATINGS WHERE UserId = " + UserId; SqlDataReader reader = command.ExecuteReader(); if (reader.Read()) { result = new RatingDBO(); result.LatestScore = (double)reader["LatestScore"]; result.TotalMoves = (int)reader["TotalMoves"]; result.TotalScore = (double)reader["TotalScore"]; result.UserId = (int)reader["UserId"]; } connection.Close(); } return(result); }