/// <summary> /// Creates a practice 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="location">The location of the practice.</param> /// <param name="date">The date of the practice (M/D).</param> /// <param name="time">The time of the practice (H/MM TT).</param> /// <param name="seasonID">The ID of the season is is being added to.</param> /// <returns>A message detailing the result of the addition.</returns> public ActionResult AJAX_AddPractice(long teamID, 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 practiceDate = Parser.ParseDateAndTime(date, time, season.year); Practice practice = new Practice(); practice.location = location; practice.season = season; practice.date = practiceDate; if (dba.AddPractice(practice)) { result = "Practice sucessfully added to the season."; } else { result = "Error adding the practice to the season."; } } catch { result = "An invalid date was given."; } } else { result = "You must be a coach of the team to add a practice."; LogEntry entry = new LogEntry(LogType.INVALID_REQUEST, LogFunction.ADD_PRACTICE, LogAction.NA); entry.User = user; entry.Message = "Attempt to add a practice to " + team.name + " (" + team.ID + ")."; dba.LogMessage(entry); } } return Json( new { message = result }, JsonRequestBehavior.AllowGet ); }
/// <summary> /// Adds the given practice to the database. /// </summary> /// <param name="practice">The practice to be added to the database.</param> /// <returns>Success of the addition.</returns> public bool AddPractice(Practice practice) { String query = "INSERT INTO " + AppConstants.MYSQL_TABLE_PRACTICE; query += " (seasonID, practiceDate, location) VALUES ("; query += practice.season.ID + ", @date, '" + practice.location + "')"; MySqlDataReader dr = null; bool success = true; try { connection.Open(); command.CommandText = query; command.Parameters.AddWithValue("@date", practice.date); dr = command.ExecuteReader(); } catch { success = false; } finally { connection.Close(); } return success; }