Example #1
0
        private async void MapGoogleresultToRestaurantModel(Dictionary <string, string> param, GooglePlacesAPI placesObject, string url)
        {
            List <string> placeIds            = new List <string>();
            Dictionary <string, string> idAdd = new Dictionary <string, string>();

            foreach (Result result in placesObject.results)
            {
                RestaurantModel restaurant = new RestaurantModel();
                restaurant.RestaurantName  = result.name;
                restaurant.PriceRangeIndex = result.price_level;
                restaurant.Open_now        = result.opening_hours.open_now;
                restaurant.Lat             = result.geometry.location.lat;
                restaurant.Lng             = result.geometry.location.lng;
                restaurant.Price_level     = result.price_level;
                restaurant.Rating          = result.rating;
                restaurant.Place_Id        = result.place_id;

                placeIds.Add(result.place_id);
                idAdd.Add(result.place_id, result.formatted_address);

                _context.Restaurants.Add(restaurant);
                //save place id to use in search by id query
                //map addressKey table to Restaurant model address key
                //photos mapped at different time
            }
            _context.SaveChanges();

            List <string> resaurantGuid = new List <string>();

            foreach (string id in placeIds)
            {
                var guid = _context.Restaurants.Where(r => r.Place_Id == id).Select(r => r.RestaurantModelPrimaryKey).FirstOrDefault();
                resaurantGuid.Add(guid);
            }

            MapQueriesToSearchJunctionTable(param, resaurantGuid);


            Mapresultphotocollection(placesObject);


            foreach (string id in placeIds)
            {
                RestaurantModel restaurant = _context.Restaurants.Where(r => r.Place_Id == id).FirstOrDefault();
                GooglePlacesAPI_PlaceIDSearchResults place = await GetResultsById(id);

                restaurant.RestaurantPhone = place.result.formatted_phone_number;
                restaurant.WebsiteUrl      = place.result.website;
                string addr = idAdd[id];
                MapAddressToLocalDb(id, addr); // Adds the new addresses to the context;
            }

            _context.SaveChanges();
        }
Example #2
0
        private async Task <GooglePlacesAPI_PlaceIDSearchResults> GetResultsById(string id)
        {
            string url = $"https://maps.googleapis.com/maps/api/place/details/json?place_id={id}&fields=formatted_phone_number,name,price_level,rating,reviews,vicinity,website&key={Api_Keys.GoogleApiKey}";

            HttpClient client = new HttpClient();

            HttpResponseMessage response = await client.GetAsync(url);

            if (response.IsSuccessStatusCode)
            {
                string json = await response.Content.ReadAsStringAsync();

                GooglePlacesAPI_PlaceIDSearchResults googleplacesapi = JsonConvert.DeserializeObject <GooglePlacesAPI_PlaceIDSearchResults>(json);

                return(googleplacesapi);
            }
            else
            {
                return(null);
            }
        }