public List<Flight> SearchFlights(DateTime departure) { var client = new HttpClient(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); baseurl += "?date=" + departure.Date.ToString("yyyy-MM-dd"); var response = client.GetAsync(baseurl).Result; var resultString = response.Content.ReadAsStringAsync().Result; List<Flight> flights = new List<Flight>(); JObject schipholflights = JObject.Parse(resultString); // get JSON result objects into a list IList<JToken> results = schipholflights["Flights"]["Flight"].Children().ToList(); foreach (JToken result in results) { SchipholFlight schipholflight = JsonConvert.DeserializeObject<SchipholFlight>(result.ToString()); if (schipholflight.ScheduleDate != DateTime.MinValue){ Flight flight = new Flight(); flight.ArrDep = schipholflight.ArrDep; if (schipholflight.ArrDep == "D") { flight.Departure = schipholflight.ScheduleDate + TimeSpan.Parse(schipholflight.ScheduleTime.ToLongTimeString()); }else if (schipholflight.ArrDep == "A"){ flight.Arrival = schipholflight.ScheduleDate + TimeSpan.Parse(schipholflight.ScheduleTime.ToLongTimeString()); } flight.FlightNumber = schipholflight.FlightNumber; flights.Add(flight); } } return flights; }
private Movement SaveNewTrip(NewTrip newTrip) { var movement = new Movement(); var db = new AirportCarpoolDbContext(); db.Entry(movement).State = EntityState.Added; var user = GetUserByUserName(db, WebSecurity.CurrentUserName); db.Entry(user).State = EntityState.Unchanged; movement.User = user; Location locationFrom = null; if (newTrip.LocationFrom == "Home" && user.Location != null) { locationFrom = Location.GetCopy(user.Location); } else if (newTrip.LocationFrom == "Schiphol") { locationFrom = Location.GetNewSchipholLocation(); } if (locationFrom != null) { db.Entry(locationFrom).State = EntityState.Added; } movement.LocationFrom = locationFrom; Location locationTo = null; if (newTrip.LocationTo == "Home" && user.Location != null) { locationTo = Location.GetCopy(user.Location); } else if (newTrip.LocationTo == "Schiphol") { locationTo = Location.GetNewSchipholLocation(); } if (locationTo != null) { db.Entry(locationTo).State = EntityState.Added; } movement.LocationTo = locationTo; movement.Driver = newTrip.Driver; movement.Passenger = newTrip.Passenger; movement.Seats = newTrip.Seats; movement.Luggage = newTrip.Luggage; if (newTrip.Driver) { movement.MaxSeats = newTrip.MaxSeats; movement.MaxLuggage = newTrip.MaxLuggage; movement.MaxKm = newTrip.MaxKm; } movement.MovementDateTime = newTrip.MovementDate.Add(newTrip.MovementTime.TimeOfDay); movement.MovementDateType = newTrip.MovementDateType; if (movement.Driver) { var carpool = new Carpool(); carpool.Status = CarpoolStatus.New; carpool.MaxKm = newTrip.MaxKm; carpool.MaxSeats = newTrip.MaxSeats; carpool.MaxLuggage = newTrip.MaxLuggage; carpool.Arrival = movement.MovementDateTime; db.Entry(carpool).State = EntityState.Added; carpool.Movements = new List<Movement>(); carpool.Movements.Add(movement); } db.SaveChanges(); db.Dispose(); if (newTrip.FlightNumber != "") { db = new AirportCarpoolDbContext(); Flight flight = new Flight { FlightNumber = newTrip.FlightNumber, ArrDep = newTrip.ArrDep, Arrival = newTrip.Arrival, Departure = newTrip.Departure, Date = newTrip.MovementDate, MovementId = movement.MovementId }; db.Entry(flight).State = EntityState.Added; db.SaveChanges(); db.Dispose(); } return movement; }