private static void ImportPlanets() { using (PlanetHuntersContext context = new PlanetHuntersContext()) { List <PlanetDto> planetDtos = ParseJson <PlanetDto>(Constants.PlanetsPath); foreach (PlanetDto planetDto in planetDtos) { if (planetDto.Name == null || planetDto.Name.Length > 255) { Console.WriteLine(Messages.Error); continue; } if (planetDto.Mass < 0.0001f) { Console.WriteLine(Messages.Error); continue; } Planet planetEntity = new Planet() { Name = planetDto.Name, Mass = planetDto.Mass }; if (HelperMethods.IsStarSystemExisting(context, planetDto.StarSystem)) { planetEntity.HostStarSystem = HelperMethods.GetStarSystem(context, planetDto.StarSystem); } else { if (planetDto.StarSystem == null || planetDto.StarSystem.Length > 255) { Console.WriteLine(Messages.Error); continue; } StarSystem starSystemEntity = new StarSystem() { Name = planetDto.StarSystem }; HelperMethods.AddStarSystemToDatabase(context, starSystemEntity); planetEntity.HostStarSystem = starSystemEntity; } HelperMethods.AddPlanetToDatabase(context, planetEntity); } } }
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); } } }