public static Constructor FromCsv(string line) { var values = line.Split(','); using (var db = new F1EncyclopediaContext()) { values[2] = Seed.UpdateNationalities(values[2]); var country = db.Countries.FirstOrDefault(x => x.Nationality == values[2]); if (country == null) { Console.WriteLine($"Country not found with nationality: {values[2]}"); } var constructor = new Constructor() { Name = values[1], CountryId = country != null ? country.Id : 1, WikiUrl = values[3] }; return(constructor); } }
public static RaceResult FromCsv(string line, F1EncyclopediaContext db) { var values = line.Split(','); var raceWeekendId = RaceWeekend.RaceWeekendIdCorrection(values[1]); var driverId = Person.DriverIdCorrection(values[2]); var constructorId = Constructor.ConstructorIdCorrection(values[3]); var statusId = RaceStatus.StatusIdCorrection(values[17]); var raceWeekend = db.RaceWeekends.FirstOrDefault(x => x.Id == raceWeekendId); var driver = db.Persons.FirstOrDefault(x => x.Id == driverId); var constructor = db.Constructors.FirstOrDefault(x => x.Id == constructorId); var status = db.RaceStatuses.FirstOrDefault(x => x.Id == statusId); LogUnmatchedProperties(raceWeekend, raceWeekendId); LogUnmatchedProperties(driver, driverId); LogUnmatchedProperties(constructor, constructorId); LogUnmatchedProperties(status, statusId); var raceResult = new RaceResult() { GridPosition = Convert.ToInt32(values[5]), Position = Convert.ToInt32(values[8]), Points = Convert.ToInt32(values[9]), Laps = Convert.ToInt32(values[10]), Time = GetTimeSpan(values[12]), RaceWeekendId = raceWeekend != null ? raceWeekend.Id : 1, DriverId = driver != null ? driver.Id : 1, ConstructorId = constructor != null ? constructor.Id : 1, StatusId = status != null ? status.Id : 1 }; return(raceResult); }
public static Track FromCsv(string line) { var values = line.Split(','); using (var db = new F1EncyclopediaContext()) { if (values[3] == "UK") { values[3] = "United Kingdom"; } var country = db.Countries.FirstOrDefault(x => x.Name == values[3]); if (country == null) { Console.WriteLine($"Could not find country with name: {values[3]}"); } var track = new Track() { Name = values[1], City = values[2], CountryId = country != null ? country.Id : 1, Lat = Convert.ToSingle(values[4]), Long = Convert.ToSingle(values[5]), Alt = Convert.ToInt16(values[6]), WikiUrl = values[7] }; return(track); } }
public static Person FromCsv(string line) { var values = line.Split(','); using (var db = new F1EncyclopediaContext()) { values[4] = Seed.UpdateNationalities(values[4]); var country = db.Countries .Where(p => p.Nationality == values[4]) .FirstOrDefault(); if (country == null) { Console.WriteLine($"Country not found from nationality: {values[4]}"); } var person = new Person() { FirstName = values[1], LastName = values[2], DoB = DateTime.Parse(values[3]), CountryId = country != null ? country.Id : 1, WikiUrl = values[5] }; return(person); } }
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(); } }
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."); } }
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."); } }
public static RaceWeekend FromCsv(string line) { var values = line.Split(','); var dateParts = values[5].Replace("\"", "").Split('-'); var timeParts = values[6].Replace("\"", "").Split(':'); using (var db = new F1EncyclopediaContext()) { var correctedTrackId = Track.TrackIdCorrection(Convert.ToInt16(values[3])); var track = db.Tracks.FirstOrDefault(x => x.Id == correctedTrackId); if (track == null) { Console.WriteLine($"Could not find Track with Id: {values[3]}"); } var raceWeekend = new RaceWeekend { Year = Convert.ToInt16(values[1]), Round = Convert.ToInt16(values[2]), TrackId = track != null ? track.Id : 1, Name = values[4].Replace("\"", ""), Date = timeParts.Length == 3 ? new DateTime( Convert.ToInt16(dateParts[0]), Convert.ToInt16(dateParts[1]), Convert.ToInt16(dateParts[2]), Convert.ToInt16(timeParts[0]), Convert.ToInt16(timeParts[1]), Convert.ToInt16(timeParts[2]) ) : new DateTime( Convert.ToInt16(dateParts[0]), Convert.ToInt16(dateParts[1]), Convert.ToInt16(dateParts[2]) ), RaceWiki = values[7].Replace("\"", "") }; return(raceWeekend); } }
public static LapTime FromCsv(string line, F1EncyclopediaContext context) { var values = line.Split(','); var raceWeekendId = RaceWeekend.RaceWeekendIdCorrection(values[0]); var driverId = Person.DriverIdCorrection(values[1]); var raceWeekend = context.RaceWeekends.FirstOrDefault(x => x.Id == raceWeekendId); var driver = context.Persons.FirstOrDefault(x => x.Id == driverId); LogUnmatchedProperties(driver, driverId); LogUnmatchedProperties(raceWeekend, raceWeekendId); return(new LapTime() { RaceWeekendId = raceWeekend != null ? raceWeekend.Id : 1, DriverId = driver != null ? driver.Id : 1, Lap = Convert.ToInt32(values[2]), Position = Convert.ToInt32(values[3]), Time = GetTimeSpan(values[5]) }); }
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(); } }
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(); } }
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(); } }
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(); } }
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(); } }
public static Qualifying FromCsv(string line) { var values = line.Split(','); using (var db = new F1EncyclopediaContext()) { var correctedRaceWeekendId = RaceWeekend.RaceWeekendIdCorrection(values[1]); var correctedDriverId = Person.DriverIdCorrection(values[2]); var correctedConstructorId = Constructor.ConstructorIdCorrection(values[3]); var raceWeekend = db.RaceWeekends.FirstOrDefault(x => x.Id == Convert.ToInt32(values[1])); var driver = db.Persons.FirstOrDefault(x => x.Id == Convert.ToInt32(values[2])); var constructor = db.Constructors.FirstOrDefault(x => x.Id == Convert.ToInt32(values[3])); LogUnmatchedProperties(raceWeekend, correctedRaceWeekendId); LogUnmatchedProperties(driver, correctedDriverId); LogUnmatchedProperties(constructor, correctedConstructorId); var quali = new Qualifying() { RaceWeekendId = raceWeekend != null ? raceWeekend.Id : 1, DriverId = driver != null ? driver.Id : 1, ConstructorId = constructor != null ? constructor.Id : 1, Position = Convert.ToInt32(values[4]), Q1 = GetTimeSpan(values[5]), Q2 = GetTimeSpan(values[6]), Q3 = GetTimeSpan(values[7]) }; return(quali); } }
static void Main(string[] args) { using (var context = new F1EncyclopediaContext()) { context.CleanTable("RaceResults"); context.CleanTable("LapTimes"); context.CleanTable("RaceStatuses"); context.CleanTable("Qualifyings"); context.CleanTable("Constructors"); context.CleanTable("RaceWeekends"); context.CleanTable("Persons"); context.CleanTable("Tracks"); context.CleanTable("Countries"); } /* Order of seeding: * Countries * Tracks * Drivers * RaceWeekends * Constructors * Qualifying * RaceStatuses * LapTime * RaceResults */ ProcessErgastCountries(); ProcessErgastTracks(); ProcessErgastDrivers(); ProcessErgastRaceWeekends(); ProcessErgastConstructors(); ProcessErgastQualifying(); ProcessErgastRaceStatus(); ProcessErgastLapTimes(); ProcessErgastRaceResults(); }
public DriverController(ILogger <DriverController> _logger, F1EncyclopediaContext _dbContext) { logger = _logger ?? throw new ArgumentNullException(nameof(_logger)); dbContext = _dbContext ?? throw new ArgumentNullException(nameof(_dbContext)); }