private List <Flight> CreateInternalFlights(DateTime relative) { List <Flight> flights = new List <Flight>(); string time = relative.ToString("dd/MM/yyyy HH:mm:ss"); // run over FilghtPlans. foreach (FlightPlan fp in context.FlightPlan) { string id = fp.ID; fp.InitialLocation = TakeInitialLocation(id).Result; fp.Segments = TakeSegments(id).Result; DateTime start = LocalLibrary.ConvertToDateTime(fp.InitialLocation.DateTime); if (DateTime.Compare(relative, start) < 0) { continue; } Flight f = new Flight(); if (CheckSegments(fp, f, start, relative)) { flights.Add(f); } // Save other properties. f.FlightID = fp.ID; f.Passengers = fp.Passengers; f.CompanyName = fp.CompanyName; f.DateTime = time; f.IsExternal = false; } return(flights); }
public async Task <ActionResult <IEnumerable <Flight> > > GetFlight([FromQuery] string relative_to = null) { if (relative_to == null) { return(NotFound()); } // True if the request contain 'sync_all'. Boolean sync = Request.Query.ContainsKey("sync_all"); FlightDbContext.ServerID.Clear(); context.SaveChanges(); List <Flight> flights = new List <Flight>(); DateTime relative = new DateTime(); try { relative = LocalLibrary.ConvertToDateTime(relative_to); } catch (Exception) { // Relative_to is not a valid time. return(NotFound()); } // Take internal flights. List <Flight> internalFlights = CreateInternalFlights(relative); foreach (Flight inf in internalFlights) { flights.Add(inf); } // Take external flights if the request contain 'sync_all'. if (sync) { List <Flight> externalFlights = CreateExternalFlights(relative_to); foreach (Flight exf in externalFlights) { flights.Add(exf); } } Task <ActionResult <IEnumerable <Flight> > > f = Task <ActionResult <IEnumerable <Flight> > > .Factory.StartNew(() => { return(flights); }); return(await f); }