-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Add functions to FriendlyEats Quickstart Angular Rewrite
#724
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| const restuarantDocRef = db.doc( | ||
| `restaurants/${event.params.restaurtantID}`); | ||
|
|
||
| logger.info(`Fetching data for restaurant ` + |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: remove this info or the one above - since we haven't performed any async reads yet having two feels redundant.
| `${event.params.restaurtantID}`); | ||
| const restaurantDocFromFirebase = await restuarantDocRef.get(); | ||
| const restaurantData = restaurantDocFromFirebase.data() as Restaurant; | ||
| const numRatingsReported = restaurantData.numRatings; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's remove numRatingsReported and inline the restaurantData.numRatings below
| const restaurantDocFromFirebase = await restuarantDocRef.get(); | ||
| const restaurantData = restaurantDocFromFirebase.data() as Restaurant; | ||
| const numRatingsReported = restaurantData.numRatings; | ||
| const fetchedRatingDocs = (await db.collection(`restaurants/${event.params.restaurtantID}/ratings`).get()).docs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: line-length
| * new reviews (and not the deletion of existing reviews), we can | ||
| * expect that when this function is triggered the number of reviews | ||
| * listed in the `restaurant.numRatings` field will be strictly less | ||
| * than the actual length of the `ratings` sub-collection. In the case |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well said
| const numRatingsReported = restaurantData.numRatings; | ||
| const fetchedRatingDocs = (await db.collection(`restaurants/${event.params.restaurtantID}/ratings`).get()).docs | ||
| const actualRatings: Rating[] = [] | ||
| fetchedRatingDocs.forEach(rating => actualRatings.push(rating.data() as Rating)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use a .map() instead of forEach and we can assign directly to a variable. Something like this:
const actualRatings: Rating[] = fetchedRatingDocs.map(ratingDoc => return ratingDoc.data() as Rating);
|
|
||
| // Calculate average review | ||
| let sumOfRatings = 0; | ||
| actualRatings.forEach(currentRating => sumOfRatings += currentRating.rating) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to the above, rather than forEach we can use a reducer for style points
const sumOfRatings = actualRatings.reduce(
(sum, currentRating) => sum + currentRating.rating,
/* initial sum value */ 0
);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great suggestion, added in next commit!
This PR adds support for Cloud Functions and the Cloud Functions Emulator to the "FriendlyEats" Quickstart in
firestore/angular-rewriteThis PR also gets rid of the stale data housed in
firestore/exported-firestoreandfirestore/angular-rewrite/exported-firestore. Fresher data is added in #723