Exemple #1
0
        private static void ImportAnomalyToDatabase(AnomalyDto anomalyDto)
        {
            if (anomalyDto.OriginPlanet == null)
            {
                throw new ArgumentNullException("Origin planet cannot be null!");
            }
            else if (anomalyDto.TeleportPlanet == null)
            {
                throw new ArgumentNullException("Teleport planet cannot be null!");
            }

            using (var db = new MassDefectDatabaseContext())
            {
                var originPlanet = db.Planets.FirstOrDefault(p => p.Name == anomalyDto.OriginPlanet);

                var teleportPlanet = db.Planets.FirstOrDefault(p => p.Name == anomalyDto.TeleportPlanet);

                if (originPlanet == null || teleportPlanet == null)
                {
                    throw new ArgumentNullException("Origin and teleport planet doesn't exist in dbo.Planets");
                }

                var newAnomaly = new Anomaly()
                {
                    OriginPlanet   = originPlanet,
                    TeleportPlanet = teleportPlanet
                };

                db.Anomalies.Add(newAnomaly);

                db.SaveChanges();
            }
        }
        public static void ImportStars()
        {
            var context = new MassDefectDatabaseContext();
            var json    = File.ReadAllText(@"..\..\Datasets\stars.json");
            var stars   = JsonConvert.DeserializeObject <IEnumerable <StarDTO> >(json);

            foreach (var star in stars)
            {
                if (star.name == null || context.Stars.Any(s => s.Name == star.name))
                {
                    Console.WriteLine("Error: Invalid data.");
                }
                else
                {
                    int?currentStarSolarSystemId = null;
                    int?hasSolarSystem           = GetSolarSystemId(star.solarSystem);
                    if (hasSolarSystem != null)
                    {
                        currentStarSolarSystemId = hasSolarSystem;
                    }
                    var currentStar = new Star()
                    {
                        Name          = star.name,
                        SolarSystemId = currentStarSolarSystemId
                    };
                    context.Stars.Add(currentStar);
                    Console.WriteLine("Successfully imported Star {0}.", star.name);
                }
            }
            context.SaveChanges();
        }
Exemple #3
0
        private static void ImportStarToDatabase(StarDto starDto)
        {
            var db = new MassDefectDatabaseContext();

            if (starDto == null)
            {
                throw new ArgumentNullException("Star cannot be null!");
            }
            else if (starDto.Name == null)
            {
                throw new ArgumentNullException("StarName cannot be null!");
            }
            else if (starDto.SolarSystem == null)
            {
                throw new ArgumentNullException("SolarSystem cannot be null!");
            }

            var solarSystem = db.SolarSystems.FirstOrDefault(s => s.Name == starDto.SolarSystem);

            if (solarSystem == null)
            {
                throw new ArgumentNullException("Solar system doesn't exists!");
            }

            var newStar = new Star()
            {
                Name        = starDto.Name,
                SolarSystem = solarSystem
            };

            db.Stars.Add(newStar);

            db.SaveChanges();
        }
