public void Import(string connectionString, StatbookModel statbook, bool assumeATeams) { _connection = new SqlConnection(connectionString); try { _connection.Open(); _transaction = _connection.BeginTransaction(); // insert leagues LeagueGateway leagueGateway = new LeagueGateway(_connection, _transaction); var leagues = leagueGateway.GetAllLeagues(); League homeLeague = leagues.FirstOrDefault(l => l.Name.ToLower() == statbook.HomeTeam.LeagueName.ToLower()); League awayLeague = leagues.FirstOrDefault(l => l.Name.ToLower() == statbook.AwayTeam.LeagueName.ToLower()); int maxID = leagues.Select(l => l.ID).Max(); if(homeLeague == null) { homeLeague = leagueGateway.GetLeague(maxID + 1, statbook.HomeTeam.LeagueName, statbook.Date, false); maxID++; } if(awayLeague == null) { awayLeague = leagueGateway.GetLeague(maxID + 1, statbook.AwayTeam.LeagueName, statbook.Date, false); maxID++; } // insert teams TeamGateway teamGateway = new TeamGateway(_connection, _transaction); Team homeTeam, awayTeam; if (assumeATeams) { homeTeam = teamGateway.GetATeam(homeLeague.ID); awayTeam = teamGateway.GetATeam(awayLeague.ID); } else { homeTeam = teamGateway.GetTeam(statbook.HomeTeam.Name, homeLeague.ID, "A", false); awayTeam = teamGateway.GetTeam(statbook.AwayTeam.Name, awayLeague.ID, "A", false); } // insert bout BoutGateway boutGateway = new BoutGateway(_connection, _transaction); if(!boutGateway.DoesBoutExist(homeTeam.ID, awayTeam.ID, statbook.Date)) { Bout bout = boutGateway.GetBout(homeTeam.ID, awayTeam.ID, statbook.Date); BoutDataImport(statbook, bout, homeTeam, awayTeam); } else { // bout already exists Console.WriteLine(string.Format("Bout between {0} and {1} on {2} already exists.", homeTeam.Name, awayTeam.Name, statbook.Date)); } _transaction.Commit(); } finally { _connection.Close(); } }
public void Import(string connectionString, StatbookModel statbook) { _connection = new SqlConnection(connectionString); try { _connection.Open(); _transaction = _connection.BeginTransaction(); // insert leagues LeagueGateway leagueGateway = new LeagueGateway(_connection, _transaction); var leagues = leagueGateway.GetAllLeagues(); League homeLeague = leagues.FirstOrDefault(l => l.Name.ToLower() == statbook.HomeTeam.LeagueName.ToLower()); League awayLeague = leagues.FirstOrDefault(l => l.Name.ToLower() == statbook.AwayTeam.LeagueName.ToLower()); int maxID = leagues.Select(l => l.ID).Max(); if(homeLeague == null || awayLeague == null) { throw new InvalidOperationException("Bad league name"); } // for the basic importer, we'll just take whatever A team this league has, rather than worrying about validating the team name TeamGateway teamGateway = new TeamGateway(_connection, _transaction); Team homeTeam = teamGateway.GetATeam(homeLeague.ID); Team awayTeam = teamGateway.GetATeam(awayLeague.ID); // insert bout BoutGateway boutGateway = new BoutGateway(_connection, _transaction); if(!boutGateway.DoesBoutExist(homeTeam.ID, awayTeam.ID, statbook.Date)) { Bout bout = boutGateway.GetBout(homeTeam.ID, awayTeam.ID, statbook.Date); BoutDataImport(statbook, bout, homeTeam, awayTeam); } else { // bout already exists Console.WriteLine(string.Format("Bout between {0} and {1} on {2} already exists.", homeTeam.Name, awayTeam.Name, statbook.Date)); } _transaction.Commit(); } finally { _connection.Close(); } }