/// <summary> /// Creates a game object with the given information, ties it to the team with the given /// team ID and saved the game to the database. /// </summary> /// <param name="teamID">The ID of the team of interest.</param> /// <param name="opponent">The name of the opposing team.</param> /// <param name="homeOrAway">If the game is at home or away (string: "Home" or "Away").</param> /// <param name="location">The location of the game.</param> /// <param name="date">The date of the game (M/D).</param> /// <param name="time">The time of the game (H/MM TT).</param> /// <param name="seasonID">The ID of the season it is being added to.</param> /// <returns>A message detailing the result of the addition.</returns> public ActionResult AJAX_AddGame(long teamID, string opponent, string homeOrAway, string location, string date, string time, long seasonID) { string result = "Request is not authenticated."; if (Request.IsAuthenticated) { DBAccessor dba = new DBAccessor(); Team team = dba.GetTeamDetails(teamID); Person user = new Person(); user.email = User.Identity.Name; Season season = dba.GetSeason(seasonID); if (team.coaches.Contains(user, new PersonComparer())) { try { DateTime gameDate = Parser.ParseDateAndTime(date, time, season.year); Game game = new Game(); game.isHome = homeOrAway.Equals("Home"); game.location = location; game.opponent = opponent; game.season = season; game.date = gameDate; if (dba.AddGame(game)) { result = "Game sucessfully added to the season."; } else { result = "Error adding the game to the season."; } } catch { result = "An invalid date was given."; } } else { result = "You must be a coach of the team to add a game."; LogEntry entry = new LogEntry(LogType.INVALID_REQUEST, LogFunction.ADD_GAME, LogAction.NA); entry.User = user; entry.Message = "Attempt to add a game to " + team.name + " (" + team.ID + ")."; dba.LogMessage(entry); } } return Json( new { message = result }, JsonRequestBehavior.AllowGet ); }
/// <summary> /// Adds the given game to the database. /// </summary> /// <param name="game">The game to be added to the database.</param> /// <returns>Success of the addition.</returns> public bool AddGame(Game game) { String query = "INSERT INTO " + AppConstants.MYSQL_TABLE_GAME; query += " (seasonID, gameDate, location, opponent, isHome) VALUES ("; query += game.season.ID + ", @date, '" + game.location + "', '" + game.opponent + "', " + Convert.ToInt16(game.isHome) + ")"; MySqlDataReader dr = null; bool success = true; try { connection.Open(); command.CommandText = query; command.Parameters.AddWithValue("@date", game.date); dr = command.ExecuteReader(); } catch { success = false; } finally { connection.Close(); } return success; }