public static void AddAnomalies(IEnumerable <AnomalyDto> anomalies)
 {
     using (var context = new MassDefectEntities())
     {
         foreach (var anomalyDto in anomalies)
         {
             if (anomalyDto.OriginPlanet == null || anomalyDto.TeleportPlanet == null)
             {
                 Console.WriteLine("Error: Invalid data");
             }
             else
             {
                 var originPlanet   = PlanetStore.GetPlanetByName(anomalyDto.OriginPlanet);
                 var teleportPlanet = PlanetStore.GetPlanetByName(anomalyDto.TeleportPlanet);
                 if (originPlanet == null || teleportPlanet == null)
                 {
                     Console.WriteLine("Error: Invalid data");
                 }
                 else
                 {
                     var anomaly = new Anomaly
                     {
                         OriginPlanetId   = originPlanet.Id,
                         TeleportPlanetId = teleportPlanet.Id
                     };
                     context.Anomalies.Add(anomaly);
                     Console.WriteLine($"Successfuly imported Anomaly between {originPlanet.Name} and {teleportPlanet.Name}");
                 }
             }
         }
         context.SaveChanges();
     }
 }
 public static void AddAnomalieWithVictims(List <AnomalyWithVictimsDto> anomalies)
 {
     using (var context = new MassDefectEntities())
     {
         foreach (var anomalyWithVictimsDto in anomalies)
         {
             var originPlanet   = PlanetStore.GetPlanetByName(anomalyWithVictimsDto.OriginPlanet);
             var teleportPlanet = PlanetStore.GetPlanetByName(anomalyWithVictimsDto.TeleportPlanet);
             if (originPlanet == null || teleportPlanet == null)
             {
                 Console.WriteLine("Error: Invalid data");
             }
             else
             {
                 var anomaly = new Anomaly
                 {
                     OriginPlanetId   = originPlanet.Id,
                     TeleportPlanetId = teleportPlanet.Id
                 };
                 context.Anomalies.Add(anomaly);
                 foreach (var victimName in anomalyWithVictimsDto.Victims)
                 {
                     var victim = context.Persons.FirstOrDefault(p => p.Name == victimName);
                     if (victim != null)
                     {
                         anomaly.Victims.Add(victim);
                     }
                 }
             }
         }
     }
 }
Ejemplo n.º 3
0
 public static void AddPersons(IEnumerable <PersonDto> persons)
 {
     using (var context = new MassDefectEntities())
     {
         foreach (var personDto in persons)
         {
             if (personDto.Name == null || personDto.HomePlanet == null)
             {
                 Console.WriteLine("Error: Invalid data");
             }
             else
             {
                 var planet = PlanetStore.GetPlanetByName(personDto.HomePlanet);
                 if (planet == null)
                 {
                     Console.WriteLine("Error: Invalid data");
                 }
                 else
                 {
                     var person = new Person
                     {
                         Name         = personDto.Name,
                         HomePlanetId = planet.Id
                     };
                     context.Persons.Add(person);
                     Console.WriteLine($"Successfuly imported person {person.Name}");
                 }
             }
         }
         context.SaveChanges();
     }
 }