protected void RunImport(object parameter) { var fileDialog = new OpenFileDialog() { FileName = "Storage.txt", DefaultExt = ".txt", Filter = "Text documents (.txt)|*.txt" }; var result = fileDialog.ShowDialog(this.OwnerWindow); if (result ?? true) { var filePath = fileDialog.FileName; var fileLines = File.ReadAllLines(filePath); int counter = 1; try { var importedTrips = new List <Trip>(); var pilots = new List <Pilot>(); var airfields = new List <Airfield>(); foreach (var line in fileLines) { var loadedTrip = Trip.LoadTripFromLegacy(line, counter - 1, pilots, airfields); importedTrips.Add(loadedTrip); counter++; } using (var sqlContext = new LogbookContext()) { sqlContext.Trips.AddRange(importedTrips); sqlContext.SaveChanges(); } MessageBox.Show( $"Successfully imported {counter} trips", "Import Successful", MessageBoxButton.OK); this.NotifyPropertyChanged("LastEntryDate"); this.NotifyPropertyChanged("HoursLast28Days"); this.NotifyPropertyChanged("HoursLast365Days"); } catch (Exception e) { MessageBox.Show( $"Error on line {counter}: {e.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } }
public static Pilot GetOrCreatePilotFromDatabase(string name) { using (var sqlContext = new LogbookContext()) { var existingPilot = sqlContext.Pilots.SingleOrDefault(p => p.Name == name); return(existingPilot ?? new Pilot() { Name = name }); } }
public static Airfield GetOrCreateAirfieldFromDatabase(string name) { using (var sqlContext = new LogbookContext()) { var existingAirfield = sqlContext.Airfields.SingleOrDefault(a => a.AirfieldName == name); return(existingAirfield ?? new Airfield() { AirfieldName = name }); } }
public EditTripPageViewModel(Trip editingTrip, MainWindow ownerWindow) { this.Trip = editingTrip; this.OwnerWindow = ownerWindow; this.OwnerWindow.Width = Math.Max(this.OwnerWindow.Width, 1000); this.OwnerWindow.Height = Math.Max(this.OwnerWindow.Height, 600); if (this.Trip.Pilots.Count == 0) { this.AddNewPilot(); using (var databaseContext = new LogbookContext()) { var rankSetting = databaseContext.Settings.SingleOrDefault(s => s.SettingKey == Constants.UserRankSettingKey); if (rankSetting != null) { var userRank = (PilotPositionEnum)rankSetting.SettingValue; switch (userRank) { case PilotPositionEnum.Captain: case PilotPositionEnum.FirstOfficer: var pilot = this.Trip.Pilots.Where(p => p.PilotPosition == userRank).FirstOrDefault()?.Pilot; if (pilot != null) { pilot.Name = "Self"; } break; default: break; } } } } this.SkipPilotFocus = this.Trip.Pilots.Count; this.SetupCommands(); }
private bool AttemptSave() { if (this.NeedsSave) { this.Trip.DepartureTime = DateTime.Today.AddTicks(this.Trip.DepartureTime.TimeOfDay.Ticks); this.Trip.ArrivalTime = DateTime.Today.AddTicks(this.Trip.ArrivalTime.TimeOfDay.Ticks); // Check if the pilots already exist, and if they do get their database id foreach (var pilotLinker in this.Trip.Pilots) { pilotLinker.Pilot = Pilot.GetOrCreatePilotFromDatabase(pilotLinker.Pilot.Name); } // Do the same for the airfields foreach (var airfieldLinker in this.Trip.Airfields) { airfieldLinker.Airfield = Airfield.GetOrCreateAirfieldFromDatabase(airfieldLinker.Airfield.AirfieldName); } using (var databaseContext = new LogbookContext()) { databaseContext.Trips.Add(this.Trip); databaseContext.SaveChanges(); } this.NeedsSave = false; MessageBox.Show( this.OwnerWindow, "Trip Saved Successfully", "Trip Saved", MessageBoxButton.OK); return(true); } return(true); }