public static void AddPlanets(IEnumerable <PlanetsDto> planetsDtos)
        {
            Console.WriteLine("Storing <Planets> data in DB ...");
            // Add StarSystems
            using (PlanetHuntersContext context = new PlanetHuntersContext())
            {
                foreach (PlanetsDto planetDto in planetsDtos)
                {
                    if (planetDto.Name == null || planetDto.StarSystem == null || !(planetDto.Mass > 0.0))
                    {
                        Console.WriteLine("ERROR: Invalid data format.");
                    }
                    else
                    {
                        //Console.WriteLine($"StarSystem from FILE: {planetDto.StarSystem} READED.");

                        StarSystem starSystem = context.StarSystems
                                                .Where(ss => ss.Name == planetDto.StarSystem)
                                                .FirstOrDefault();
                        //Console.WriteLine($"CHECK StSys NAME from DB {starSystem.Name} -------------------");

                        if (starSystem == null)
                        {
                            context.StarSystems.Add(new StarSystem()
                            {
                                Name = planetDto.StarSystem
                            });
                            context.SaveChanges();
                            //Console.WriteLine($"StarSystem {planetDto.StarSystem} NOT in DB -> successfully ADDED.");
                        }
                        //else
                        //    {
                        //    Console.WriteLine($"StarSystem {planetDto.StarSystem} ALREADY in DB -> {starSystem.Id}");
                        //    }

                        // ADD Planet
                        starSystem = context.StarSystems
                                     .Where(ss => ss.Name == planetDto.StarSystem)
                                     .FirstOrDefault();

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

                        context.Planets.Add(planet);
                        context.SaveChanges();
                        Console.WriteLine($"Record {planet.Name} successfully imported.");
                    }
                }
            }
            Console.WriteLine("<Planets> table stored!");
        }
        public static void AddStarsFromXML(XElement starsRoot)
        {
            Console.WriteLine("Storing New <Stars> data in DB ...");

            using (PlanetHuntersContext context = new PlanetHuntersContext())
            {
                foreach (XElement starElement in starsRoot.Elements())
                {
                    StarsDto starDto = new StarsDto()
                    {
                        Name        = starElement.Element("Name").Value,
                        Temperature = int.Parse(starElement.Element("Temperature").Value),
                        StarSystem  = starElement.Element("StarSystem").Value
                    };
                    if (starDto.Name == null || starDto.StarSystem == null || !(starDto.Temperature >= 2400))
                    {
                        Console.WriteLine("ERROR: Invalid data format.");
                    }
                    else
                    {
                        StarSystem starSystem = context.StarSystems
                                                .Where(ss => ss.Name == starDto.StarSystem)
                                                .FirstOrDefault();

                        if (starSystem == null)
                        {
                            context.StarSystems.Add(new StarSystem()
                            {
                                Name = starDto.Name
                            });
                            context.SaveChanges();
                        }

                        starSystem = context.StarSystems
                                     .Where(ss => ss.Name == starDto.StarSystem)
                                     .FirstOrDefault();

                        Star star = new Star()
                        {
                            Name         = starDto.Name,
                            Temperature  = starDto.Temperature,
                            StarSystemId = starSystem.Id
                        };

                        context.Stars.Add(star);
                        Console.WriteLine($"Record {star.Name} successfully imported.");
                    }
                }

                context.SaveChanges();
            }      // End USING
            Console.WriteLine("<Stars> table stored!");
        }
        public static void AddAstronomers(IEnumerable <AstronomerDto> astronomers)
        {
            using (var context = new PlanetHuntersContext())
            {
                foreach (var astronomer in astronomers)
                {
                    if (astronomer.FirstName == null ||
                        astronomer.LastName == null ||
                        astronomer.FirstName.Length > Astronomer.FirstNameMaxLength ||
                        astronomer.LastName.Length > Astronomer.LastNameMaxLength)
                    {
                        Console.WriteLine("Invalid data format.");
                    }
                    else
                    {
                        Astronomer newAstronomer = new Astronomer()
                        {
                            FirstName = astronomer.FirstName,
                            LastName  = astronomer.LastName
                        };

                        context.Astronomers.Add(newAstronomer);
                        Console.WriteLine($"Record {astronomer.FirstName} {astronomer.LastName} successfully imported.");
                    }
                }

                context.SaveChanges();
            }
        }
        public static void StoreAstronomers(List <AstronomerDTO> astronomers)
        {
            using (var context = new PlanetHuntersContext())
            {
                foreach (var a in astronomers)
                {
                    if (a.FirstName == null || a.LastName == null)
                    {
                        Console.WriteLine("Invalid data format.");
                        continue;
                    }

                    var currentAstronomer = new Astronomer()
                    {
                        FirstName = a.FirstName,
                        LastName  = a.LastName
                    };

                    try
                    {
                        context.Astronomers.Add(currentAstronomer);
                        context.SaveChanges();
                        Console.WriteLine($"Record {a.FirstName} {a.LastName} successfully imported.");
                    }
                    catch (DbEntityValidationException)
                    {
                        Console.WriteLine("Invalid data format.");
                    }
                }
            }
        }
        public static void AddTelescopes(IEnumerable <TelescopeDto> telescopes)
        {
            using (var context = new PlanetHuntersContext())
            {
                foreach (var telescope in telescopes)
                {
                    if (telescope.Name == null ||
                        telescope.Location == null ||
                        telescope.Name.Length > Telescope.NameMaxLength ||
                        telescope.Location.Length > Telescope.LocationMaxLength ||
                        (telescope.MirrorDiameter <= Telescope.MirrorDiameterMinSize && telescope.MirrorDiameter != null))
                    {
                        Console.WriteLine("Invalid data format.");
                    }
                    else
                    {
                        Telescope newTelescope = new Telescope()
                        {
                            Name           = telescope.Name,
                            Location       = telescope.Location,
                            MirrorDiameter = telescope.MirrorDiameter
                        };

                        context.Telescopes.Add(newTelescope);
                        Console.WriteLine($"Record {telescope.Name} successfully imported.");
                    }
                }

                context.SaveChanges();
            }
        }
