private static void AddTeamsIntoLeague(League league, IEnumerable<XElement> teamElements, FootballEntities context)
        {
            foreach (var teamElement in teamElements)
            {
                if (teamElement.Attribute("name") == null)
                {
                    throw new ArgumentException("Atribute \"name\" is required.");
                }

                var currTeamCountryName = teamElement.Attribute("country") != null
                    ? teamElement.Attribute("country").Value
                    : null;
                var teamName = teamElement.Attribute("name").Value;

                // If this team exist
                if (context.Teams.Any(t => t.TeamName == teamName && t.Country.CountryName == currTeamCountryName))
                {
                    var existingTeam =
                        context.Teams.FirstOrDefault(
                            t => t.TeamName == teamName && t.Country.CountryName == currTeamCountryName);

                    Console.WriteLine("Existing team: {0} ({1})", existingTeam.TeamName,
                        existingTeam.Country != null ? existingTeam.Country.CountryName : "no country");

                    // If this team is in this league
                    if (league.Teams.Any(t => t.TeamName == existingTeam.TeamName && t.Country == existingTeam.Country))
                    {
                        Console.WriteLine("Existing team in league: {0} belongs to {1}", existingTeam.TeamName, league.LeagueName);
                    }
                    else
                    {
                        league.Teams.Add(existingTeam);
                        context.SaveChanges();

                        Console.WriteLine("Added team to league: {0} to league {1}", existingTeam.TeamName,
                        league.LeagueName);
                    }
                }
                else
                {
                    var newTeam = new Team
                    {
                        TeamName = teamName,
                        Country = context.Countries.FirstOrDefault(c => c.CountryName == currTeamCountryName)
                    };

                    context.Teams.Add(newTeam);
                    context.SaveChanges();

                    Console.WriteLine("Created team: {0} ({1})", newTeam.TeamName,
                        newTeam.Country != null ? newTeam.Country.CountryName : "no country");

                    league.Teams.Add(newTeam);
                    context.SaveChanges();

                    Console.WriteLine("Added team to league: {0} to league {1}", newTeam.TeamName,
                        league.LeagueName);
                }
            }
        }
        private static League CreateLeagueIfNotExtists(FootballEntities context, XElement xmlLeague)
        {
            League league = null;
            var xmlLeagueName = xmlLeague.Element("league-name");
            if (xmlLeagueName != null)
            {
                string leagueName = xmlLeagueName.Value;
                var leagueInDb = context.Leagues.FirstOrDefault(l => l.LeagueName == leagueName);

                if (leagueInDb != null)
                {
                    Console.WriteLine("Existing league: " + leagueInDb.LeagueName);
                    league = leagueInDb;
                }
                else
                {
                    league = new League { LeagueName = leagueName };
                    context.Leagues.Add(league);
                    context.SaveChanges();
                    Console.WriteLine("Created league: " + league.LeagueName);
                }
            }

            return league;
        }
 private static void AddTeamToLeagueIfNotExist(FootballEntities context, Team team, League league)
 {
     if (league != null)
     {
         bool teamInLeague = league.Teams.Contains(team);
         if (!teamInLeague)
         {
             league.Teams.Add(team);
             context.SaveChanges();
             Console.WriteLine("Added team to league: " + team.TeamName + " to league " + league.LeagueName);
         }
         else
         {
             Console.WriteLine("Existing team in league: " + team.TeamName + " belongs to " + league.LeagueName);
         }
     }
 }
Exemplo n.º 4
0
        private static League GetLeagueEntity(FootballEntities context, XElement leagueName)
        {
            League leagueEntity = context.Leagues.FirstOrDefault(l => l.LeagueName == leagueName.Value);

            if (leagueEntity == null)
            {
                leagueEntity = new League {LeagueName = leagueName.Value};
                context.Leagues.Add(leagueEntity);
                context.SaveChanges();
                Console.WriteLine("Created league: {0}", leagueName.Value);
            }
            else
            {
                Console.WriteLine("Existing league: {0}", leagueName.Value);
            }

            return leagueEntity;
        }
        private static void AddLeague(XElement leagueElement)
        {
            Console.WriteLine("Processing league #" + counter++ + " ...");

            var context = new FootballEntities();

            var leagueName = leagueElement.Element("league-name") != null
                    ? leagueElement.Element("league-name").Value
                    : null;

            if (leagueName == null)
            {
                AddTeams(leagueElement);
            }
            else
            {
                var teamElements = leagueElement.XPathSelectElements("teams/team");

                // If league exist
                if (context.Leagues.Any(l => l.LeagueName == leagueName))
                {
                    var existingLeague = context.Leagues.FirstOrDefault(l => l.LeagueName == leagueName);
                    Console.WriteLine("Existing league: {0}", existingLeague.LeagueName);

                    AddTeamsIntoLeague(existingLeague, teamElements, context);
                    context.SaveChanges();
                }
                else
                {
                    var newLeague = new League
                    {
                        LeagueName = leagueName
                    };
                    Console.WriteLine("Created league: {0}", newLeague.LeagueName);

                    AddTeamsIntoLeague(newLeague, teamElements, context);
                    context.Leagues.Add(newLeague);
                    context.SaveChanges();
                }


            }
            Console.WriteLine();
        }
        private static void CreateTeamsIfNotExist(
            FootballEntities context, League league, IEnumerable<XElement> xmlTeams)
        {
            if (xmlTeams == null)
            {
                return;
            }

            foreach (var xmlTeam in xmlTeams)
            {
                string teamName = xmlTeam.Attribute("name").Value;
                var country = xmlTeam.Attribute("country");
                string countryName = null;

                if (country != null)
                {
                    countryName = country.Value;
                }

                var team = context.Teams
                        .FirstOrDefault(t => t.TeamName == teamName && t.Country.CountryName == countryName);

                if (team != null)
                {
                    Console.WriteLine("Existing team: {0} ({1})", team.TeamName, countryName ?? "no country");
                }
                else
                {
                    Team newTeam = new Team();
                    newTeam.TeamName = teamName;
                    newTeam.Country = context.Countries.FirstOrDefault(c => c.CountryName == countryName);
                    context.Teams.Add(newTeam);
                    context.SaveChanges();
                    Console.WriteLine("Created team: {0} ({1})", teamName, countryName ?? "no country");
                    team = newTeam;
                }

                AddTeamToLeagueIfNotExist(context, team, league);
            }
        }