public static void ImportPlanet(string name, double mass, string starSystem) { using (var context = new PlanetHuntersContext()) { Planet planet = new Planet { Name = name, Mass = mass }; if (StarSystemExists(starSystem)) { planet.HostStarSystem = GetStarSystemByName(starSystem, context); } else { planet.HostStarSystem = new StarSystem { Name = starSystem }; } context.Planets.Add(planet); context.SaveChanges(); } }
public static void ImportStar(string name, int temperature, string starSystem) { using (var context = new PlanetHuntersContext()) { Star star = new Star { Name = name, Temperature = temperature }; if (StarSystemExists(starSystem)) { star.HostStarSystem = GetStarSystemByName(starSystem, context); } else { star.HostStarSystem = new StarSystem { Name = starSystem }; } context.Stars.Add(star); context.SaveChanges(); } }
public static void ImportTelescopes(ICollection <TelescopeDTO> telescopes) { using (var context = new PlanetHuntersContext()) { foreach (var telescope in telescopes) { if (telescope.Name == null || telescope.Location == null) { Console.WriteLine("Invalid data format."); continue; } Telescope telescopeEntity = new Telescope() { Name = telescope.Name, Location = telescope.Location, }; if (telescope.MirrorDiameter != null && telescope.MirrorDiameter > 0m) { telescopeEntity.MirrorDiameter = telescope.MirrorDiameter; } context.Telescopes.Add(telescopeEntity); Console.WriteLine($"Successfully imported {nameof(telescopeEntity)} {telescopeEntity.Name}."); } context.SaveChanges(); } }
public static void ImportAstronomers(ICollection <AstronomerDTO> astronomers) { using (var context = new PlanetHuntersContext()) { foreach (var astronomer in astronomers) { if (astronomer.FirstName == null || astronomer.LastName == null) { Console.WriteLine("Invalid data format."); continue; } Astronomer astronomerEntity = new Astronomer() { FirstName = astronomer.FirstName, LastName = astronomer.LastName }; context.Astronomers.Add(astronomerEntity); Console.WriteLine($"Successfully imported {nameof(astronomerEntity)} {astronomerEntity.FirstName}."); } context.SaveChanges(); } }
public static void ImportTelescopes(List <Telescope> telescopes) { using (var context = new PlanetHuntersContext()) { context.Telescopes.AddRange(telescopes); context.SaveChanges(); } }
public static void ImportAstronomers(List <Astronomer> astronomers) { using (var context = new PlanetHuntersContext()) { context.Astronomers.AddRange(astronomers); context.SaveChanges(); } }
public static void AddStarToDatabase(PlanetHuntersContext context, Star starEntity) { try { context.Stars.Add(starEntity); context.SaveChanges(); Console.WriteLine(Messages.StarImported, starEntity.Name); } catch (DbEntityValidationException) { context.Stars.Remove(starEntity); Console.WriteLine(Messages.Error); } }
public static void AddPlanetToDatabase(PlanetHuntersContext context, Planet planetEntity) { try { context.Planets.Add(planetEntity); context.SaveChanges(); Console.WriteLine(Messages.PlanetImported, planetEntity.Name); } catch (DbEntityValidationException) { context.Planets.Remove(planetEntity); Console.WriteLine(Messages.Error); } }
public static void AddTelescopeToDatabase(PlanetHuntersContext context, Telescope telescopeEntity) { try { context.Telescopes.Add(telescopeEntity); context.SaveChanges(); Console.WriteLine(Messages.TelescopeImported, telescopeEntity.Name); } catch (DbEntityValidationException) { context.Telescopes.Remove(telescopeEntity); Console.WriteLine(Messages.Error); } }
public static void AddAstronomerToDatabase(PlanetHuntersContext context, Astronomer astronomerEntity) { try { context.Astronomers.Add(astronomerEntity); context.SaveChanges(); Console.WriteLine(Messages.AstronomerImported, astronomerEntity.FirstName, astronomerEntity.LastName); } catch (DbEntityValidationException) { context.Astronomers.Remove(astronomerEntity); Console.WriteLine(Messages.Error); } }
public static void ImportPlanets(ICollection <PlanetDTO> planets) { using (var context = new PlanetHuntersContext()) { foreach (var planet in planets) { if (planet.Name == null || planet.Mass == null || planet.StarSystem == null || decimal.Parse(planet.Mass) <= 0m) { Console.WriteLine("Invalid data format."); continue; } if (!context.StarSystems.Any(s => s.Name == planet.StarSystem)) { StarSystem starSystemEntity = new StarSystem() { Name = planet.StarSystem, }; context.StarSystems.Add(starSystemEntity); } context.SaveChanges(); Planet planetEntity = new Planet() { Name = planet.Name, Mass = decimal.Parse(planet.Mass), StarSystem = context.StarSystems.First(s => s.Name == planet.StarSystem) }; context.Planets.Add(planetEntity); Console.WriteLine($"Successfully imported {nameof(planetEntity)} {planetEntity.Name}."); } context.SaveChanges(); } }
public static void ImportStars(List <StarDTO> stars) { using (var context = new PlanetHuntersContext()) { foreach (var star in stars) { if (star.Name == null || star.Temperature == null || star.StarSystem == null || int.Parse(star.Temperature) < 2400) { Console.WriteLine("Invalid data format."); continue; } if (!context.StarSystems.Any(s => s.Name == star.StarSystem)) { StarSystem starSystemEntity = new StarSystem() { Name = star.StarSystem, }; context.StarSystems.Add(starSystemEntity); } context.SaveChanges(); Star starEntity = new Star() { Name = star.Name, Temperature = int.Parse(star.Temperature), StarSystem = context.StarSystems.First(s => s.Name == star.StarSystem) }; context.Stars.Add(starEntity); Console.WriteLine($"Successfully imported {nameof(starEntity)} {starEntity.Name}."); } context.SaveChanges(); } }
public static void AddDiscoveryToDatabase(PlanetHuntersContext context, Discovery discoveryEntity) { try { context.Discoveries.Add(discoveryEntity); context.SaveChanges(); Console.WriteLine(Messages.DiscoveryImported, discoveryEntity.DateMade, discoveryEntity.TelesopeUsed.Name, discoveryEntity.Stars.Count, discoveryEntity.Planets.Count, discoveryEntity.Pioneers.Count, discoveryEntity.Observers.Count); } catch (DbEntityValidationException) { context.Discoveries.Remove(discoveryEntity); Console.WriteLine(Messages.Error); } }
public static void ImportDiscoveries(ICollection <DiscoveryDTO> discoveries) { using (var context = new PlanetHuntersContext()) { foreach (var ds in discoveries) { if (ds.DateMade == null || ds.Telescope == null || (ds.Observers == null && ds.Pioneers == null) || ds.Planets == null || ds.Stars == null) { Console.WriteLine("Invalid data format."); continue; } if (!(AnyAstr(ds.Observers) || AnyAstr(ds.Pioneers))) { Console.WriteLine("entity not found 1"); continue; } if (!(AnyPlanets(ds.Planets) || AnyStars(ds.Stars))) { Console.WriteLine("entity not found 2"); continue; } var observers = GetAst(ds.Observers); var pioneers = GetAst(ds.Pioneers); Discovery discoveryEntity = new Discovery() { DateMade = DateTime.ParseExact(ds.DateMade, "yyyy-MM-dd", CultureInfo.InvariantCulture), TelescopeId = context.Telescopes.First(t => t.Name == ds.Telescope).Id, Planets = context.Planets.Where(p => ds.Planets.Contains(p.Name)).ToList(), Stars = context.Stars.Where(p => ds.Stars.Select(s => s.Name).Contains(p.Name)).ToList(), Observers = observers, Pioneers = pioneers }; context.Didscoveries.Add(discoveryEntity); Console.WriteLine($"Successfully imported {nameof(discoveryEntity)} {discoveryEntity.Telescope}."); } context.SaveChanges(); } }
public static void ImportDiscovery(DateTime date, string telescopeName, List <string> starNames, List <string> planetNames, List <string> pioneersNames, List <string> observersNames) { using (var context = new PlanetHuntersContext()) { Discovery discovery = new Discovery { TelescopeUsed = GetTelescopeByName(telescopeName, context), Stars = GetStarsByName(starNames, context), Planets = GetPlanetsByName(planetNames, context), Pioneers = GetAstronomersByName(pioneersNames, context), Observers = GetAstronomersByName(observersNames, context) }; Publication publication = new Publication { Discovery = discovery, ReleaseDate = date }; context.Discoveries.Add(discovery); context.Publications.Add(publication); context.SaveChanges(); } }