示例#6
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();
            }
        }
        public static void AddAstronomers(IEnumerable <AstronomerDto> astronomers)
        {
            using (var context = new PlanetHuntersContext())
            {
                foreach (var astronomer in astronomers)
                {
                    // Validate Firstname
                    if (astronomer.FirstName == null || astronomer.FirstName.Length > 50)
                    {
                        Console.WriteLine(Notifications.ErrorMsg.InvalidFormat);
                        continue;
                    }

                    // Validate LastName
                    if (astronomer.LastName == null || astronomer.LastName.Length > 50)
                    {
                        Console.WriteLine(Notifications.ErrorMsg.InvalidFormat);
                        continue;
                    }

                    // Create Astronomer Entity
                    AddEntityAstronomer(context, astronomer);

                    // Success notification
                    Console.WriteLine(string.Format(Notifications.SuccessMsg.RecordImported, astronomer.FirstName + " " + astronomer.LastName));
                }

                context.SaveChanges();
            }
        }
示例#8
0
        public static void AddStar(XElement starDto)
        {
            using (var ctx = new PlanetHuntersContext())
            {
                try
                {
                    string name           = starDto.Element("Name")?.Value;
                    int    temperature    = int.Parse(starDto.Element("Temperature").Value);
                    string starSystemName = starDto.Element("StarSystem")?.Value;
                    if (name == null || temperature < 2400 || starSystemName == null)
                    {
                        Printer.PrintError();
                        return;
                    }
                    var star = new Star
                    {
                        Name = name,
                        TemperatureInKelvin = temperature
                    };
                    if (!Check.IsSystemExisting(starSystemName))
                    {
                        AddStarSystem(starSystemName);
                    }
                    star.HostStarSystemId = DataLoader.GetStarSystemByName(starSystemName).Id;

                    ctx.Stars.AddOrUpdate(s => s.Name, star);
                    ctx.SaveChanges();
                    Printer.PrintSuccess(star.Name);
                }
                catch (Exception e)
                {
                    Printer.PrintError();
                }
            }
        }
