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 Planet GetPlanetByName(string planetName) { using (var ctx = new PlanetHuntersContext()) { return(ctx.Planets.Include("HostStarSystem").FirstOrDefault(p => p.Name == planetName)); } }
public static Telescope GetTelescopeByName(string telescopeName) { using (var ctx = new PlanetHuntersContext()) { return(ctx.Telescopes.FirstOrDefault(t => t.Name == telescopeName)); } }
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 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 bool IsTelescopeExisting(string telescopeName) { using (var ctx = new PlanetHuntersContext()) { return(ctx.Telescopes.Any(t => t.Name == telescopeName)); } }
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 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."); } } } } }
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(); } }
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(); } }
public static void ExportStars() { using (var ctx = new PlanetHuntersContext()) { ICollection <XElement> elements = new List <XElement>(); ctx.Stars.Where(s => s.DiscoveryId != null).ToList().Select(s => new { s.Name, s.TemperatureInKelvin, StarSystemName = s.HostStarSystem.Name, s.Discovery.DateMade, TelescopeName = s.Discovery.TelescopeUsed.Name, Astronomers = s.Discovery.Pioneers.OrderBy(p => p.FirstName) }).ToList().ForEach(s => { XElement element = new XElement("Star", new XElement("Name", s.Name), new XElement("Temeperature", s.TemperatureInKelvin), new XElement("StarSystem", s.StarSystemName), new XElement("DiscoveryInfo", new XAttribute("DiscoveryDate", s.DateMade.ToShortDateString()), new XAttribute("TelescopeName", s.TelescopeName))); elements.Add(element); }); XDocument doc = new XDocument(); doc.Add(new XElement("Stars", elements)); doc.Save("../../../Export/stars.xml"); } }
private void DeserializeAstronomers() { var astronomerDtos = JsonFileOperations.DeserializeJsonCollection <AstronomerDto>(Constants.AstronomerJsonFile); using (PlanetHuntersContext ctx = new PlanetHuntersContext()) { AstronomerRepo repo = new AstronomerRepo(ctx); foreach (var dto in astronomerDtos) { if (dto.FirstName != null && dto.LastName != null) { var a = new Astronomer(); Mapper.Map(dto, a); repo.Insert(a); this.DebugLog.AppendLine($"Record {dto.FirstName} {dto.LastName} successfully imported."); } else { this.DebugLog.AppendLine(Constants.InvalidDataFormat); } } DbUtil.ExecTransaction(ctx); } }
private void DeserializeTelescopes() { var telescopeDtos = JsonFileOperations.DeserializeJsonCollection <TelescopeDto>(Constants.TelescopeJsonFile); using (PlanetHuntersContext ctx = new PlanetHuntersContext()) { TelescopeRepo repo = new TelescopeRepo(ctx); foreach (var dto in telescopeDtos) { if (dto.Name != null && dto.Location != null) { if (dto.MirrorDiameter != null && dto.MirrorDiameter <= 0) { continue; } var t = new Telescope(); Mapper.Map(dto, t); repo.Insert(t); this.DebugLog.AppendLine($"Record {dto.Name} successfully imported."); } else { this.DebugLog.AppendLine(Constants.InvalidDataFormat); } } DbUtil.ExecTransaction(ctx); } }
public static bool IsStarExisting(string starName) { using (var ctx = new PlanetHuntersContext()) { return(ctx.Stars.Any(s => s.Name == starName)); } }
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(); } }
public static bool IsAstronomerExisting(string firstName, string lastName) { using (var ctx = new PlanetHuntersContext()) { return(ctx.Astronomers.Any(a => a.FirstName == firstName && a.LastName == lastName)); } }
public static StarCollectionDto GetFlat() { using (var context = new PlanetHuntersContext()) { var result = context.Stars.Select(s => new StarExportDto { Name = s.Name, Temperature = s.Temperature, StarSystem = s.StarSystem.Name, DiscoveryInfo = new DiscoveryInfo { Astronomers = s.Discovery.Pioneers .Select(d => new AstronomerDto { Name = d.FirstName + " " + d.LastName, Pioneer = true }) .Union(s.Discovery.Observers.Select(c => new AstronomerDto { Name = c.FirstName + " " + c.LastName, Pioneer = false }) .ToList()) .ToList(), DiscoveryDate = s.Discovery.DateMade.ToString(), TelescopeName = s.Discovery.Telescope.Name } }).ToList(); return(new StarCollectionDto { Stars = result }); } }
public static bool IsSystemExisting(string starSystemName) { using (var ctx = new PlanetHuntersContext()) { return(ctx.StarSystems.Any(ss => ss.Name == starSystemName)); } }
public static List <ExportPlanetDTO> GetPlanetsByTelescope(string telescopeName) { using (var context = new PlanetHuntersContext()) { var telescopeEntity = context.Telescopes.Where(t => t.Name == telescopeName).FirstOrDefault(); var discoveries = telescopeEntity.Discoveries; List <ExportPlanetDTO> planets = new List <ExportPlanetDTO>(); foreach (var d in discoveries) { foreach (var p in d.Planets) { var orbiting = new List <string>(); orbiting.Add(p.HostStarSystem.Name); var currentPlanet = new ExportPlanetDTO() { Name = p.Name, Mass = (decimal)p.Mass, Orbiting = orbiting }; planets.Add(currentPlanet); } } planets.Sort((a, b) => - 1 * a.Mass.CompareTo(b.Mass)); return(planets); } }
public static void ExportPlanets() { using (PlanetHuntersContext context = new PlanetHuntersContext()) { Console.WriteLine("Start Exporting Data <Export Planets by Telescope Name> ..."); var TelescopeName = context.Telescopes.ToList(); foreach (Telescope telescope in TelescopeName) { var name = telescope.Name; var exportedPlanets = context.Planets .Where(pl => pl.Discovery.Telescope.Name == name) .Select(planet => new { Name = name, Mass = planet.Mass, Orbit = new { planet.StarSystem.Name } }); var planetAsJson = JsonConvert.SerializeObject(exportedPlanets, Formatting.Indented); var fileName = "planets-by-telescope-" + name + ".json"; Console.WriteLine($"Saving file: <{fileName}> ..."); File.WriteAllText($"../../../PlanetHunters.Export/Exports/{fileName}", planetAsJson); Console.WriteLine("Done!"); } } }
private static void ExportAstronomers(string starSystemName) { using (PlanetHuntersContext context = new PlanetHuntersContext()) { List <AstronomerDto> astronomersFiltered = context.Astronomers .Where(astronomer => astronomer.DiscoveriesMade .Any( discovery => discovery.Planets.Any( planet => planet.HostStarSystem.Name == starSystemName) || discovery.Stars.Any( star => star.HostStarSystem.Name == starSystemName)) || astronomer.DiscoveriesObserved .Any( discovery => discovery.Planets.Any( planet => planet.HostStarSystem.Name == starSystemName) || discovery.Stars.Any( star => star.HostStarSystem.Name == starSystemName))) .OrderBy(astronomer => astronomer.LastName) .Select(astronomer => new AstronomerDto() { Name = astronomer.FirstName + " " + astronomer.LastName, Role = astronomer.DiscoveriesMade.Any( discovery => discovery.Pioneers.Any( pioneer => pioneer.FirstName == astronomer.FirstName && pioneer.LastName == astronomer.LastName)) ? "pioneer" : "observer" }) .ToList(); ExportJsonToFolder(string.Format(Constants.astronomersPath, starSystemName), astronomersFiltered); } }
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(); } } }
private static void ExportStarsWithSerializer() { using (PlanetHuntersContext context = new PlanetHuntersContext()) { List <StarDto> starsFiltered = context.Stars .Where(star => star.DiscoveryId != null) .Select(star => new StarDto() { Name = star.Name, Temperature = star.Temperature, StarSystem = star.HostStarSystem.Name, DiscoveryInfo = new DiscoveryInfoDto() { DiscoveryDate = star.Discovery.DateMade, TelescopeName = star.Discovery.TelesopeUsed.Name, AsronomerPionersDto = star.Discovery.Pioneers .Select(pioner => new AstronomerPioneerDto() { Name = pioner.FirstName + " " + pioner.LastName, Pioneer = true }) .Union(star.Discovery.Observers.Select(obsever => new AstronomerPioneerDto() { Name = obsever.FirstName + " " + obsever.LastName, Pioneer = false }) .ToList()) .ToList(), } }).ToList(); ExportXmlToFolder(starsFiltered, Constants.starsPath, "Stars"); } }
public static StarSystem GetStarSystemByName(string starSystemName) { using (var ctx = new PlanetHuntersContext()) { return(ctx.StarSystems.FirstOrDefault(ss => ss.Name == starSystemName)); } }
public static ICollection <Star> GetStars() { using (var context = new PlanetHuntersContext()) { return(context.Stars.ToList()); } }
public static HashSet <AstroDto> GetAstronomerByStarSystemName(string starSystemName) { using (var ctx = new PlanetHuntersContext()) { var astrota = new HashSet <AstroDto>(); ctx.StarSystems.Include("Planets").First(ss => ss.Name == starSystemName) .Planets .ToList().ForEach(p => { if (p.Discovery != null) { var pioneers = p.Discovery.Pioneers.ToList(); pioneers.ForEach(pi => { astrota.Add(new AstroDto { Name = $"{pi.FirstName} {pi.LastName}", Role = "pioneer" }); }); var observers = p.Discovery.Observers.ToList(); observers.ForEach(pi => { astrota.Add(new AstroDto { Name = $"{pi.FirstName} {pi.LastName}", Role = "observer" }); }); } }); return(astrota); } }
public static bool IsPlanetExisting(string planetName) { using (var ctx = new PlanetHuntersContext()) { return(ctx.Planets.Any(p => p.Name == planetName)); } }
public static Star GetStarByName(string starName) { using (var ctx = new PlanetHuntersContext()) { return(ctx.Stars.Include("HostStarSystem").FirstOrDefault(s => s.Name == starName)); } }
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!"); }