Exemple #4
0
        private static void ImportPersonToDatabase(PersonDto personDto)
        {
            if (personDto == null)
            {
                throw new ArgumentNullException("Person cannot be null!");
            }
            else if (personDto.Name == null)
            {
                throw new ArgumentNullException("Person name cannot be null!");
            }
            else if (personDto.HomePlanet == null)
            {
                throw new ArgumentNullException("Person home planet cannot be null!");
            }

            using (var db = new MassDefectDatabaseContext())
            {
                var homePlanet = db.Planets.FirstOrDefault(p => p.Name == personDto.HomePlanet);

                if (homePlanet == null)
                {
                    throw new ArgumentNullException("Person home planet doesn't exist in dbo.Planets");
                }

                var newPerson = new Person()
                {
                    Name       = personDto.Name,
                    HomePlanet = homePlanet
                };

                db.Persons.Add(newPerson);

                db.SaveChanges();
            }
        }
        public static void ImportAnomalityVictims()
        {
            var context = new MassDefectDatabaseContext();
            var json    = File.ReadAllText(@"..\..\Datasets\anomaly-victims.json");
            var victims = JsonConvert.DeserializeObject <IEnumerable <VictimDTO> >(json);

            foreach (var victim in victims)
            {
                if (victim.id == null || victim.person == null)
                {
                    Console.WriteLine("Error: Invalid data.");
                }
                else
                {
                    if (context.Anomalies.Any(a => a.Id == victim.id) || GetPersonId(victim.person) != null)
                    {
                        int?tempId = GetPersonId(victim.person);
                        if (tempId.HasValue)
                        {
                            int    victimId    = tempId.Value;
                            Person victimToAdd = context.People.FirstOrDefault(p => p.Id == victimId);
                            context.Anomalies.First(v => v.Id == victim.id).Victims.Add(victimToAdd);
                        }
                    }
                    else
                    {
                        continue;
                    }
                }
            }
            context.SaveChanges();
        }
        public static void ImportAnomalities()
        {
            var context   = new MassDefectDatabaseContext();
            var json      = File.ReadAllText(@"..\..\Datasets\anomalies.json");
            var anomalies = JsonConvert.DeserializeObject <IEnumerable <AnomalyDTO> >(json);

            foreach (var anomaly in anomalies)
            {
                if (anomaly.originPlanet == null || anomaly.teleportPlanet == null || GetPlanetId(anomaly.originPlanet) == null || GetPlanetId(anomaly.teleportPlanet) == null)
                {
                    Console.WriteLine("Error: Invalid data.");
                }
                else
                {
                    var currentAnomaly = new Anomaly()
                    {
                        OriginPlanetId   = GetPlanetId(anomaly.originPlanet),
                        TeleportPlanetId = GetPlanetId(anomaly.teleportPlanet)
                    };
                    context.Anomalies.Add(currentAnomaly);
                    Console.WriteLine("Successfully imported anomaly");
                }
            }
            context.SaveChanges();
        }
        public static void ImportPeople()
        {
            var context = new MassDefectDatabaseContext();
            var json    = File.ReadAllText(@"..\..\Datasets\persons.json");
            var people  = JsonConvert.DeserializeObject <IEnumerable <PersonDTO> >(json);

            foreach (var person in people)
            {
                if (person.name == null)
                {
                    Console.WriteLine("Error: Invalid data.");
                }
                else
                {
                    int?currentHomePlanetId = null;
                    int?homePlanetId        = GetPlanetId(person.homePlanet);
                    if (homePlanetId != null)
                    {
                        currentHomePlanetId = homePlanetId;
                    }
                    var currentPerson = new Person()
                    {
                        Name         = person.name,
                        HomePlanetId = currentHomePlanetId,
                    };
                    context.People.Add(currentPerson);
                    Console.WriteLine("Successfully imported Person {0}.", person.name);
                }
            }
            context.SaveChanges();
        }
Exemple #8
0
        private static void ImportPlanetToDatabase(PlanetDto planetDto)
        {
            var db = new MassDefectDatabaseContext();

            if (planetDto == null)
            {
                throw new ArgumentNullException("Planet cannot be null!");
            }
            else if (planetDto.Name == null)
            {
                throw new ArgumentNullException("Planet name cannot be null!");
            }
            else if (planetDto.Sun == null)
            {
                throw new ArgumentNullException("Planet sun cannot be null!");
            }
            else if (planetDto.SolarSystem == null)
            {
                throw new ArgumentNullException("Planet solar system cannot be null!");
            }

            var sun = db.Stars.FirstOrDefault(s => s.Name == planetDto.Sun);

            var solarSystem = db.SolarSystems.FirstOrDefault(s => s.Name == planetDto.SolarSystem);

            if (sun == null)
            {
                throw new ArgumentNullException("Sun doesn't exist in dbo.Stars");
            }
            else if (solarSystem == null)
            {
                throw new ArgumentNullException("Solar system doesn't exist in dbo.SolarSystems");
            }

            var newPlanet = new Planet()
            {
                Name        = planetDto.Name,
                Sun         = sun,
                SolarSystem = solarSystem
            };

            db.Planets.Add(newPlanet);

            db.SaveChanges();
        }
Exemple #9
0
        private static void ImportSolarSystemToDatabase(SolarSystemDto solarSystemDto)
        {
            if (solarSystemDto?.Name == null)
            {
                throw new ArgumentNullException("Name cannot be null!");
            }

            SolarSystem newSolarSystem = new SolarSystem()
            {
                Name = solarSystemDto.Name
            };

            using (var db = new MassDefectDatabaseContext())
            {
                db.SolarSystems.Add(newSolarSystem);
                db.SaveChanges();
            }
        }