示例#9
0
        public static void AddPlanet(PlanetDto planetDto)
        {
            using (var ctx = new PlanetHuntersContext())
            {
                try
                {
                    var planet = new Planet
                    {
                        Name = planetDto.Name,
                        Mass = planetDto.Mass ?? 0
                    };
                    if (!Check.IsSystemExisting(planetDto.StarSystem))
                    {
                        AddStarSystem(planetDto.StarSystem);
                    }
                    planet.HostStarSystemId = DataLoader.GetStarSystemByName(planetDto.StarSystem).Id;

                    ctx.Planets.Add(planet);
                    ctx.SaveChanges();
                    Printer.PrintSuccess(planet.Name);
                }
                catch (Exception)
                {
                    Printer.PrintError();
                }
            }
        }
        public static void AddTelescopes(IEnumerable <TelescopesDto> telescopesDtos)
        {
            Console.WriteLine("Storing <Telescopes> data in DB ...");
            using (PlanetHuntersContext context = new PlanetHuntersContext())
            {
                foreach (TelescopesDto telescopeDto in telescopesDtos)
                {
                    double telescopeMirrorDiamDouble = telescopeDto.MirrorDiameter == null
                        ? 0
                        : telescopeDto.MirrorDiameter.Value;

                    if (telescopeDto.Name == null || telescopeDto.Location == null || !(telescopeMirrorDiamDouble > 0.0))
                    {
                        Console.WriteLine("ERROR: Invalid data format.");
                    }
                    else
                    {
                        Telescope telescope = new Telescope()
                        {
                            Name           = telescopeDto.Name,
                            Location       = telescopeDto.Location,
                            MirrorDiameter = telescopeMirrorDiamDouble
                        };

                        context.Telescopes.Add(telescope);
                        Console.WriteLine($"Record {telescope.Name} successfully imported.");
                    }
                }

                context.SaveChanges();
            }
            Console.WriteLine("<Telescopes> table stored!");
        }
        public static void AddAstronomers(IEnumerable <AstronomersDto> astronomersDtos)
        {
            Console.WriteLine("Storing <Astronomers> data in DB ...");
            using (PlanetHuntersContext context = new PlanetHuntersContext())
            {
                foreach (AstronomersDto astronomerDto in astronomersDtos)
                {
                    if (astronomerDto.FirstName == null || astronomerDto.LastName == null)
                    {
                        Console.WriteLine("ERROR: Invalid data format.");
                    }
                    else
                    {
                        Astronomer astronomer = new Astronomer()
                        {
                            FirstName = astronomerDto.FirstName,
                            LastName  = astronomerDto.LastName
                        };

                        context.Astronomers.Add(astronomer);
                        Console.WriteLine($"Record {astronomer.FirstName} {astronomer.LastName} successfully imported.");
                    }
                }

                context.SaveChanges();
            }
            Console.WriteLine("<Astronomers> table stored!");
        }
        public static void StoreTelescopes(List <TelescopeDTO> telescopes)
        {
            using (var context = new PlanetHuntersContext())
            {
                foreach (var t in telescopes)
                {
                    if (t.Name == null || t.Location == null || t.MirrorDiameter == null)
                    {
                        Console.WriteLine("Invalid data format.");
                        continue;
                    }

                    try
                    {
                        var currrentTelescope = new Telescope()
                        {
                            Name           = t.Name,
                            Location       = t.Location,
                            MirrorDiameter = (decimal)t.MirrorDiameter
                        };

                        context.Telescopes.Add(currrentTelescope);
                        context.SaveChanges();
                        Console.WriteLine($"Record {t.Name} successfully imported.");
                    }
                    catch (Exception ex)
                    {
                        if (ex is DbEntityValidationException || ex is ArgumentOutOfRangeException)
                        {
                            Console.WriteLine("Invalid data format.");
                        }
                    }
                }
            }
        }
