/// <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)); } }