Ejemplo n.º 1
0
        public async Task LoadTeamsAndResults()
        {
            _dataBaseTools.Reset();

            var teams = GetTeams();

            foreach (var team in teams)
            {
                FillTeamDetails(team);

                await _teamRepository.AddAsync(team);

                await _teamRepository.SaveChangesAsync();
            }

            foreach (var team in teams)
            {
                var results = GetTeamResults(team);

                foreach (var result in results)
                {
                    var versusTeam = await _teamRepository.GetTeamByNameAsync(result.VersusTeam);

                    var thisTeam = await _teamRepository.GetTeamByNameAsync(team.Name);

                    var hasGameAlreadyBeenPlayed = result.Result.Split('-').Length == 2;

                    if (!hasGameAlreadyBeenPlayed)
                    {
                        return;
                    }

                    var homeTeamPoints   = Convert.ToInt32(result.Result.Split('-')[0].Trim());
                    var abroadTeamPoints = Convert.ToInt32(result.Result.Split('-')[1].Trim());

                    var day = await _dayRepository.GetDayByOrderAsync(result.Day);

                    if (day == null)
                    {
                        day = new Day(result.Day);
                        await _dayRepository.AddDayAsync(day);
                    }

                    var homeTeam = result.VersusIndicator == "vs"
                        ? thisTeam
                        : versusTeam;

                    var abroadTeam = result.VersusIndicator == "vs"
                        ? versusTeam
                        : thisTeam;

                    var game = new Game(homeTeam, abroadTeam, homeTeamPoints, abroadTeamPoints);
                    var isGameAlreadySaved = day.Games.Any(g =>
                                                           g.HomeTeamId == thisTeam.Id && g.AbroadTeamId == versusTeam.Id);

                    if (isGameAlreadySaved)
                    {
                        return;
                    }

                    day.AddGame(game);
                    await _dayRepository.SaveChangesAsync();
                }
            }
        }