示例#13
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();
            }
        }
示例#14
0
        public static void AddStars(List <StarDto> starsDto)
        {
            using (var context = new PlanetHuntersContext())
            {
                foreach (var star in starsDto)
                {
                    // Validate required input
                    if (star.Name == null || star.Temperature == null || star.StarSystem == null)
                    {
                        Console.WriteLine(Notifications.ErrorMsg.InvalidFormat);
                        continue;
                    }

                    // Validate Name
                    if (star.Name.Length > 255)
                    {
                        Console.WriteLine(Notifications.ErrorMsg.InvalidFormat);
                        continue;
                    }

                    // Validate Mass
                    int  temperature;
                    bool isNumberTemp = int.TryParse(star.Temperature, out temperature);
                    if (!isNumberTemp)
                    {
                        Console.WriteLine(Notifications.ErrorMsg.InvalidFormat);
                        continue;
                    }
                    if (temperature <= 0)
                    {
                        Console.WriteLine(Notifications.ErrorMsg.InvalidFormat);
                        continue;
                    }

                    // Validate StarSystem
                    StarSystem starSystem = context.StarSystems.FirstOrDefault(ss => ss.Name == star.StarSystem);

                    bool isNewStarSystem = false;
                    if (starSystem == null)
                    {
                        isNewStarSystem = true;
                        starSystem      = AddEntityStarSystem(context, star.StarSystem); // Add new Star System
                    }

                    // Create Star Entity
                    AddEntityStar(context, star.Name, temperature, starSystem.Id);

                    // Success notification
                    Console.WriteLine(string.Format(Notifications.SuccessMsg.RecordImported, star.Name));

                    if (isNewStarSystem)
                    {
                        Console.WriteLine(string.Format(Notifications.SuccessMsg.RecordImported, star.StarSystem));
                    }
                }

                context.SaveChanges();
            }
        }
示例#15
0
        public static void AddPlanets(IEnumerable <PlanetDto> planets)
        {
            using (var context = new PlanetHuntersContext())
            {
                foreach (var planet in planets)
                {
                    if (planet.Name == null ||
                        planet.StarSystem == null ||
                        planet.Name.Length > Planet.NameMaxLength ||
                        planet.Mass <= Planet.MassMinSize ||
                        planet.StarSystem.Length > StarSystem.NameMaxLength)
                    {
                        Console.WriteLine("Invalid data format.");
                    }
                    else
                    {
                        var starSystem = context.StarSystems.FirstOrDefault(s => s.Name == planet.StarSystem);
                        if (starSystem == null)
                        {
                            starSystem = new StarSystem()
                            {
                                Name = planet.StarSystem
                            };

                            context.StarSystems.Add(starSystem);
                            context.SaveChanges();
                        }

                        Planet newPlanet = new Planet()
                        {
                            Name             = planet.Name,
                            Mass             = planet.Mass,
                            HostStarSystemId = starSystem.Id
                        };

                        context.Planets.Add(newPlanet);
                        Console.WriteLine($"Record {planet.Name} successfully imported.");
                    }
                }

                context.SaveChanges();
            }
        }