Exemple #10
0
        private static void ImportAnomalyVictimToDatabase(AnomalyVictim anomalyVictim)
        {
            if (anomalyVictim.Person == null || anomalyVictim.Id == 0)
            {
                throw new ArgumentNullException("Person cannot be null and Id cannot be 0!");
            }

            using (var db = new MassDefectDatabaseContext())
            {
                var anomaly = db.Anomalies.FirstOrDefault(a => a.Id == anomalyVictim.Id);

                var victim = db.Persons.FirstOrDefault(p => p.Name == anomalyVictim.Person);

                if (anomaly != null && victim != null)
                {
                    anomaly.Victims.Add(victim);

                    db.SaveChanges();
                }
            }
        }
        public static void ImportPlanets()
        {
            var context = new MassDefectDatabaseContext();
            var json    = File.ReadAllText(@"..\..\Datasets\planets.json");
            var planets = JsonConvert.DeserializeObject <IEnumerable <PlanetDTO> >(json);

            foreach (var planet in planets)
            {
                if (planet.name == null || context.Planets.Any(p => p.Name == planet.name))
                {
                    Console.WriteLine("Error: Invalid data.");
                }
                else
                {
                    int?currentPlanetSolarSystemId = null;
                    int?currentPlanetSunId         = null;
                    int?solarSystemId = GetSolarSystemId(planet.solarSystem);
                    int?sunId         = GetStarId(planet.sun);
                    if (solarSystemId != null)
                    {
                        currentPlanetSolarSystemId = solarSystemId;
                    }
                    if (sunId != null)
                    {
                        currentPlanetSunId = sunId;
                    }
                    var currentPlanet = new Planet()
                    {
                        Name          = planet.name,
                        SolarSystemId = currentPlanetSolarSystemId,
                        SunId         = currentPlanetSunId
                    };
                    context.Planets.Add(currentPlanet);
                    Console.WriteLine("Successfully imported Planet {0}.", planet.name);
                }
            }
            context.SaveChanges();
        }
Exemple #12
0
        private static void ImportAnomalyAndVictims(XElement anomalyNode, MassDefectDatabaseContext db)
        {
            XAttribute originPlanetName   = anomalyNode.Attribute("origin-planet");
            XAttribute teleportPlanetName = anomalyNode.Attribute("teleport-planet");

            if (originPlanetName == null || teleportPlanetName == null)
            {
                throw new ArgumentNullException("Origin and teleport planet cannot be null!");
            }

            var originPlanet   = GetPlanetByName(originPlanetName.Value, db);
            var teleportPlanet = GetPlanetByName(teleportPlanetName.Value, db);

            if (originPlanet == null || teleportPlanet == null)
            {
                throw new ArgumentNullException("Origin or teleport planet doesn't exist in dbo.Planets!");
            }

            var newAnomaly = new Anomaly()
            {
                OriginPlanet   = originPlanet,
                TeleportPlanet = teleportPlanet
            };

            db.Anomalies.Add(newAnomaly);

            //Victims
            var victims = anomalyNode.XPathSelectElements("victims/victim");

            foreach (var victim in victims)
            {
                ImportVictim(victim, db, newAnomaly);
            }

            db.SaveChanges();
        }
        public static void ImportSolarSystems()
        {
            var context      = new MassDefectDatabaseContext();
            var json         = File.ReadAllText(@"..\..\Datasets\solar-systems.json");
            var solarSystems = JsonConvert.DeserializeObject <IEnumerable <SolarSystemDTO> >(json);

            foreach (var solarSystem in solarSystems)
            {
                if (solarSystem.name == null || context.SolarSystems.Any(s => s.Name == solarSystem.name))
                {
                    Console.WriteLine("Error: Invalid data.");
                }
                else
                {
                    var currentSolarSystem = new SolarSystem()
                    {
                        Name = solarSystem.name
                    };
                    context.SolarSystems.Add(currentSolarSystem);
                    Console.WriteLine("Successfully imported Solar System {0}.", solarSystem.name);
                }
            }
            context.SaveChanges();
        }