private void btnFlightLeg_Click_1(object sender, RoutedEventArgs e)
        {
            var selectedRoute = cmbRoute.SelectedItem as FlightRoute;

            var selectedPlane = cmbFlight.SelectedItem as Airplane;

            var flightDate = dateFlightDate.SelectedDate.Value;

            var departureTime = dteDepartureTime.Text.Split(':');

            var arrivalTime = dteArrivalTime.Text.Split(':');

            FlightLeg flightLeg = new FlightLeg()
                {
                     FlightNumber = Convert.ToInt32(txtFlightNumber.Text),
                     TravelDate = flightDate,
                     SelectedRoute = selectedRoute,
                     SelectedPlane = selectedPlane
                };

               flightLeg.ScheduledDepartureTime = DateTime.Now.Date.Add(new TimeSpan(Convert.ToInt32(departureTime[0]),Convert.ToInt32(departureTime[1]),0)); //flightDate.AddHours(10), //Convert.ToDateTime(arrivalTime),
               flightLeg.ScheduledArrivalTime = DateTime.Now.Date.Add(new TimeSpan(Convert.ToInt32(arrivalTime[0]), Convert.ToInt32(arrivalTime[1]), 0)); //flightDate.AddHours(10), //Convert.ToDateTime(arrivalTime),//flightDate.AddHours(12),// Convert.ToDateTime(departureTime),

            new FlightManager().AddFlightLeg(flightLeg);

            var flightlegs = new FlightManager().GetFlightLegs();
            dgFlightRoutes.ItemsSource = flightlegs;

            cmbRoute.SelectedItem = null;
            txtFlightNumber.Text = "";
        }
 public List<Customer> GetFlightPassengers(FlightLeg flightLeg)
 {
     return new AirlineRepository().GetFlightPassengers(flightLeg);
 }
 public void AddFlightLeg(FlightLeg flightLeg)
 {
     new AirlineRepository().AddFlightLeg(flightLeg);
 }
        public List<Customer> GetFlightPassengers(FlightLeg flightLeg)
        {
            List<Customer> customers = new List<Customer>();
            using (var context = new Model1Container())
            {
                List<Passenger> passengers = new List<Passenger>();
                List<Ticket> tickets = new List<Ticket>();
                var flightSeats = from  p in context.Fligt_Leg_Seat_Assignment  where
                    ( p.Flight_Leg_Id == flightLeg.Id && !p.IsAvailable) select p.Id;

                foreach (var seatId in flightSeats)
                {
                    var ticket = context.Tickets.Where(p => p.SeatId == seatId).SingleOrDefault();
                    if (ticket != null)
                    {
                        tickets.Add(ticket);
                    }
                }

                foreach (var ticket in tickets)
                {
                    var passenger = context.Passengers.Where(p => p.Id == ticket.PassengerId).SingleOrDefault();
                    if (passenger != null)
                    {
                        passengers.Add(passenger);
                    }
                }

                foreach (var passenger in passengers)
                {
                    customers.Add(new Customer
                    { FirstName = passenger.FirstName,
                        LastName = passenger.LastName,
                        Id = passenger.Id,
                         Email = passenger.email,
                          Phone = passenger.phone,
                          Address = passenger.address,
                          DateOfBirth = passenger.DateofBirth
                    });

                }
            }
            return customers;
        }
        public List<FlightLeg> GetFlightLegs(int flightRouteId)
        {
            List<FlightLeg> flightLegs = null;
            using (var context = new Model1Container())
            {
                var flight_legs = context.Flight_Leg.Where(p => p.Flight_Route_Id == flightRouteId);

                if (flight_legs != null)
                {
                    flightLegs = new List<FlightLeg>();
                    foreach (var leg in flight_legs)
                    {
                        var flightLeg = new FlightLeg()
                        {
                            FlightNumber = leg.FlightNumber,
                            TravelDate = leg.Date,
                            ScheduledArrivalTime = leg.ScheduledArrivalTime,
                            ScheduledDepartureTime = leg.ScheduledDepartureTime,
                            Id = leg.Id,
                            Fare = leg.Fare,
                            AvailableSeats = leg.AvailableSeats
                        };

                        flightLegs.Add(flightLeg);
                    }
                }
            }
            return flightLegs;
        }
        public List<FlightLeg> GetFlightLegs()
        {
            List<FlightLeg> flightlegs = new List<FlightLeg>();
            using (var dataContext = new Model1Container())
            {
                var legs = from p in dataContext.Flight_Leg select p;

                foreach (var leg in legs)
                {
                    FlightLeg flightLeg = new FlightLeg
                    {
                        FlightNumber = leg.FlightNumber,
                        TravelDate = leg.Date,
                        ScheduledArrivalTime = leg.ScheduledArrivalTime,
                        ScheduledDepartureTime = leg.ScheduledDepartureTime,
                        Id = leg.Id,
                        Fare = leg.Fare,
                        AvailableSeats = leg.AvailableSeats,
                        FlightRouteId = leg.Flight_Route_Id
                    };
                    flightlegs.Add(flightLeg);
                }
            }
            return flightlegs;
        }
        public void AddFlightLeg(FlightLeg flightLeg)
        {
            var newFlightLeg = new Flight_Leg();

            using (var dataContext = new Model1Container())
            {

                newFlightLeg.AvailableSeats = flightLeg.SelectedPlane.NoOfSeats;
                newFlightLeg.Date = flightLeg.TravelDate;
                newFlightLeg.Fare = 300;
                newFlightLeg.IsCancelled = false;
                newFlightLeg.IsRescheduled = false;
                newFlightLeg.Flight_Route_Id = flightLeg.SelectedRoute.Id;
                newFlightLeg.FlightNumber = (short)flightLeg.FlightNumber;
                newFlightLeg.ScheduledDepartureTime = flightLeg.ScheduledDepartureTime;
                newFlightLeg.ScheduledArrivalTime = flightLeg.ScheduledArrivalTime;

                dataContext.Flight_Leg.AddObject(newFlightLeg);
                //dataContext.AddToFlight_Leg(newFlightLeg);
                dataContext.SaveChanges();

                for (int row = 0; row < flightLeg.SelectedPlane.TotalRows; row++)
                {

                    for (int col = 0; col < flightLeg.SelectedPlane.TotalColumns; col++)
                    {
                        string seatNumber = "";
                        if (col == 0)
                        {
                            seatNumber = string.Format("{0}{1}", row+1, "A");
                        }
                        if (col == 1)
                        {
                            seatNumber = string.Format("{0}{1}", row + 1, "B");
                        }
                        if (col == 2)
                        {
                            seatNumber = string.Format("{0}{1}", row + 1, "C");
                        }
                        if (col == 3)
                        {
                            seatNumber = string.Format("{0}{1}", row + 1, "D");
                        }
                        if (col == 4)
                        {
                            seatNumber = string.Format("{0}{1}", row + 1, "E");
                        }
                        if (col == 5)
                        {
                            seatNumber = string.Format("{0}{1}", row + 1, "F");
                        }

                        dataContext.AddToFligt_Leg_Seat_Assignment(new Fligt_Leg_Seat_Assignment { SeatingClass = "Economy", Flight_Leg_FlightNumber=newFlightLeg.FlightNumber, SeatNumber = seatNumber, Flight_Leg_Id = newFlightLeg.Id, IsAvailable = true, SeatType = "Normal" });

                        //dataContext.AddToFligt_Leg_Seat_Assignment(new Fligt_Leg_Seat_Assignment { SeatingClass = "Economy", Flight_Leg_Id = newFlightLeg.Id,  SeatNumber = seatNumber, IsAvailable = true, SeatType = "Normal" });
                    }

                }
                dataContext.SaveChanges();
            }
        }