Skip to content

Instantly share code, notes, and snippets.

View gantrim's full-sized avatar

Greg Antrim gantrim

View GitHub Profile
@gantrim
gantrim / getUserProfileImageUrl.ts
Created February 3, 2019 18:54
Firebase Part 6 - Get Profile Image
getUserProfileImageUrl() {
if (this.user && this.user.profileImagePath) {
this.storageRef.child(`users/${this.user.id + '/' + this.user.profileImagePath}`).getDownloadURL()
.then(url => {
this.profileImageUrl = url;
});
}
}
@gantrim
gantrim / saveUser.ts
Created February 3, 2019 18:52
Firebase Part 6 - Profile image Upload
/**
* 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;
@gantrim
gantrim / userProfile.component.ts
Created February 3, 2019 18:40
Firebase Part 6 - User and review refs
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();
});
@gantrim
gantrim / postview.js
Created February 3, 2019 15:09
Firebase Part 5 - postView Function
postView(breweryId) {
this.http.post('your-uri', {
breweryId: breweryId
})
.subscribe((data: any) => {
console.log(`You are viewer number ${data.views}`);
});
}
@gantrim
gantrim / expressEndpoint.js
Created February 3, 2019 15:05
Firebase Part 5 - HTTP Express Endpoint
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
@gantrim
gantrim / deleteUserReview.js
Created February 3, 2019 14:58
Firebase Part 5 - Delete User Review
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);
@gantrim
gantrim / deleteBreweryReview.js
Created February 3, 2019 14:54
Firebase Part 5 - Delete Brewery Review
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()
@gantrim
gantrim / updateUserReview.js
Created February 3, 2019 14:46
Firebase Part 5 - Update User Review
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);
}
@gantrim
gantrim / aggregateRatings.js
Created February 3, 2019 14:41
Firebase Part 5 - Aggregate Ratings
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({
@gantrim
gantrim / updateReview.js
Created February 3, 2019 14:39
Firebase Part 5 - update Review Function
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) {