public static int?GetDistance(string depCountry, string depCity, string depSuburb, string destCountry, string destCity, string destSuburb) { //get from db using (var db = new StallEntities()) { var id = BuilId(depCountry, depCity, depSuburb, destCountry, destCity, destSuburb); var dbResult = GetDistance(id, db); if (dbResult != null) { return(dbResult); } //call google map api var glResult = GoogleMapHelper.GetSuburbDistanceFromGoogleMapApi(depCountry, depCity, depSuburb, destCountry, destCity, destSuburb, GreenspotConfiguration.AccessAccounts["google.map"].Secret); if (glResult == null) { StallApplication.SysError($"[GOOGLE DISTANCE]failed to get distance {depCountry},{depCity},{depSuburb} to {destCountry},{destCity},{destSuburb}"); return(null); } else { //save to db var distance = new SuburbDistance() { ID = id, DepartureCountryCode = depCountry, DepartureCity = depCity, DepartureSuburb = depSuburb, DestinationCountryCode = destCountry, DestinationCity = destCity, DestinationSuburb = destSuburb, Meters = glResult.Value }; db.SuburbDistances.Add(distance); db.SaveChanges(); return(glResult.Value); } } }
//private IEnumerable<Product> GetProducts(Func<Product, bool> condition) //{ // if (Products == null) // { // return null; // } // return Products.Where(condition); //} public int?GetDistance(string destCountryCode, string destCity, string destSuburb) { if (string.IsNullOrEmpty(destCountryCode) || string.IsNullOrEmpty(destCity) || string.IsNullOrEmpty(destSuburb)) { return(null); } if (!CountryId.Equals(destCountryCode) || !City.Equals(destCity)) { return(null); } if (Suburb.Equals(destSuburb)) { return(0); } return(SuburbDistance.GetDistance(CountryId, City, Suburb, destCountryCode, destCity, destSuburb)); }