示例#16
0
        public static void AddStars(IEnumerable <StarDto> stars)
        {
            using (var context = new PlanetHuntersContext())
            {
                foreach (var star in stars)
                {
                    if (star.Name == null ||
                        star.StarSystem == null ||
                        star.Name.Length > Star.NameMaxLength ||
                        star.StarSystem.Length > StarSystem.NameMaxLength ||
                        star.Temperature < Star.TemperatureMinValue)
                    {
                        Console.WriteLine("Invalid data format.");
                    }
                    else
                    {
                        var starSystem = context.StarSystems.FirstOrDefault(s => s.Name == star.StarSystem);
                        if (starSystem == null)
                        {
                            starSystem = new StarSystem()
                            {
                                Name = star.StarSystem
                            };

                            context.StarSystems.Add(starSystem);
                            context.SaveChanges();
                        }

                        Star newStar = new Star()
                        {
                            Name             = star.Name,
                            Temperature      = star.Temperature,
                            HostStarSystemId = starSystem.Id
                        };

                        context.Stars.Add(newStar);
                        Console.WriteLine($"Record {star.Name} successfully imported.");
                    }
                }

                context.SaveChanges();
            }
        }
 private static void RestoreDiscoveriesDateMade()
 {
     using (var context = new PlanetHuntersContext())
     {
         foreach (var discovery in context.Discoveries)
         {
             discovery.DateMade = context.Publications.FirstOrDefault(p => p.DiscoveryId == discovery.Id).ReleaseDate;
         }
         context.SaveChanges();
     }
 }
示例#18
0
        private static StarSystem AddStarEntity(PlanetHuntersContext context, string starSystemName)
        {
            StarSystem starSystem = new StarSystem()
            {
                Name = starSystemName
            };

            context.StarSystems.Add(starSystem);
            context.SaveChanges();

            return(starSystem);
        }
示例#19
0
        public static void AddPlanets(IEnumerable <PlanetDto> planets)
        {
            using (var context = new PlanetHuntersContext())
            {
                foreach (var planet in planets)
                {
                    // Validate required input
                    if (planet.Name == null || planet.Mass.ToString() == null || planet.StarSystem == null)
                    {
                        Console.WriteLine(Notifications.ErrorMsg.InvalidFormat);
                        continue;
                    }

                    // Validate Name
                    if (planet.Name.Length > 255)
                    {
                        Console.WriteLine(Notifications.ErrorMsg.InvalidFormat);
                        continue;
                    }

                    // Validate Mass
                    if (planet.Mass <= 0)
                    {
                        Console.WriteLine(Notifications.ErrorMsg.InvalidFormat);
                        continue;
                    }

                    // Validate Star System
                    StarSystem starSystem      = context.StarSystems.FirstOrDefault(s => s.Name == planet.StarSystem);
                    bool       isNewStarSystem = false;

                    if (starSystem == null)
                    {
                        isNewStarSystem = true;
                        starSystem      = AddStarEntity(context, planet.StarSystem); // Create Star Entity
                    }

                    // Create Planet Entity
                    AddPlanetEntity(context, planet.Name, planet.Mass, starSystem.Id);

                    // Success notification Planet & StarSystem
                    Console.WriteLine(string.Format(Notifications.SuccessMsg.RecordImported, planet.Name));

                    if (isNewStarSystem)
                    {
                        Console.WriteLine(string.Format(Notifications.SuccessMsg.RecordImported, planet.StarSystem));
                    }
                }

                context.SaveChanges();
            }
        }
