Beispiel #1
0
        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);
                }
            }
        }