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 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 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;
        }
Exemplo n.º 5
0
        private static Team GetTeamEntity(FootballEntities context, string name, string country)
        {
            Team teamEntity = context.Teams.FirstOrDefault(t => t.TeamName == name && t.Country.CountryName == country);

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

            return teamEntity;
        }
Exemplo n.º 6
0
        public static void Main()
        {
            var context = new FootballEntities();

            var doc = XDocument.Load(ImportFilePath);
            var leagues = doc.Descendants("league");

            var i = 1;

            foreach (XElement league in leagues)
            {
               Console.WriteLine("Processing league #{0} ...", i);
                var leagueName = league.Element("league-name");
                League leagueEntity = leagueName != null ? GetLeagueEntity(context, leagueName) : null;

                foreach (XElement team in league.XPathSelectElements("teams/team"))
                {
                    var name = team.Attribute("name").Value;
                    var countryNode = team.Attribute("country");
                    var country = countryNode != null ? countryNode.Value : null;

                    Team teamEntity = GetTeamEntity(context, name, country);

                    if (leagueName != null)
                    {
                        var existingTeam = leagueEntity.Teams.FirstOrDefault(t => t == teamEntity);
                        if (existingTeam == null)
                        {
                            teamEntity.Leagues.Add(leagueEntity);
                            context.SaveChanges();
                            Console.WriteLine("Added team to league: {0} to league {1}", name, leagueName.Value);
                        }
                        else
                        {
                            Console.WriteLine("Existing team in league: {0} belongs to {1}", name, leagueName.Value);
                        }
                    }
                }

                i++;
                Console.WriteLine();
            }
        }
        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 void AddTeams(XElement leagueElement)
        {
            var context = new FootballEntities();

            foreach (var teamElement in leagueElement.XPathSelectElements("teams/team"))
            {
                if (teamElement.Attribute("name") == null)
                {
                    throw new ArgumentException("Team name is required.");
                }

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

                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");
                }
                else
                {
                    var newTeam = new Team
                    {
                        Country = context.Countries.FirstOrDefault(c => c.CountryName == currTeamCountryName),
                        TeamName = teamName
                    };

                    context.Teams.Add(newTeam);
                    context.SaveChanges();
                    Console.WriteLine("Created team: {0} ({1})", newTeam.TeamName,
                        newTeam.Country != null ? newTeam.Country.CountryName : "no country");
                }
            }
        }
        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);
            }
        }