Esempio n. 1
0
        private void metroButton2_Click(object sender, EventArgs e)
        {
            SolarSystemContext context = new SolarSystemContext();

            var exportedPlanets = context.Planets
                                  .Where(planet => !planet.OriginAnomalies.Any())
                                  .Select(planet => new
            {
                name = planet.Name
            });
            var planetsAsJson = JsonConvert.SerializeObject(exportedPlanets, Newtonsoft.Json.Formatting.Indented);

            File.WriteAllText("../../plantes.json", planetsAsJson);
        }
Esempio n. 2
0
        private void metroButton5_Click(object sender, EventArgs e)
        {
            SolarSystemContext context    = new SolarSystemContext();
            Repository         repository = new Repository();
            StringBuilder      result     = new StringBuilder();

            var json    = File.ReadAllText(fileLocation);
            var persons = JsonConvert.DeserializeObject <IEnumerable <PersonDTO> >(json);

            foreach (var person in persons)
            {
                string personName = person.Name;
                string planetName = person.HomePlanet;

                if (personName == null || planetName == null)
                {
                    result.AppendLine("Error: Invalid Data.");
                }
                else
                {
                    var personEntity = new Person();
                    var personPlanet = repository.GetPlanetByName(planetName, context);
                    if (personPlanet == null)
                    {
                        result.AppendLine("Error: Invalid Data.");
                    }
                    else
                    {
                        if (repository.GetPersonByName(personName, context) == null)
                        {
                            personEntity.Name       = personName;
                            personEntity.HomePlanet = personPlanet;
                            context.Persons.Add(personEntity);
                            result.AppendLine($"Successfully imported Person {personName}");
                        }
                        else
                        {
                            result.AppendLine("Attempt for duplicate record insertion.");
                        }
                    }
                }
                Logger.Text = result.ToString();
                context.SaveChanges();
            }
        }
Esempio n. 3
0
        private void metroButton1_Click(object sender, EventArgs e)
        {
            SolarSystemContext context = new SolarSystemContext();

            var exportedPlanetsAndPeople = context.Persons
                                           .Where(person => !person.Anomalies.Any())
                                           .Select(person => new
            {
                name       = person.Name,
                homePlanet = new
                {
                    name = person.HomePlanet.Name
                }
            });
            var peopleAsJson = JsonConvert.SerializeObject(exportedPlanetsAndPeople, Newtonsoft.Json.Formatting.Indented);

            File.WriteAllText("../../people.json", peopleAsJson);
        }
Esempio n. 4
0
        private void metroButton6_Click(object sender, EventArgs e)
        {
            SolarSystemContext context    = new SolarSystemContext();
            Repository         repository = new Repository();
            StringBuilder      result     = new StringBuilder();

            var json           = File.ReadAllText(fileLocation);
            var anomalyVictims = JsonConvert.DeserializeObject <IEnumerable <AnomalyVictimsDTO> >(json);

            foreach (var anomalyVictim in anomalyVictims)
            {
                int anomalyId = 0;
                anomalyId = anomalyVictim.Id;
                string victimName = anomalyVictim.Person;
                if (anomalyId == 0 || victimName == null)
                {
                    result.AppendLine("Error: Invalid Data.");
                }
                else
                {
                    var anomalyEntity = repository.GetAnomalyById(anomalyId, context);
                    var personEntity  = repository.GetPersonByName(victimName, context);

                    if (anomalyEntity == null || personEntity == null)
                    {
                        result.AppendLine("Error: Invalid Data.");
                    }
                    else
                    {
                        if (anomalyEntity.Persons.Contains(personEntity))
                        {
                            result.AppendLine("Duplicate data insertion attempt.");
                        }
                        else
                        {
                            anomalyEntity.Persons.Add(personEntity);
                            context.SaveChanges();
                        }
                    }
                }
            }
            Logger.Text = result.ToString();
        }
Esempio n. 5
0
        private void metroButton3_Click(object sender, EventArgs e)
        {
            SolarSystemContext context    = new SolarSystemContext();
            Repository         repository = new Repository();
            StringBuilder      result     = new StringBuilder();

            var json    = File.ReadAllText(fileLocation);
            var planets = JsonConvert.DeserializeObject <IEnumerable <PlanetDTO> >(json);

            foreach (var planet in planets)
            {
                string planetName      = planet.Name;
                string sunName         = planet.Sun;
                string solarSystemName = planet.SolarSystem;

                if (planetName == null || sunName == null || solarSystemName == null)
                {
                    result.AppendLine("Error: Invalid data.");
                }
                else
                {
                    var planetEntity = new Planet()
                    {
                        Name        = planetName,
                        Sun         = repository.GetSunByName(sunName, context),
                        SolarSystem = repository.GetSolarSystemByName(solarSystemName, context)
                    };
                    if (repository.GetPlanetByName(planetName, context) == null)
                    {
                        context.Planets.Add(planetEntity);
                        result.AppendLine($"Successfully imported planet {planetName}.");
                    }
                    else
                    {
                        result.AppendLine("Attempt for duplicate record insertion.");
                    }
                }
            }
            Logger.Text = result.ToString();
            context.SaveChanges();
        }
