Ejemplo n.º 1
0
        public bool DeleteCity(City city)
        {
            try
            {
                if (!this.context.CitySet.Contains(city))
                {
                    throw new ArgumentException(string.Format("The database does not contain {0} city", city));
                }

                this.context.CitySet.Remove(city);
                this.context.SaveChanges();
                return true;
            }
            catch (SqlException e)
            {
                Console.WriteLine(e.Message);
                return false;
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
                return false;
            }
            catch (Exception 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;
 }
Ejemplo n.º 4
0
 static void Main ( string[] args )
 {
     using(var db = new TicketSystemModel())
     {
         //db.Database.Create();
         Console.WriteLine("Enter a name of a city:");
         string name = Console.ReadLine();
         City city = new City() { CityID = 1 , Name = name };
         db.Cities.Add(city);
         db.SaveChanges();
     }
 }
Ejemplo n.º 5
0
 public bool AddCity(City city)
 {
     try
     {
         this.context.CitySet.Add(city);
         this.context.SaveChanges();
         return true;
     }
     catch (SqlException e)
     {
         Console.WriteLine(e.Message);
         return false;
     }
 }
 private void cityDataGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
 {
     if (!e.Row.IsEditing)
     {
         var city = e.Row.Item as City;
         if (city != null)
         {
             beginEditCity = new City();
             beginEditCity.CityID = city.CityID;
             beginEditCity.Name = city.Name;
         }
     }
 }
        private void startCityButton_Click(object sender, RoutedEventArgs e)
        {
            var selectedItem = cityDataGrid.SelectedItem;
            if (selectedItem != null)
            {
                var selectedCity = selectedItem as City;
                if (selectedCity != null)
                {
                    int endCityId = endCityForSchedule?.CityID ?? -1;
                    if (selectedCity.CityID != endCityId)
                    {
                        startCityForSchedule = selectedCity;
                        chosenStartCityLabel.Content = $"Start city: {selectedCity.CityID} - {selectedCity.Name}";
                        startCityInScheduleLabel.Content = $"{selectedCity.CityID} - {selectedCity.Name}";
                        return;
                    }
                    else
                    {
                        MessageBox.Show("Start and end city cannot be the same!");
                        return;
                    }
                }
            }

            MessageBox.Show("Plese select a city from the grid!");
        }
        private void cityDataGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
        {
            var city = e.Row.Item as City;
            if (city != null)
            {
                if (city.CityID != 0)
                {
                    try
                    {
                        if (city.Name.Length < 1)
                        {
                            MessageBox.Show("City should have a name! Nothing saved to database!");
                            city.Name = beginEditCity.Name;
                            e.Row.Item = null;
                            e.Row.Item = city;
                        }
                        else
                            dbContext.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show($"Could not update database! Error message: {ex.Message}");
                    }
                }
                else
                {
                    try
                    {
                        if (city.Name.Length < 1)
                        {
                            MessageBox.Show("City should have a name! Nothing saved to database!");
                            var currentItemsSource = trainDataGrid.ItemsSource;
                            trainDataGrid.ItemsSource = null;
                            trainDataGrid.Items.Remove(e.Row);
                            trainDataGrid.ItemsSource = currentItemsSource;
                        }
                        else
                        {
                            dbContext.Cities.Add(city);
                            dbContext.SaveChanges();
                            e.Row.Item = null;
                            e.Row.Item = city;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show($"Could not update database! Error message: {ex.Message}");
                    }
                }
            }

            beginEditCity = null;
        }
        private void cityDataGrid_PreviewCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            var cityToDelete = cityDataGrid.SelectedItem as City;

            if (e.Command == DataGrid.DeleteCommand)
            {
                if (MessageBox.Show(string.Format("Would you like to delete {0}?",
                                                  cityToDelete.Name),
                                                  "Confirm Delete",
                                                  MessageBoxButton.YesNo,
                                                  MessageBoxImage.Question,
                                                  MessageBoxResult.No) != MessageBoxResult.Yes)
                {
                    e.Handled = true;
                }
                else
                {
                    try
                    {
                        var schedulesToDelete = from schedule in dbContext.Schedules
                                                where schedule.StartCityID == cityToDelete.CityID
                                                || schedule.EndCityID == cityToDelete.CityID
                                                select schedule;

                        foreach (var schedule in schedulesToDelete)
                        {

                            var ticketsToDelete = from ticket in dbContext.Tickets
                                                  where ticket.ScheduleID == schedule.ScheduleID
                                                  select ticket;

                            dbContext.Tickets.RemoveRange(ticketsToDelete);

                            var seatsToDelete = from seat in schedule.Train.Seats
                                                where seat.ScheduleID == schedule.ScheduleID
                                                select seat;

                            dbContext.Seats.RemoveRange(seatsToDelete);

                            dbContext.Schedules.Remove(schedule);
                        }

                        dbContext.Cities.Remove(cityToDelete);

                        dbContext.SaveChanges();

                        if(cityToDelete.CityID == (startCityForSchedule?.CityID ?? -1))
                        {
                            startCityForSchedule = null;
                            chosenStartCityLabel.Content = "Start city: None";
                            startCityInScheduleLabel.Content = "None";
                        }
                        else if (cityToDelete.CityID == (endCityForSchedule?.CityID ?? -1))
                        {
                            endCityForSchedule = null;
                            chosenEndCityLabel.Content = "End city: None";
                            endCityInScheduleLabel.Content = "None";
                        }

                        UpdateDataGrids();
                    }
                    catch (Exception ex)
                    {
                        e.Handled = true;
                        MessageBox.Show($"Could not delete item in database! Error message: {ex.Message}");
                    }
                }
            }
        }
Ejemplo n.º 10
0
 // TODO : edit a trip
 public IEnumerable<Schedule> AllTripsFrom(City a, City b)
 {
     return this.context.ScheduleSet.Where(x => x.StartCity.ToString().Equals(a.ToString()) && x.EndCity.ToString().Equals(b.ToString()));
 }
 public ComboBoxCustomItem(City city)
 {
     CustomItem = city;
     Content = city.Name;
 }
Ejemplo n.º 12
0
        private static void CreateCity()
        {
            using (var db = new TicketDB())
            {
                Console.WriteLine("Creating city!");
                Console.Write("Name: ");
                var name = Console.ReadLine().Trim();

                try
                {
                    City city = new City
                    {
                        Name = name
                    };

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

                Console.WriteLine("City created successfully!");
            }
        }