Example #1
0
        public static void ProcessErgastCountries()
        {
            string fileLocation = Seed.baseLocation + "countries.csv";
            var    data         = new List <Country>();
            var    headers      = new List <string>();

            using (var sr = new StreamReader(fileLocation))
            {
                headers = sr.ReadLine().Split(',').ToList();

                data = File.ReadAllLines(fileLocation)
                       .Skip(1)
                       .Select(x => Country.FromCsv(x))
                       .ToList();
            }

            using (var db = new F1EncyclopediaContext())
            {
                foreach (var c in data)
                {
                    db.Countries.AddIfNotExists(c, x => x.Name == c.Name);
                }
                db.SaveChanges();
            }
        }
Example #2
0
        public static void ProcessErgastDrivers()
        {
            var fileLocation = Seed.baseLocation + "drivers.csv";
            var counter      = 0;
            var length       = 0;
            var docOpen      = true;

            var data = new List <Person>();

            using (var db = new F1EncyclopediaContext())
            {
                while (docOpen)
                {
                    try
                    {
                        // Checks file is currently open.
                        using (var sr = new StreamReader(fileLocation))
                        {
                            var dataArr = File.ReadAllLines(fileLocation);

                            Console.WriteLine("Beginning processing.\n");
                            length = dataArr.Length;

                            data = File.ReadAllLines(fileLocation)
                                   .Skip(1)
                                   .Select(x =>
                            {
                                counter++;
                                Console.Write("\rProcessed: {0} ({1}%)", counter, counter * 100 / length);
                                return(Person.FromCsv(x));
                            })
                                   .ToList();

                            docOpen = false;
                        }
                    }
                    catch (IOException e)
                    {
                        Console.Write(e.Message);
                        Console.WriteLine("\rCSV file is open in another application. Please close to continue.");
                        System.Threading.Thread.Sleep(2000);
                    }
                }

                counter = 0;
                Console.WriteLine("Completed processing. Starting add.");

                foreach (var d in data)
                {
                    counter++;
                    db.Persons.AddIfNotExists(d, x => x.FirstName == d.FirstName && x.LastName == d.LastName);
                    Console.Write("\rAdded: {0} ({1}%)", counter, counter * 100 / length);
                }
                Console.WriteLine("Entities added and tracked. Saving changes...");
                db.SaveChanges();
                Console.WriteLine("Completed.");
            }
        }
Example #3
0
        public static void ProcessErgastLapTimes()
        {
            string fileLocation = Seed.baseLocation + "lap_times.csv";
            var    data         = new List <LapTime>();
            var    docOpen      = true;
            var    counter      = 0;
            var    length       = 0;

            using (var db = new F1EncyclopediaContext())
            {
                while (docOpen)
                {
                    try
                    {
                        using (var sr = new StreamReader(fileLocation))
                        {
                            var fileArr = File.ReadAllLines(fileLocation);
                            length = fileArr.Length;
                            data   = fileArr.Skip(1)
                                     .Select(x =>
                            {
                                Console.Write("\rProcessed: {0}", counter, counter * 100 / length);
                                return(LapTime.FromCsv(x, db));
                            })
                                     .ToList();
                        }
                        docOpen = false;
                    }
                    catch (IOException e)
                    {
                        docOpen = true;
                        Console.WriteLine("CSV file is open in another application. Please close to continue.");
                        System.Threading.Thread.Sleep(2000);
                    }
                }

                counter = 0;
                Console.WriteLine("Completed processing data. Starting add.\n");

                foreach (var lt in data)
                {
                    db.LapTimes.AddIfNotExists(lt, x =>
                                               x.RaceWeekendId == lt.RaceWeekendId &&
                                               x.DriverId == lt.DriverId &&
                                               x.Lap == lt.Lap);
                    Console.Write("\rAdded: {0}", counter += 1);
                }
                Console.WriteLine("Entities added and tracked, saving changes...");
                db.SaveChanges();
                Console.WriteLine("Completed.");
            }
        }
Example #4
0
        public static void ProcessErgastRaceResults()
        {
            string fileLocation = Seed.baseLocation + "results.csv";
            var    data         = new List <RaceResult>();

            using (var db = new F1EncyclopediaContext())
            {
                using (var sr = new StreamReader(fileLocation))
                {
                    data = File.ReadAllLines(fileLocation)
                           .Skip(1)
                           .Select(x => RaceResult.FromCsv(x, db))
                           .ToList();
                }

                foreach (var rr in data)
                {
                    db.RaceResults.AddIfNotExists(rr,
                                                  x => x.RaceWeekendId == rr.RaceWeekendId && x.DriverId == rr.DriverId);
                }
                db.SaveChanges();
            }
        }
Example #5
0
        public static void ProcessErgastRaceStatus()
        {
            string fileLocation = Seed.baseLocation + "status.csv";
            var    data         = new List <RaceStatus>();

            // Checks file is currently open.
            using (var sr = new StreamReader(fileLocation))
            {
                data = File.ReadAllLines(fileLocation)
                       .Skip(1)
                       .Select(x => RaceStatus.FromCsv(x))
                       .ToList();
            }

            using (var db = new F1EncyclopediaContext())
            {
                foreach (var rs in data)
                {
                    db.RaceStatuses.AddIfNotExists(rs, x => x.Status == rs.Status);
                }
                db.SaveChanges();
            }
        }
Example #6
0
        public static void ProcessErgastConstructors()
        {
            string fileLocation = Seed.baseLocation + "constructors.csv";
            var    data         = new List <Constructor>();

            // Checks file is currently open.
            using (var sr = new StreamReader(fileLocation))
            {
                data = File.ReadAllLines(fileLocation)
                       .Skip(1)
                       .Select(x => Constructor.FromCsv(x))
                       .ToList();
            }

            using (var db = new F1EncyclopediaContext())
            {
                foreach (var c in data)
                {
                    db.Constructors.AddIfNotExists(c, x => x.Name == c.Name);
                }
                db.SaveChanges();
            }
        }
Example #7
0
        public static void ProcessErgastQualifying()
        {
            string fileLocation = Seed.baseLocation + "qualifying.csv";
            var    data         = new List <Qualifying>();

            // Checks file is currently open.
            using (var sr = new StreamReader(fileLocation))
            {
                data = File.ReadAllLines(fileLocation)
                       .Skip(1)
                       .Select(x => Qualifying.FromCsv(x))
                       .ToList();
            }

            using (var db = new F1EncyclopediaContext())
            {
                foreach (var q in data)
                {
                    db.Qualifyings.AddIfNotExists(q, x => x.DriverId == q.DriverId && x.RaceWeekendId == q.RaceWeekendId);
                }
                db.SaveChanges();
            }
        }
Example #8
0
        public static void ProcessErgastRaceWeekends()
        {
            string fileLocation = Seed.baseLocation + "races.csv";
            var    data         = new List <RaceWeekend>();

            // Checks file is currently open.
            using (var sr = new StreamReader(fileLocation))
            {
                data = File.ReadAllLines(fileLocation)
                       .Skip(1)
                       .Select(x => RaceWeekend.FromCsv(x))
                       .ToList();
            }

            using (var db = new F1EncyclopediaContext())
            {
                foreach (var rw in data)
                {
                    db.RaceWeekends.AddIfNotExists(rw, x => x.Name == rw.Name && x.Round == rw.Round && x.Year == rw.Year);
                }
                db.SaveChanges();
            }
        }