Esempio n. 6
0
        private void metroButton4_Click(object sender, EventArgs e)
        {
            SolarSystemContext context    = new SolarSystemContext();
            Repository         repository = new Repository();
            StringBuilder      result     = new StringBuilder();

            var json      = File.ReadAllText(fileLocation);
            var anomalies = JsonConvert.DeserializeObject <IEnumerable <AnomaliesDTO> >(json);

            foreach (var anomaly in anomalies)
            {
                string originPlanetName   = anomaly.OriginPlanet;
                string teleportPlanetName = anomaly.TeleportPlanet;
                if (originPlanetName == null || teleportPlanetName == null)
                {
                    result.AppendLine("Error: Invalid Data.");
                }
                else
                {
                    var originPlanet   = repository.GetPlanetByName(originPlanetName, context);
                    var teleportPlanet = repository.GetPlanetByName(teleportPlanetName, context);
                    if (originPlanet == null || teleportPlanet == null)
                    {
                        result.AppendLine("Error: Invalid Data.");
                    }
                    else
                    {
                        var anomalyEntity = new Anomaly()
                        {
                            OriginPlanet   = originPlanet,
                            TeleportPlanet = teleportPlanet
                        };
                        context.Anomalies.Add(anomalyEntity);
                        result.AppendLine("Successfully imported Anomaly.");
                    }
                }
            }
            Logger.Text = result.ToString();
            context.SaveChanges();
        }
Esempio n. 7
0
        private void metroButton7_Click(object sender, EventArgs e)
        {
            SolarSystemContext context = new SolarSystemContext();

            var topAnomaly = context.Anomalies.OrderByDescending(anomaly => anomaly.OriginPlanet.Persons.Count)
                             .Select(anomaly => new
            {
                id           = anomaly.Id,
                originPlanet = new
                {
                    name = anomaly.OriginPlanet.Name
                },
                teleportPlanet = new
                {
                    name = anomaly.TeleportPlanet.Name
                },
                victimsCount = anomaly.OriginPlanet.Persons.Count
            }).Take(1);
            var anomalyAsJson = JsonConvert.SerializeObject(topAnomaly, Newtonsoft.Json.Formatting.Indented);

            File.WriteAllText("../../anomaly.json", anomalyAsJson);
        }
Esempio n. 8
0
        private void metroButton1_Click_1(object sender, EventArgs e)
        {
            SolarSystemContext context    = new SolarSystemContext();
            Repository         repository = new Repository();
            StringBuilder      result     = new StringBuilder();

            var json  = File.ReadAllText(fileLocation);
            var stars = JsonConvert.DeserializeObject <IEnumerable <StarDTO> >(json);

            foreach (var star in stars)
            {
                string starName        = star.Name;
                string solarSystemName = star.SolarSystem;
                if (starName == null || solarSystemName == null)
                {
                    result.AppendLine("Error: Invalid data.");
                }
                else
                {
                    var starEntity = new Star()
                    {
                        Name        = starName,
                        SolarSystem = repository.GetSolarSystemByName(solarSystemName, context)
                    };
                    if (repository.GetSunByName(starName, context) == null)
                    {
                        context.Stars.Add(starEntity);
                        result.AppendLine($"Successfully imported star {starName}.");
                    }
                    else
                    {
                        result.AppendLine("Attempt for duplicate record insertion.");
                    }
                }
            }
            Logger.Text = result.ToString();
            context.SaveChanges();
        }
Esempio n. 9
0
        private void metroButton1_Click(object sender, EventArgs e)
        {
            SolarSystemContext context    = new SolarSystemContext();
            Repository         repository = new Repository();
            StringBuilder      result     = new StringBuilder();

            var json         = File.ReadAllText(fileLocation);
            var solarSystems = JsonConvert.DeserializeObject <IEnumerable <SolarSystemDTO> >(json);

            foreach (var solarSystem in solarSystems)
            {
                string solarSystemName = solarSystem.Name;

                if (solarSystemName == null)
                {
                    result.AppendLine("Solar system name is required.");
                }
                else
                {
                    var solarSystemEntity = new SolarSystem()
                    {
                        Name = solarSystemName
                    };

                    if (repository.GetSolarSystemByName(solarSystemName, context) == null)
                    {
                        context.SolarSystems.Add(solarSystemEntity);
                        result.AppendLine($"Successfully imported Solar System {solarSystemName}.");
                    }
                    else
                    {
                        result.AppendLine("Attempt for duplicate record insertion.");
                    }
                }
            }
            Logger.Text = result.ToString();
            context.SaveChanges();
        }
