//Update LeagueInfo table based on WorldDetails.xml file private void UpdateLeagueInfo(HttpClient session, XmlParser parser, ht_stats_dk_dbContext context) { string leagueData = GetLeagueData(session); List <LeagueInfo> leagueList = parser.ParseLeagueInfoData(leagueData); Debug.WriteLine("LeagueInfo Count: " + leagueList.Count()); foreach (LeagueInfo league in leagueList) { //add or update entity if (context.LeagueInfo.Where(c => c.LeagueId == league.LeagueId).Count() == 0) // Find(league.LeagueId) == null) { context.Add(league); Debug.WriteLine("Adding: " + league.LeagueId); } else { context.Update(league); } } try { context.SaveChanges(); Debug.WriteLine("Database changes saved!"); } catch (Exception e) { Debug.WriteLine("DB ERROR: " + e); } }
//Update Team table based on TeamDetails.xml private void UpdateTeams(HttpClient session, XmlParser parser, ht_stats_dk_dbContext context) { List <int> teamIDs = new List <int>(); int updated = 0; int added = 0; // get a number of series from League table - move to method? using (ht_stats_dk_dbContext db = new ht_stats_dk_dbContext()) { var data = (from s in db.League where s.LeagueLevel <= 5 orderby s.SeriesId ascending select s).Skip(0).Take(1); //refactor to approach used in UpdateMatchInfo(). //for every series, get team IDs foreach (League l in data) { string leagueData = GetLeagueUnitData(l.SeriesId, session); teamIDs.AddRange(parser.ParseTeamIDs(leagueData)); } Debug.WriteLine("Getting data on " + teamIDs.Count + " teams!"); } using (ht_stats_dk_dbContext db = new ht_stats_dk_dbContext()) { //for every team in league, get team data foreach (int id in teamIDs) { string teamData = GetTeamData(id, session); if (teamData == null) { Debug.WriteLine("no teamdata at team ID: " + id); break; } bool existingTeam = db.Team.Where(c => c.TeamId == id).Count() == 1; //do not store bot teams if (!parser.IsBotTeam(id, teamData)) { //add or update team if (!existingTeam) { db.Add(parser.ParseTeamData(id, teamData)); //new team added++; //Debug.WriteLine("Added team: " + id); } else { db.Update(parser.ParseTeamData(id, teamData)); //existing team updated++; //Debug.WriteLine("Updated team: " + id); } } else { if (existingTeam) { //we encountered a bot team in our db //it became bot since first fetch, update to maintain data integrity (matches) db.Update(parser.ParseTeamData(id, teamData)); updated++; //Debug.WriteLine("Updated BOT team: " + id); } } } Debug.WriteLine("Updating " + updated + " teams"); Debug.WriteLine("Adding " + added + " teams"); Debug.WriteLine("Skipped " + (teamIDs.Count - updated - added) + " bot teams."); try { db.SaveChanges(); Debug.WriteLine("Database changes saved!"); } catch (Exception e) { Debug.WriteLine("DB ERROR: " + e); } } }