Esempio n. 1
0
        public static void AddPlanets(ICollection <PlanetImportDto> planets)
        {
            var filteredPlanets = new List <PlanetImportDto>();

            foreach (var planetDto in planets)
            {
                if (planetDto.Name == null || planetDto.Mass <= 0 || planetDto.StarSystem == null)
                {
                    Console.WriteLine("Invalid data");
                }
                else
                {
                    filteredPlanets.Add(planetDto);
                    Console.WriteLine($"Record {planetDto.Name} successfully imported.");
                }
            }

            StarSystemStore.AddStarSystems(filteredPlanets.Select(p => p.StarSystem).Distinct().ToList());

            using (var context = new PlanetHuntersContext())
            {
                context.Planets.AddRange(filteredPlanets.Select(p => new Planet
                {
                    Name         = p.Name,
                    Mass         = p.Mass,
                    StarSystemId = StarSystemStore.GetSystemByName(p.StarSystem, context).Id
                }));
                context.SaveChanges();
            }
        }
Esempio n. 2
0
        public static void AddStars(ICollection <StarImportDto> stars)
        {
            var filteredStars = new List <StarImportDto>();

            foreach (var starDto in stars)
            {
                if (starDto.Name == null || starDto.Temperature < 2400 || starDto.StarSystem == null)
                {
                    Console.WriteLine("Invalid data");
                }
                else
                {
                    filteredStars.Add(starDto);
                    Console.WriteLine($"Record {starDto.Name} successfully imported.");
                }
            }

            StarSystemStore.AddStarSystems(filteredStars.Select(s => s.StarSystem).Distinct().ToList());

            using (var context = new PlanetHuntersContext())
            {
                context.Stars.AddRange(filteredStars.Select(s => new Star
                {
                    Name         = s.Name,
                    Temperature  = s.Temperature,
                    StarSystemId = StarSystemStore.GetSystemByName(s.StarSystem, context).Id
                }));
                context.SaveChanges();
            }
        }