Esempio n. 10
0
        private void metroButton3_Click(object sender, EventArgs e)
        {
            var context           = new SolarSystemContext();
            var exporetdAnomalies = context.Anomalies
                                    .OrderBy(anomaly => anomaly.Id)
                                    .Select(anomaly => new
            {
                anomalyId          = anomaly.Id,
                originPlanetName   = anomaly.OriginPlanet.Name,
                teleportPlanetName = anomaly.TeleportPlanet.Name,
                victims            = anomaly.Persons.Select(p => new
                {
                    name = p.Name
                })
            });
            var xmlDocument = new XElement("anomalies");

            foreach (var exportedAnomaly in exporetdAnomalies)
            {
                var anomalyNode = new XElement("anomaly");
                anomalyNode.Add(new XAttribute("id", exportedAnomaly.anomalyId));
                anomalyNode.Add(new XAttribute("origin-planet", exportedAnomaly.originPlanetName));
                anomalyNode.Add(new XAttribute("teleport-planet", exportedAnomaly.teleportPlanetName));

                var victimsNode = new XElement("victims");
                foreach (var victim in exportedAnomaly.victims)
                {
                    var victimNode = new XElement("victim");
                    victimNode.Add(new XAttribute("name", victim.name));
                    victimsNode.Add(victimNode);
                }
                anomalyNode.Add(victimsNode);
                xmlDocument.Add(anomalyNode);
            }
            xmlDocument.Save("../../anomalies.xml");
        }
Esempio n. 11
0
        public Person GetPersonByName(string victimName, SolarSystemContext context)
        {
            var personEntity = context.Persons.FirstOrDefault(p => p.Name == victimName);

            return(personEntity);
        }
Esempio n. 12
0
        public SolarSystem GetSolarSystemByName(string solarSystem, SolarSystemContext context)
        {
            var solarSystemEntity = context.SolarSystems.FirstOrDefault(s => s.Name == solarSystem);

            return(solarSystemEntity);
        }
Esempio n. 13
0
        public Star GetSunByName(string sunName, SolarSystemContext context)
        {
            var sunEntity = context.Stars.FirstOrDefault(s => s.Name == sunName);

            return(sunEntity);
        }
Esempio n. 14
0
        public Planet GetPlanetByName(string planetName, SolarSystemContext context)
        {
            var planetEntity = context.Planets.FirstOrDefault(p => p.Name == planetName);

            return(planetEntity);
        }
Esempio n. 15
0
        public Anomaly GetAnomalyById(int anomalyId, SolarSystemContext context)
        {
            var anomalyEntity = context.Anomalies.FirstOrDefault(a => a.Id == anomalyId);

            return(anomalyEntity);
        }
Esempio n. 16
0
        private void metroButton7_Click(object sender, EventArgs e)
        {
            SolarSystemContext context    = new SolarSystemContext();
            Repository         repository = new Repository();
            StringBuilder      result     = new StringBuilder();

            var xml       = XDocument.Load(fileLocation);
            var anomalies = xml.XPathSelectElements("anomalies/anomaly");

            context = new SolarSystemContext();
            foreach (var anomaly in anomalies)
            {
                var originPlanetName   = anomaly.Attribute("origin-planet");
                var teleportPlanetName = anomaly.Attribute("teleport-planet");
                if (originPlanetName == null || teleportPlanetName == null)
                {
                    result.AppendLine("Error: Invalid Data.");
                }
                else
                {
                    var originPlanet   = repository.GetPlanetByName(originPlanetName.Value, context);
                    var teleportPlanet = repository.GetPlanetByName(teleportPlanetName.Value, context);
                    if (originPlanet == null || teleportPlanet == null)
                    {
                        result.AppendLine("Error: Invalid Data.");
                    }
                    else
                    {
                        var anomalyEntity = new Anomaly()
                        {
                            OriginPlanet   = originPlanet,
                            TeleportPlanet = teleportPlanet
                        };
                        context.Anomalies.Add(anomalyEntity);

                        var victims = anomaly.XPathSelectElements("victims/victim");
                        foreach (var victim in victims)
                        {
                            var name = victim.Attribute("name");
                            if (name == null)
                            {
                                result.AppendLine("Error: Invalid Data.");
                            }
                            else
                            {
                                string personName   = name.Value;
                                var    personEntity = repository.GetPersonByName(personName, context);
                                if (personEntity == null)
                                {
                                    result.AppendLine("Error: Invalid Data.");
                                }
                                else
                                {
                                    anomalyEntity.Persons.Add(personEntity);
                                }
                            }
                            context.SaveChanges();
                            result.AppendLine("Successfully imported anomaly.");
                        }
                    }
                }
            }
            Logger.Text = result.ToString();
        }