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();
            }
        }
        private static bool AnyAstr(IEnumerable <string> observers)
        {
            if (observers == null)
            {
                return(false);
            }
            var context = new PlanetHuntersContext();

            foreach (var o in observers)
            {
                string[] names = o.Split(',');
                string   fName = names[0];
                string   lName = names[1];
                if (names.Length < 2)
                {
                    return(false);
                }
                if (!context.Astronomers.Any(a => a.FirstName == fName &&
                                             a.LastName == lName))
                {
                    return(false);
                }
            }

            return(true);
        }
Beispiel #3
0
        public static string Planets(string telescopeName)
        {
            using (var context = new PlanetHuntersContext())
            {
                var planetsByTelescope = context.Discoveries
                                         .Where(d => d.TelescopeUsed.Name == telescopeName)
                                         .Select(d => d.Planets
                                                 .Select(s => new
                {
                    Name     = s.Name,
                    Mass     = s.Mass,
                    Orbiting = s.HostStarSystem.Name
                })
                                                 ).ToList();



                JsonSerializerSettings settings = new JsonSerializerSettings
                {
                    Formatting       = Formatting.Indented,
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                };

                var json = JsonConvert.SerializeObject(planetsByTelescope, settings);

                return(json);
            }
        }
        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();
            }
        }
Beispiel #5
0
        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();
            }
        }
Beispiel #6
0
 private static bool StarSystemExists(string starSystemName)
 {
     using (var context = new PlanetHuntersContext())
     {
         return(context.StarSystems.Any(ss => ss.Name == starSystemName));
     }
 }
Beispiel #7
0
        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();
            }
        }
Beispiel #8
0
 public static void InitDB()
 {
     using (var context = new PlanetHuntersContext())
     {
         context.Database.Initialize(true);
     }
 }
Beispiel #9
0
 public static void ImportAstronomers(List <Astronomer> astronomers)
 {
     using (var context = new PlanetHuntersContext())
     {
         context.Astronomers.AddRange(astronomers);
         context.SaveChanges();
     }
 }
Beispiel #10
0
        public static void InitDB()
        {
            // Inizializirame/sazdavame bazata
            var context = new PlanetHuntersContext();

            context.Database.Initialize(true);
            Console.WriteLine("DataBase <PlanetHunters> created!");
        }
Beispiel #11
0
 public static void ImportTelescopes(List <Telescope> telescopes)
 {
     using (var context = new PlanetHuntersContext())
     {
         context.Telescopes.AddRange(telescopes);
         context.SaveChanges();
     }
 }
        public static bool IsPlanetExisting(PlanetHuntersContext context, string planetName)
        {
            Planet planetEntity = context.Planets.FirstOrDefault(planet => planet.Name == planetName);

            if (planetEntity == null)
            {
                return(false);
            }

            return(true);
        }
        public static bool IsStarExisting(PlanetHuntersContext context, string starName)
        {
            Star starEntity = context.Stars.FirstOrDefault(star => star.Name == starName);

            if (starEntity == null)
            {
                return(false);
            }

            return(true);
        }
        public static bool IsTelescopeExisting(PlanetHuntersContext context, string telescopeNameAsString)
        {
            Telescope telescopeEntity = context.Telescopes.FirstOrDefault(telescope => telescope.Name == telescopeNameAsString);

            if (telescopeEntity == null)
            {
                return(false);
            }

            return(true);
        }
