Exemple #1
0
        private static void ImportStars()
        {
            XDocument documentXml = XDocument.Load(Constants.StarsPath);

            IEnumerable <XElement> starsNode = documentXml.XPathSelectElements("Stars/Star");

            using (PlanetHuntersContext context = new PlanetHuntersContext())
            {
                foreach (XElement starNode in starsNode)
                {
                    string starName = starNode.Element("Name")?.Value;
                    string starTemperatureAsString = starNode.Element("Temperature")?.Value;
                    string starSystemName          = starNode.Element("StarSystem")?.Value;

                    if (starName == null || starName.Length > 255)
                    {
                        Console.WriteLine(Messages.Error);
                        continue;
                    }

                    if (starTemperatureAsString == null)
                    {
                        Console.WriteLine(Messages.Error);
                        continue;
                    }

                    int starTemperature = int.Parse(starTemperatureAsString);
                    if (starTemperature < 2400)
                    {
                        Console.WriteLine(Messages.Error);
                        continue;
                    }

                    Star starEntity = new Star()
                    {
                        Name        = starName,
                        Temperature = starTemperature
                    };

                    if (HelperMethods.IsStarSystemExisting(context, starSystemName))
                    {
                        starEntity.HostStarSystem = HelperMethods.GetStarSystem(context, starSystemName);
                    }
                    else
                    {
                        if (starSystemName == null || starSystemName.Length > 255)
                        {
                            Console.WriteLine(Messages.Error);
                            continue;
                        }

                        StarSystem starSystemEntity = new StarSystem()
                        {
                            Name = starSystemName
                        };

                        HelperMethods.AddStarSystemToDatabase(context, starSystemEntity);
                        starEntity.HostStarSystem = starSystemEntity;
                    }

                    HelperMethods.AddStarToDatabase(context, starEntity);
                }
            }
        }