public void AddFixture(DateTime date, string leagueId) { var parseDate = date.ToString("yyyy-MM-dd"); var url = $"https://allsportsapi.com/api/football/?met=Fixtures&leagueId={leagueId}&APIkey={apikey}&from={parseDate}&to={parseDate}"; var json = new WebClient().DownloadString(url); var result = JsonConvert.DeserializeObject <JsonFixture>(json); if (result.success == 1 && result.result != null) { foreach (var jsonFixture in result.result) { Fixture fixture = Mapper.Map <Fixture>(jsonFixture); fixture.HomeTeam = context.Teams.FirstOrDefault(t => t.TeamKey.ToString() == jsonFixture.home_team_key); fixture.AwayTeam = context.Teams.FirstOrDefault(t => t.TeamKey.ToString() == jsonFixture.away_team_key); if (fixture.AwayTeam == null) { service.AddTeams(int.Parse(jsonFixture.league_key)); } Fixture fixtureDB = context.Fixtures.FirstOrDefault(s => s.EventKey == jsonFixture.event_key); if (fixtureDB == null) { context.Fixtures.Add(fixture); } else { if (!Equals(fixture, fixtureDB)) { fixture.ID = fixtureDB.ID; fixture.HomeTeamID = fixtureDB.HomeTeamID; fixture.AwayTeamID = fixtureDB.AwayTeamID; context.Entry(fixtureDB).CurrentValues.SetValues(fixture); } } } context.SaveChanges(); } }
public void AddStandings(int leagueId) { var url = $"https://allsportsapi.com/api/football/?&met=Standings&leagueId={leagueId}&APIkey={apikey}"; var json = new WebClient().DownloadString(url); var result = JsonConvert.DeserializeObject <Root>(json); foreach (var jsonStanding in result.result.total) { Standing standing = Mapper.Map <Standing>(jsonStanding); Team team = _context.Teams.FirstOrDefault(t => t.TeamKey.ToString() == jsonStanding.team_key); if (team == null) { AddTeams(int.Parse(jsonStanding.league_key)); } standing.Team = team; standing.League = _context.Leagues.FirstOrDefault(t => t.LeagueID.ToString() == jsonStanding.league_key); Standing standingDB = _context.Standings.FirstOrDefault(s => s.TeamKey == standing.TeamKey); if (standingDB == null) { _context.Standings.Add(standing); } else { if (!Equals(standing, standingDB)) { standing.ID = standingDB.ID; standing.LeagueId = standingDB.LeagueId; standing.TeamID = standingDB.TeamID; _context.Entry(standingDB).CurrentValues.SetValues(standing); } } } _context.SaveChanges(); }