public bool DeleteTrain(Train train)
        {
            try
            {
                // TODO : throwing and catching exception?
                if (!this.context.TrainSet.Contains(train))
                {
                    throw new ArgumentException("Databse does not containt specified train");
                }

                this.context.TrainSet.Remove(train);
                this.context.SaveChanges();
                return true;
            }
            catch (SqlException e)
            {
                Console.WriteLine(e.Message);
                return false;
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
                return false;
            }
        }
        public ScheduleAddWindow(TicketDB dbContext, City startCity, City endCity, Train train)
        {
            InitializeComponent();

            this.dbContext = dbContext;
            this.startCity = startCity;
            this.endCity = endCity;
            this.train = train;
        }
 public AdminWindow(TicketDB dbContext, User user)
 {
     InitializeComponent();
     this.dbContext = dbContext;
     this.user = user;
     startCityForSchedule = null;
     endCityForSchedule = null;
     trainForSchedule = null;
     beginEditCity = null;
     beginEditTrain = null;
     beginEditSchedule = null;
 }
 public bool Addtrain(Train train)
 {
     try
     {
         this.context.TrainSet.Add(train);
         this.context.SaveChanges();
         return true;
     }
     catch (SqlException e)
     {
         Console.WriteLine(e.Message);
         return false;
     }
 }
        public bool EdinTrain(Train train, int seats)
        {
            try
            {
                if (!this.context.TrainSet.Contains(train))
                {
                    Console.WriteLine("Train not in database");
                    return false;
                }
                if (this.context.TrainSet.FirstOrDefault(x => x.Id == train.Id).Seats != seats)
                {
                    this.context.TrainSet.FirstOrDefault(x => x.Id == train.Id).Seats = seats;
                    this.context.SaveChanges();
                    return true;
                }

                return false;
            }
            catch (SqlException e)
            {
                Console.WriteLine(e.Message);
                return false;
            }
        }
        private void trainDataGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
        {
            var train = e.Row.Item as Train;
            if (train != null)
            {
                if (train.TrainID != 0)
                {
                    try
                    {
                        if (train.NumberOfSeats <= 0)
                        {
                            MessageBox.Show("Seats number must be possitive! Nothing saved to database!");
                            train.NumberOfSeats = beginEditTrain.NumberOfSeats;
                            train.BriefDescription = beginEditTrain.BriefDescription;
                            e.Row.Item = null;
                            e.Row.Item = train;
                        }
                        else if (train.BriefDescription.Length < 1)
                        {
                            MessageBox.Show("Train must have a description! Nothing saved to database!");
                            train.NumberOfSeats = beginEditTrain.NumberOfSeats;
                            train.BriefDescription = beginEditTrain.BriefDescription;
                            e.Row.Item = null;
                            e.Row.Item = train;
                        }
                        else
                        {
                            if (train.NumberOfSeats < beginEditTrain.NumberOfSeats)
                            {
                                var seatsToRemove = from s in dbContext.Seats
                                                    where s.TrainID == train.TrainID
                                                          && s.SeatNumber > train.NumberOfSeats
                                                    select s;

                                var ticketsToRemove = from t in dbContext.Tickets
                                                      where t.TrainID == train.TrainID
                                                            && t.SeatNumber > train.NumberOfSeats
                                                      select t;

                                dbContext.Tickets.RemoveRange(ticketsToRemove);
                                dbContext.Seats.RemoveRange(seatsToRemove);
                            }
                            else if (train.NumberOfSeats > beginEditTrain.NumberOfSeats)
                            {
                                var schedulesForTrain = from s in dbContext.Schedules
                                                         where s.TrainID == train.TrainID
                                                         select s;

                                foreach (var schedule in schedulesForTrain)
                                {
                                    for (int i = beginEditTrain.NumberOfSeats; i < train.NumberOfSeats; i++)
                                    {
                                        dbContext.Seats.Add(new Seat()
                                        {
                                            SeatNumber = i + 1,
                                            TrainID = train.TrainID,
                                            ScheduleID = schedule.ScheduleID,
                                            Taken = false,
                                            Class = SeatClass.FirstClass
                                        });
                                    }
                                }
                            }
                            dbContext.SaveChanges();
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show($"Could not update database! Error message: {ex.Message}");
                    }
                }
                else
                {
                    try
                    {
                        if (train.NumberOfSeats <= 0)
                        {
                            MessageBox.Show("Seats number must be possitive! Nothing saved to database!");
                            var currentItemsSource = trainDataGrid.ItemsSource;
                            trainDataGrid.ItemsSource = null;
                            trainDataGrid.Items.Remove(e.Row);
                            trainDataGrid.ItemsSource = currentItemsSource;
                        }
                        else if (train.BriefDescription.Length < 1)
                        {
                            MessageBox.Show("Train must have a description! Nothing saved to database!");
                            var currentItemsSource = trainDataGrid.ItemsSource;
                            trainDataGrid.ItemsSource = null;
                            trainDataGrid.Items.Remove(e.Row);
                            trainDataGrid.ItemsSource = currentItemsSource;
                        }
                        else
                        {
                            dbContext.Trains.Add(train);
                            dbContext.SaveChanges();
                            e.Row.Item = null;
                            e.Row.Item = train;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show($"Could not update database! Error message: {ex.Message}");
                    }
                }
            }

            beginEditTrain = null;
        }
        private void trainDataGrid_PreviewCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            var trainToDelete = trainDataGrid.SelectedItem as Train;

            if (e.Command == DataGrid.DeleteCommand)
            {
                if (MessageBox.Show(string.Format("Would you like to delete train {0}?",
                                                  trainToDelete.TrainID),
                                                  "Confirm Delete",
                                                  MessageBoxButton.YesNo,
                                                  MessageBoxImage.Question,
                                                  MessageBoxResult.No) != MessageBoxResult.Yes)
                {
                    e.Handled = true;
                }
                else
                {
                    try
                    {
                        dbContext.Seats.RemoveRange(trainToDelete.Seats);

                        var ticketsToDelete = from ticket in dbContext.Tickets
                                              where ticket.TrainID == trainToDelete.TrainID
                                              select ticket;

                        dbContext.Tickets.RemoveRange(ticketsToDelete);

                        var schedulesToDelete = from schedule in dbContext.Schedules
                                                where schedule.TrainID == trainToDelete.TrainID
                                                select schedule;

                        dbContext.Schedules.RemoveRange(schedulesToDelete);

                        dbContext.Trains.Remove(trainToDelete);

                        dbContext.SaveChanges();

                        if (trainToDelete.TrainID == (trainForSchedule?.TrainID ?? -1))
                        {
                            trainForSchedule = null;
                            chosenTrainLabel.Content = "Train: None";
                            trainInScheduleTextBlock.Text = "None";
                        }

                        UpdateDataGrids();
                    }
                    catch (Exception ex)
                    {
                        e.Handled = true;
                        MessageBox.Show($"Could not delete item in database! Error message: {ex.Message}");
                    }
                }
            }
        }
 private void trainDataGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
 {
     if (!e.Row.IsEditing)
     {
         var train = e.Row.Item as Train;
         if (train != null)
         {
             beginEditTrain = new Train();
             beginEditTrain.TrainID = train.TrainID;
             beginEditTrain.NumberOfSeats = train.NumberOfSeats;
             beginEditTrain.BriefDescription = train.BriefDescription;
         }
     }
 }
        private void trainButton_Click(object sender, RoutedEventArgs e)
        {
            var selectedItem = trainDataGrid.SelectedItem;
            if (selectedItem != null)
            {
                var selectedTrain = selectedItem as Train;
                if (selectedTrain != null)
                {
                    trainForSchedule = selectedTrain;
                    chosenTrainLabel.Content = $"Train: {selectedTrain.TrainID} - {selectedTrain.BriefDescription}";
                    trainInScheduleTextBlock.Text = $"{selectedTrain.TrainID} - {selectedTrain.BriefDescription}";
                    return;
                }
            }

            MessageBox.Show("Plese select a train from the grid!");
        }
示例#10
0
        private static void CreateTrain()
        {
            using (var db = new TicketDB())
            {
                Console.WriteLine("Creating train!");
                Console.Write("Brief Description: ");
                var briefDescription = Console.ReadLine().Trim();
                Console.Write("Number Of Seats: ");
                var numberOfSeatsArg = Console.ReadLine().Trim();

                int numberOfSeats;
                try
                {
                    numberOfSeats = int.Parse(numberOfSeatsArg);
                }
                catch (FormatException)
                {
                    Console.WriteLine("Invalid number format! Operation aborted!");
                    return;
                }

                try
                {
                    Train train = new Train
                    {
                        BriefDescription = briefDescription,
                        NumberOfSeats = numberOfSeats
                    };

                    db.Trains.Add(train);
                    db.SaveChanges();
                }
                catch (OptimisticConcurrencyException ex)
                {
                    Console.WriteLine("Cannot insert train in database! Operation exited with message:");
                    Console.WriteLine(ex.Message);
                    return;
                }

                Console.WriteLine("Train created successfully!");
            }
        }
示例#11
0
        public bool EditTrain(Train train, int id)
        {
            try
            {
                if (!this.context.TrainSet.Contains(train))
                {
                    Console.WriteLine("Train not in database");
                    return false;
                }

                // TODO : check if train id already exists if sql exception does not cover that
                this.context.TrainSet.FirstOrDefault(x => x.Id == train.Id).Id = id;
                this.context.SaveChanges();
                return true;
            }
            catch (SqlException e)
            {
                Console.WriteLine(e.Message);
                return false;
            }
        }