This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getUserProfileImageUrl() { | |
if (this.user && this.user.profileImagePath) { | |
this.storageRef.child(`users/${this.user.id + '/' + this.user.profileImagePath}`).getDownloadURL() | |
.then(url => { | |
this.profileImageUrl = url; | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Save user to Firestore | |
* If a profileImageFile was uploaded, save that to the Storage reference | |
*/ | |
saveUser() { | |
if (this.profileImageFile) { | |
this.uploadTask = this.storageRef.child(`users/${this.user.id + '/' + this.filePath}`).put(this.profileImageFile); | |
this.uploadTask.on('state_changed', (snapshot) => { | |
const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
this.userRef = this.db.collection('users').doc(firebase.auth().currentUser.uid); | |
this.userRef.onSnapshot(doc => { | |
this.user = { ...doc.data(), | |
id: doc.id | |
}; | |
// Get the image url whenever the user changes. | |
this.getUserProfileImageUrl(); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
postView(breweryId) { | |
this.http.post('your-uri', { | |
breweryId: breweryId | |
}) | |
.subscribe((data: any) => { | |
console.log(`You are viewer number ${data.views}`); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
expressApp.post('/brewery-viewed', (req, res) => { | |
const breweryId = req.body.breweryId; | |
const docRef = admin.firestore().collection('breweries').doc(breweryId); | |
return docRef.get() | |
.then(doc => { | |
const brewery = doc.data(); | |
const views = brewery.views ? brewery.views + 1 : 1; | |
docRef.update({ | |
views: views |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports.deleteUserReview = function(snapshot, context) { | |
const reviewId = context.params.reviewId; | |
const deletedReview = snapshot.data(); | |
const deleteBatch = admin.firestore().batch(); | |
const breweryReviewDocRef = admin.firestore() | |
.collection('breweries') | |
.doc(deletedReview.breweryId) | |
.collection('reviews') | |
.doc(reviewId); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports.deleteBreweryReview = function(snapshot, context) { | |
const reviewId = context.params.reviewId; | |
const deletedReview = snapshot.data(); | |
const userReviewDocRef = admin.firestore() | |
.collection('users') | |
.doc(deletedReview.uid) | |
.collection('reviews') | |
.doc(reviewId); | |
return userReviewDocRef.delete() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports.updateUserReview = function(change, context) { | |
const reviewId = context.params.reviewId; | |
const previousValue = change.before.data(); | |
const newValue = change.after.data(); | |
const docRef = admin.firestore().collection('breweries').doc(newValue.breweryId); | |
updateReview(docRef, newValue, previousValue, reviewId); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function aggregateRatings(breweryId) { | |
const docRef = admin.firestore().collection('breweries').doc(breweryId); | |
return docRef.collection('reviews').get() | |
.then(querySnapshot => { | |
const numberReviews = querySnapshot.size; | |
const totalRating = querySnapshot.docs.map(doc => doc.data().rating) | |
.reduce((rating, total) => rating + total, 0); | |
const averageRating = (totalRating / numberReviews).toFixed(2); | |
return docRef.update({ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function updateReview(docRefToUpdate, newValue, previousValue, reviewId) { | |
if (checkReviewChanged(newValue, previousValue)) { | |
docRefToUpdate.collection('reviews').doc(reviewId).set(newValue); | |
} | |
else { | |
return null; | |
} | |
if (newValue && previousValue && newValue.rating === previousValue.rating) { |