Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions services/google.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,23 @@ const getRestaurants = async (zip) => {

return axios
.get(url)
.then((res) => {
.then(async (res) => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we factor this out into its own separate function?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

const { results } = res.data;

return Promise.allSettled(
results.map((restaurant) =>
new GoogleRestaurant({ ...restaurant, zip: zip }).save()
)
results.map((restaurant) => {
const placeDetailsUrl = `https://maps.googleapis.com/maps/api/place/details/json?place_id=${restaurant.place_id}&fields=photo&key=${API_KEY}`;

return axios.get(placeDetailsUrl)
.then((res) => {
const { result } = res.data;
const photos = result.photos || [];
// Limit to 10 photos. To remove limit, change to restaurant.photos = photos;
restaurant.photos = photos.slice(0, 10);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This slicing should go somewhere else. No data received by a Google Places API call should be thrown away. Maybe add it to the part where we create the Restaurant object (rather than GoogleRestaurant)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll just remove the slice for now. I'm thinking we save everything but then when we display, we only display like 10.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whenever we do a call to an external API, we want to immediately save the data it returns in as raw of a format as possible to minimize the chance of data loss. So the GoogleRestaurant object shouldn't be touched. Instead, we should add this after we've completed the save of the GoogleRestaurant. Preferably it goes something like this:

query google places for restaurants -> immediately turn the result into a GoogleRestaurant object and save -> do the place photos query -> immediately save those into their own object, perhaps call it GoogleRestaurantImage -> finally take the results of all the previous steps and construct a Restaurant object and save that


return new GoogleRestaurant({ ...restaurant, zip: zip }).save();
});
})
);
})
.then((restaurantsSettled) =>
Expand All @@ -66,6 +77,8 @@ const getRestaurants = async (zip) => {
});
};



module.exports = {
getImageTags,
getRestaurantImages,
Expand Down