예제 #1
0
        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);
                }
            }
        }
        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);
        }