Exemplo n.º 1
0
        private static void ImportPlanets(SystemContext context)
        {
            var json       = File.ReadAllText("../../Import/Json/planets.json");
            var planetsDto = JsonConvert.DeserializeObject <List <PlanetDto> >(json);

            foreach (var planetDto in planetsDto)
            {
                if (planetDto.Mass > 0)
                {
                    if (!DM.IsStarSystemExisting(planetDto.Name))
                    {
                        DM.CreateStarSystem(planetDto.StarSystem);
                    }
                    var starSystem = DM.GetStarSystem(planetDto.StarSystem);

                    Planet planet = new Planet()
                    {
                        Name       = planetDto.Name,
                        Mass       = planetDto.Mass,
                        StarSystem = starSystem,
                    };

                    context.Planets.Add(planet);
                    context.StarSystems.FirstOrDefault(ss => ss.Name == starSystem.Name).Planets.Add(planet);
                    context.SaveChanges();

                    Console.WriteLine($"Record {planetDto.Name} successfully imported.");
                }
                else
                {
                    Console.WriteLine("Error: Invalid data input.");
                }
            }
        }
Exemplo n.º 2
0
        private static void ImportStars(SystemContext context)
        {
            var      xml    = XDocument.Load("../../Import/Xml/stars.xml");
            XElement import = xml.Root;

            foreach (var star in import.Elements())
            {
                string name        = star.Element("Name").Value;
                int    temperature = int.Parse(star.Element("Temperature").Value);
                string starSystem  = star.Element("StarSystem").Value;

                if (temperature > 2400)
                {
                    if (!DM.IsStarSystemExisting(starSystem))
                    {
                        DM.CreateStarSystem(starSystem);
                    }

                    var ss = DM.GetStarSystem(starSystem);

                    Star s = new Star()
                    {
                        Name        = name,
                        Temperature = temperature,
                        StarSystem  = ss,
                    };
                    Console.WriteLine($"Record {name} successfully imported.");
                    context.Stars.Add(s);
                    context.StarSystems.FirstOrDefault(w => w.Name == ss.Name).Stars.Add(s);
                }
                else
                {
                    Console.WriteLine("Error: Invalid data.");
                }
            }
            context.SaveChanges();
        }