コード例 #1
0
        /// <summary>
        /// Get the coordination information of airport asynchronously
        /// Firstly, the method checks if airport informations exist in database.
        /// If exist, directly returns coordinates from database.
        /// If not exist, it calls external api and parse informations to get coordinations
        /// On the last step, it saves informations to database for get it on the next request and avoid api calls
        /// </summary>
        /// <param name="airportIataCode">Iata code of airport</param>
        /// <returns>coordination informations in Tuple: (Item1:latitude, Item2:longitude)</returns>
        private async Task <Tuple <double, double> > GetAirportCoordinations(string airportIataCode)
        {
            using (var con = new AppDbContext(new DbContextOptions <AppDbContext>()))
            {
                var    repository   = new AirportRepository(con);
                double latitude     = 0.0;
                double longitude    = 0.0;
                var    savedAirport = await repository.GetAirport(airportIataCode);

                // If airport exist in db, no need to call external api
                if (savedAirport != null)
                {
                    SetCoordinates(savedAirport, out latitude, out longitude);
                }
                else
                {
                    IRestResponse response = AirportHelper.CallApi(airportIataCode);
                    if (response.IsSuccessful == false)
                    {
                        throw new Exception(AirportHelper.ParseApiError(response));
                    }
                    var airport = AirportHelper.GetAirportFromResponse(response);
                    if (airport != null)
                    {
                        SetCoordinates(airport, out latitude, out longitude);
                        // Save airport to in-memory database using by repository
                        await repository.AddAirport(airport);
                    }
                }
                return(Tuple.Create(latitude, longitude));
            }
        }
コード例 #2
0
        private static string GetMinimumRoute(string toAirport)
        {
            if (toAirport == "LGA")
            {
                return(string.Empty);
            }

            string result = string.Empty;

            string[] airports = AirportHelper.AirportList();
            string[,] routes = AirportHelper.RoutesList();
            string startingAirport = "LGA";
            int    answer          = 3;

            string[,] tempoRoute = new string[99, 99];

            foreach (var item in routes)
            {
            }
            ;



            List <int> adjacent = new List <int>();//[n]

            // go through routes
            //Korasaju's algo
            string[] who = new string[99];

            return(result);
        }
コード例 #3
0
        public void TestMethod_emptyString()
        {
            string        source   = string.Empty;
            List <string> expected = new List <string>();
            List <string> result   = AirportHelper.ListAllRoutes(source);

            Assert.IsTrue(AssertListAreEqual(result, expected));
        }
コード例 #4
0
        public void TestMethod_GetAllPossibleAirport()
        {
            string        source   = "LGA";
            List <string> expected = new List <string> {
                "BGI"
            };
            List <string> result = AirportHelper.GetAllPossibleAirport(source);

            Assert.IsTrue(AssertListAreEqual(result, expected));
        }
コード例 #5
0
        public void TestMethod_LGA()
        {
            string        source   = "LGA";
            List <string> expected = new List <string> {
                "LGA-BGI", "LGA-JFK"
            };
            List <string> result = AirportHelper.ListAllRoutes(source);

            Assert.IsTrue(AssertListAreEqual(result, expected));
        }
コード例 #6
0
            public Builder GetAllAirportsFromDB()
            {
                foreach (var data in this.datas)
                {
                    if (!string.IsNullOrEmpty(data))
                    {
                        ListAirport.Add(AirportHelper.GetDataAirportFromString(data));
                    }
                }

                return(this);
            }
コード例 #7
0
        public bool IsRouteValid()
        {
            bool result = false;

            if (FromAirport == ToAirport)
            {
                return(false);
            }

            var allRoutes = AirportHelper.RouteList();

            result = allRoutes.Contains($"{FromAirport.Name}-{ToAirport.Name}") || allRoutes.Contains($"{ToAirport.Name}-{FromAirport.Name}");
            return(result);
        }
コード例 #8
0
        public async Task <IActionResult> GetDistances(string airport1, string airport2)
        {
            try
            {
                if (string.IsNullOrEmpty(airport1) || string.IsNullOrEmpty(airport2))
                {
                    return(BadRequest(new { Error = "airport code can not be null or empty", Success = false }));
                }

                // Firstly, check if result is cached previously, if it's exist in cache, directly return
                string key = string.Format(_cacheKeyFormula, airport1, airport2);
                if (_memoryCache.TryGetValue(key, out double distanceCache))
                {
                    return(Ok(new { Distance_Between_Airports = distanceCache + " miles", Success = true }));
                }

                List <string> airports = new List <string> {
                    airport1, airport2
                };
                List <Tuple <double, double> > airportCoordinations = new List <Tuple <double, double> >();

                var taskList = new List <Task <Tuple <double, double> > >();
                foreach (string airportIataCode in airports)
                {
                    taskList.Add(
                        Task.Run(async() =>
                    {
                        return(await GetAirportCoordinations(airportIataCode));
                    }));
                }
                await Task.WhenAll(taskList.ToArray());

                airportCoordinations.Add(taskList[0].Result);
                airportCoordinations.Add(taskList[1].Result);
                // Calculate distance in miles between two airports
                var distance = AirportHelper.CalculateDistanceInMiles(airportCoordinations[0].Item1, airportCoordinations[0].Item2,
                                                                      airportCoordinations[1].Item1, airportCoordinations[1].Item2);
                // Cache result of request(airport1/airport2)
                CacheResult(key, distance);
                //Cache result of the vice versa of request (airport2 / airport1). Because both request have same result
                key = string.Format(_cacheKeyFormula, airport2, airport1);
                CacheResult(key, distance);

                return(Ok(new { Distance_Between_Airports = distance + " miles", Success = true }));
            }
            catch (Exception ex)
            {
                return(BadRequest(new { Error = ex.Message, Success = false }));
            }
        }
コード例 #9
0
        static void Main()
        {
            string[] airports = AirportHelper.AirportList();
            string[,] routes = AirportHelper.RoutesList();
            string startingAirport = "LGA";
            int    answer          = 3;

            // write a method that gives the minimum connexion from startingAirport to any airports
            foreach (string airport in airports)
            {
                Console.WriteLine($"From LGA to {airport}, you have to go through {GetMinimumRoute(airport)}");
            }

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
コード例 #10
0
ファイル: HomeController.cs プロジェクト: sbronzetti/FOD
 public ActionResult Airports()
 {
     Models.AirportsModel s = new Models.AirportsModel();
     s.Airports = AirportHelper.GetAirports();
     return(View(s));
 }