示例#20
0
 public static void AddStarSystems(ICollection <string> starSystems)
 {
     using (var context = new PlanetHuntersContext())
     {
         var existingSystems = context.StarSystems.Select(ss => ss.Name).ToList();
         foreach (var system in starSystems.Where(ss => !existingSystems.Contains(ss)))
         {
             context.StarSystems.Add(new StarSystem {
                 Name = system
             });
         }
         context.SaveChanges();
     }
 }
        public static void AddDiscoveries(IEnumerable <DiscoveryDto> discoveries)
        {
            using (var context = new PlanetHuntersContext())
            {
                foreach (var discovery in discoveries)
                {
                    // 1753 - datetime in SQL can only hold years after 1753
                    if (discovery.DateMade == null ||
                        discovery.DateMade.Year < 1753)
                    {
                        Console.WriteLine("Invalid data format!");
                    }
                    else
                    {
                        var telescope = TelescopeStore.GetTelescopeByName(discovery.Telescope, context);
                        if (telescope == null)
                        {
                            Console.WriteLine("Invalid data format!");
                            continue;
                        }

                        var stars     = GetStars(discovery, context);
                        var planets   = GetPlanets(discovery, context);
                        var pioneers  = GetAstronomes(discovery.Pioneers, context);
                        var observers = GetAstronomes(discovery.Observers, context);

                        Discovery newDiscovery = new Discovery()
                        {
                            DateMade        = discovery.DateMade,
                            TelescopeUsedId = telescope.Id,
                            Stars           = stars,
                            Planets         = planets,
                            Pioneers        = pioneers,
                            Observers       = observers
                        };

                        context.Discoveries.Add(newDiscovery);
                        int    year  = discovery.DateMade.Year;
                        string month = discovery.DateMade.Month.ToString().PadLeft(2, '0');
                        string day   = discovery.DateMade.Day.ToString().PadLeft(2, '0');
                        Console.WriteLine($"Discovery ({year}/{month}/{day}-{discovery.Telescope}) with " +
                                          $"{discovery.Stars.Count} star(s), {discovery.Planets.Count} planet(s), {discovery.Pioneers.Count} pioneer(s) and " +
                                          $"{discovery.Observers.Count} observer(s) successfully imported.");
                    }
                }

                context.SaveChanges();
            }
        }
 private void SeedJournals()
 {
     using (var context = new PlanetHuntersContext())
     {
         context.Journals.AddOrUpdate(j => j.Name,
                                      new Journal {
             Name = "Astronomy"
         },
                                      new Journal {
             Name = "New Astronomy"
         },
                                      new Journal {
             Name = "Astronomy and Astrophysics"
         });
         context.SaveChanges();
     }
 }
示例#23
0
 public static void AddAstronome(Astronomer astronomer)
 {
     using (var ctx = new PlanetHuntersContext())
     {
         try
         {
             ctx.Astronomers.AddOrUpdate(a => new { a.FirstName, a.LastName }, astronomer);
             ctx.SaveChanges();
             Printer.PrintSuccess(astronomer.FirstName + " " + astronomer.LastName);
         }
         catch (Exception)
         {
             ctx.Astronomers.Remove(astronomer);
             Printer.PrintError();
         }
     }
 }
 private void SeedPublications()
 {
     using (var context = new PlanetHuntersContext())
     {
         var discoveries = context.Discoveries;
         foreach (var discovery in discoveries)
         {
             context.Publications.AddOrUpdate(p => new { p.ReleaseDate, p.DiscoveryId },
                                              new Publication
             {
                 JournalId   = context.Journals.FirstOrDefault(j => j.Name == "Astronomy").Id,
                 ReleaseDate = discovery.DateMade,
                 DiscoveryId = discovery.Id
             });
         }
         context.SaveChanges();
     }
 }
示例#25
0
 private static void AddStarSystem(string starSystemName)
 {
     using (var ctx = new PlanetHuntersContext())
     {
         try
         {
             ctx.StarSystems.Add(new StarSystem
             {
                 Name = starSystemName
             });
             ctx.SaveChanges();
             Printer.PrintSuccess(starSystemName);
         }
         catch (Exception)
         {
             Printer.PrintError();
         }
     }
 }
