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);
        }
        private List <Flight> CreateExternalFlights(string relative_to)
        {
            List <Flight> externalFlights = new List <Flight>();

            foreach (Server s in context.Server)
            {
                string get = s.ServerURL + "api/Flights?relative_to=" + relative_to;

                try
                {
                    externalFlights = LocalLibrary.GetFlightFromServer <List <Flight> >(get);
                }
                catch (Exception)
                {
                    continue;
                }
                foreach (Flight f in externalFlights)
                {
                    f.IsExternal = true;
                    // Save the server that the current flight belongs to him.
                    FlightDbContext.ServerID[f.FlightID] = s;
                }
            }
            context.SaveChanges();
            return(externalFlights);
        }
        public string IDGenerator()
        {
            // Create random ID that look like- '000-000-000'.
            string id   = "";
            int    num1 = LocalLibrary.GetNumber();

            id = id + num1;
            int num2 = LocalLibrary.GetNumber();

            id = id + num2;
            int num3 = LocalLibrary.GetNumber();

            id = id + num3;
            id = id + "-";
            int num4 = LocalLibrary.GetNumber();

            id = id + num4;
            int num5 = LocalLibrary.GetNumber();

            id = id + num5;
            int num6 = LocalLibrary.GetNumber();

            id = id + num6;
            id = id + "-";
            int num7 = LocalLibrary.GetNumber();

            id = id + num7;
            int num8 = LocalLibrary.GetNumber();

            id = id + num8;
            int num9 = LocalLibrary.GetNumber();

            id = id + num9;
            return(id);
        }
        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);
        }
        public async Task <ActionResult <FlightPlan> > GetFlightPlan(string id)
        {
            var loc = await context.FirstLoc.ToListAsync();

            var seg = await context.Segments.ToListAsync();

            var flightPlan = await context.FlightPlan.FindAsync(id);

            if (flightPlan != null)
            {
                flightPlan.InitialLocation = loc.Where(a => a.ID.CompareTo(id) == 0).First();
                flightPlan.Segments        = seg.Where(a => a.ID.CompareTo(id) == 0).ToList();
                return(flightPlan);
            }
            // Ask only the relevant server.
            try
            {
                var s = FlightDbContext.ServerID[id];
                if (s == null)
                {
                    return(NotFound());
                }
                string get = s.ServerURL + "api/FlightPlan/" + id;
                flightPlan = LocalLibrary.GetFlightFromServer <FlightPlan>(get);
                if (flightPlan == null)
                {
                    return(NotFound());
                }
            }
            catch (WebException)
            {
                return(NotFound());
            }
            catch (KeyNotFoundException)
            {
                return(NotFound());
            }

            return(flightPlan);
        }
        public string IDGenerator()
        {
            // Create random ID that look like- 'AA-00000000'.
            string id = "";
            // Generates a key.
            char c1 = LocalLibrary.GetLetter();

            id = id + c1;
            char c2 = LocalLibrary.GetLetter();

            id = id + c2;
            id = id + "-";
            // Generates the numbers.
            int num1 = LocalLibrary.GetNumber();

            id = id + num1;
            int num2 = LocalLibrary.GetNumber();

            id = id + num2;
            int num3 = LocalLibrary.GetNumber();

            id = id + num3;
            int num4 = LocalLibrary.GetNumber();

            id = id + num4;
            int num5 = LocalLibrary.GetNumber();

            id = id + num5;
            int num6 = LocalLibrary.GetNumber();

            id = id + num6;
            int num7 = LocalLibrary.GetNumber();

            id = id + num7;
            int num8 = LocalLibrary.GetNumber();

            id = id + num8;
            return(id);
        }