Beispiel #15
0
        public static XElement Stars()
        {
            using (var context = new PlanetHuntersContext())
            {
                var stars = context.Stars.Where(s => s.Discovery != null).ToList();

                XElement starsNode = new XElement("Stars");

                foreach (var star in stars)
                {
                    XElement starNode = new XElement("Star");

                    starNode.Add(new XElement("Name", star.Name));
                    starNode.Add(new XElement("Temperature", star.Temperature));
                    starNode.Add(new XElement("StarSystem", star.HostStarSystem.Name));
                    var discoveryNode = new XElement("DiscoveryInfo");
                    //discoveryNode.Add(new XAttribute("DiscoveryDate", star.Discovery.Date.ToString("yyyy'/'MM'/'dd")));
                    discoveryNode.Add(new XAttribute("TelescopeName", star.Discovery.TelescopeUsed.Name));

                    //we dont export the date since the bonus taks changed thing a bit

                    List <Astronomer> astronomersPioneers  = star.Discovery.Pioneers.ToList();
                    List <Astronomer> astronomersObservers = star.Discovery.Observers.ToList();

                    List <Astronomer> allAstronomers = new List <Astronomer>();

                    allAstronomers.AddRange(astronomersObservers);
                    allAstronomers.AddRange(astronomersPioneers);

                    allAstronomers = allAstronomers.OrderBy(a => a.FullName).ToList();

                    foreach (var a in allAstronomers)
                    {
                        XElement astro = new XElement("Astronomer");

                        if (astronomersPioneers.Contains(a))
                        {
                            astro.Add(new XAttribute("Pioneer", true));
                            astro.Value = a.FullName;
                        }
                        else
                        {
                            astro.Add(new XAttribute("Pioneer", false));
                            astro.Value = a.FullName;
                        }
                        discoveryNode.Add(astro);
                    }
                    starNode.Add(discoveryNode);
                    starsNode.Add(starNode);
                }

                return(starsNode);
            }
        }
        public static bool IsStarSystemExisting(PlanetHuntersContext context, string starSystemName)
        {
            StarSystem starSystem = context.StarSystems.FirstOrDefault(system => system.Name == starSystemName);

            if (starSystem == null)
            {
                return(false);
            }

            return(true);
        }
        public static bool IsAstronomerExisting(PlanetHuntersContext context, string firstName, string lastName)
        {
            Astronomer astronomerEntity = context.Astronomers
                                          .FirstOrDefault(astronomer => astronomer.FirstName == firstName && astronomer.LastName == lastName);

            if (astronomerEntity == null)
            {
                return(false);
            }

            return(true);
        }
Beispiel #18
0
        private static ICollection <Star> GetStarsByName(List <string> starNames, PlanetHuntersContext context)
        {
            List <Star> stars = new List <Star>();

            foreach (var sn in starNames)
            {
                var star = context.Stars.FirstOrDefault(s => s.Name == sn);
                stars.Add(star);
            }

            return(stars);
        }
        private static bool AnyStars(IEnumerable <c> stars)
        {
            var context = new PlanetHuntersContext();

            foreach (var s in stars)
            {
                if (!context.Stars.Any(x => x.Name == s.Name))
                {
                    return(false);
                }
            }

            return(true);
        }
        private static bool AnyPlanets(IEnumerable <string> planets)
        {
            var context = new PlanetHuntersContext();

            foreach (var p in planets)
            {
                if (!context.Planets.Any(x => x.Name == p))
                {
                    return(false);
                }
            }

            return(true);
        }
 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 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 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);
     }
 }
Beispiel #26
0
        public static bool ValidateStarsByName(List <string> starNames)
        {
            using (var context = new PlanetHuntersContext())
            {
                bool valid = true;

                foreach (var name in starNames)
                {
                    if (!context.Stars.Any(s => s.Name == name))
                    {
                        valid = false;
                        return(valid);
                    }
                }

                return(valid);
            }
        }
        private static ICollection <Astronomer> GetAst(IEnumerable <string> observers)
        {
            if (observers == null)
            {
                return(null);
            }
            var context = new PlanetHuntersContext();
            var obs     = new List <Astronomer>();

            foreach (var o in observers)
            {
                string[] names = o.Split(',');

                obs.Add(context.Astronomers.FirstOrDefault(a => a.FirstName == names[0] && a.LastName == names[1]));
            }

            return(obs);
        }
Beispiel #28
0
        public static bool ValidateAstronomersByName(List <string> astronomersNames)
        {
            using (var context = new PlanetHuntersContext())
            {
                bool valid = true;

                foreach (var name in astronomersNames)
                {
                    if (!context.Astronomers.Any(a => a.FirstName + " " + a.LastName == name))
                    {
                        valid = false;
                        return(valid);
                    }
                }

                return(valid);
            }
        }
Beispiel #29
0
        public static bool ValidatePlanetsByName(List <string> planetNames)
        {
            using (var context = new PlanetHuntersContext())
            {
                bool valid = true;

                foreach (var name in planetNames)
                {
                    if (!context.Planets.Any(p => p.Name == name))
                    {
                        valid = false;
                        return(valid);
                    }
                }

                return(valid);
            }
        }
        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();
            }
        }