示例#26
0
        private void DeserializeStars()
        {
            var xmlDocument = XDocument.Load(Constants.StarXmlFile).Root.Elements();

            using (PlanetHuntersContext ctx = new PlanetHuntersContext())
            {
                StarSystemRepo syRepo = new StarSystemRepo(ctx);
                StarRepo       stRepo = new StarRepo(ctx);

                foreach (var starElem in xmlDocument)
                {
                    StringReader  reader     = new StringReader(starElem.ToString());
                    XmlSerializer serializer = new XmlSerializer(typeof(StarDto));
                    var           starDto    = (StarDto)serializer.Deserialize(reader);

                    if (starDto.Temperature > 2400 && starDto.Name != null && starDto.StarSystem != null)
                    {
                        var starSystem = syRepo.SearchFor(s => s.Name == starDto.StarSystem).FirstOrDefault();
                        if (starSystem == null)
                        {
                            starSystem = new StarSystem()
                            {
                                Name = starDto.StarSystem
                            }
                        }
                        ;

                        var star = new Star();
                        Mapper.Map(starDto, star);
                        star.HostStarSystem = starSystem;

                        stRepo.Insert(star);
                        ctx.SaveChanges();

                        this.DebugLog.AppendLine($"Record {star.Name} successfully imported.");
                    }
                    else
                    {
                        this.DebugLog.AppendLine(Constants.InvalidDataFormat);
                    }
                }
            }
        }
 public static void AddAstronomers(ICollection <Astronomer> astronomers)
 {
     using (var context = new PlanetHuntersContext())
     {
         foreach (var astronomer in astronomers)
         {
             if (astronomer.FirstName == null || astronomer.LastName == null)
             {
                 Console.WriteLine("Invalid data");
             }
             else
             {
                 context.Astronomers.Add(astronomer);
                 Console.WriteLine($"Record {astronomer.FirstName} {astronomer.LastName} successfully imported.");
             }
         }
         context.SaveChanges();
     }
 }
 public static void AddTelescopes(ICollection <Telescope> telescopes)
 {
     using (var context = new PlanetHuntersContext())
     {
         foreach (var telescope in telescopes)
         {
             if (telescope.Name == null || telescope.Location == null || telescope.MirrorDiameter <= 0)
             {
                 Console.WriteLine("Invalid data");
             }
             else
             {
                 context.Telescopes.Add(telescope);
                 Console.WriteLine($"Record {telescope.Name} successfully imported.");
             }
         }
         context.SaveChanges();
     }
 }
示例#29
0
        private void DeserializePlanets()
        {
            var planetDtos = JsonFileOperations.DeserializeJsonCollection <PlanetDto>(Constants.PlanetJsonFile);

            using (PlanetHuntersContext ctx = new PlanetHuntersContext())
            {
                StarSystemRepo stRepo = new StarSystemRepo(ctx);
                PlanetRepo     plRepo = new PlanetRepo(ctx);

                foreach (var dto in planetDtos)
                {
                    if (dto.Mass > 0 && dto.Name != null && dto.StarSystem != null)
                    {
                        var planet = new Planet();
                        Mapper.Map(dto, planet);

                        var starSystem = stRepo.SearchFor(s => s.Name == dto.StarSystem).FirstOrDefault();

                        if (starSystem == null)
                        {
                            starSystem = new StarSystem()
                            {
                                Name = dto.StarSystem
                            }
                        }
                        ;

                        starSystem.Planets.Add(planet);
                        planet.HostStarSystem = starSystem;

                        plRepo.Insert(planet);
                        ctx.SaveChanges();

                        this.DebugLog.AppendLine($"Record {dto.Name} successfully imported.");
                    }
                    else
                    {
                        this.DebugLog.AppendLine(Constants.InvalidDataFormat);
                    }
                }
            }
        }
        public static void StoreStars(IEnumerable <XElement> starsXml)
        {
            using (var context = new PlanetHuntersContext())
            {
                foreach (var s in starsXml)
                {
                    var name           = s.Element("Name");
                    var temperature    = s.Element("Temperature");
                    var starSystemName = s.Element("StarSystem");

                    if (name == null || temperature == null || starSystemName == null)
                    {
                        Console.WriteLine("Invalid data format.");
                        continue;
                    }

                    var starSystemEntity = context.StarSystems.FirstOrDefault(sm => sm.Name == starSystemName.Value);

                    var currentStar = new Star()
                    {
                        Name           = name.Value,
                        Temperature    = int.Parse(temperature.Value),
                        HostStarSystem = starSystemEntity == null ? new StarSystem()
                        {
                            Name = starSystemName.Value
                        } : starSystemEntity
                    };

                    try
                    {
                        context.Stars.Add(currentStar);
                        context.SaveChanges();
                        Console.WriteLine($"Record {name.Value} successfully imported.");
                    }
                    catch (DbEntityValidationException)
                    {
                        Console.WriteLine("Invalid data format.");
                    }
                }
            }
        }