Ejemplo n.º 1
0
        public IActionResult Index(string id)
        {
            AllFixturesViewModel     fixtureViewModel = new AllFixturesViewModel();
            IList <FixtureViewModel> fixtures         = new List <FixtureViewModel>();
            var allFixtures = fixtureService.AddFixture(DateTime.Now.Date);

            if (id == null && allFixtures.result != null)
            {
                id = footballService
                     .GetLeagueByID(allFixtures.result.FirstOrDefault(l => footballService.FindLeagueByID(l.league_key)).league_key)
                     .LeagueID
                     .ToString();
            }

            fixtureService.AddFixture(DateTime.Now.Date, id);
            List <Fixture> fixtureData = footballService.GetFixture(DateTime.Now.Date, id);

            foreach (var f in fixtureData)
            {
                fixtureService.Livescore(id);

                if (footballService.GetTeamByKey(f.HomeTeamKey) == null ||
                    footballService.GetTeamByKey(f.AwayTeamKey) == null)
                {
                    standingService.AddTeams(int.Parse(id));
                    standingService.AddPlayers(int.Parse(id));
                }

                FixtureViewModel currentFixture = Mapper.Map <FixtureViewModel>(f);
                currentFixture.EventStatus = f.Status == null ? "0 - 0" : f.Status;

                fixtures.Add(currentFixture);
            }

            fixtureViewModel.LeagueName = footballService.GetLeagueByID(id).Name;
            fixtureViewModel.Countries  = footballService.GetAllCountries();
            fixtureViewModel.Leagues    = footballService.GetAllLeagues();
            fixtureViewModel.Fixtures   = fixtures.Where(f => f.HomeTeam.Badge != null && f.AwayTeam.Badge != null);

            return(View(fixtureViewModel));
        }
Ejemplo n.º 2
0
        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();
            }
        }