Last active
April 5, 2016 12:42
-
-
Save Bernardstanislas/42bafe01fb9778f111a1687f254cc3dd to your computer and use it in GitHub Desktop.
Squash hook as a bridge to firebase
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
import Firebase from 'firebase'; | |
const firebaseRef = new Firebase('https://squanalytics.firebaseio.com'); | |
const pointsRef = firebaseRef.child('points'); | |
const stanPointsRef = pointsRef.child('stan'); | |
const arthurPointsRef = pointsRef.child('arthur'); | |
export default async function (hook) { | |
const currentTime = new Date(); | |
let {player, increment} = hook.params; | |
increment = JSON.parse(increment); | |
const respond = error => { | |
if (error) { | |
hook.res.status(500).json(error); | |
} else { | |
hook.res.json('ok'); | |
} | |
}; | |
if (increment) { | |
switch(player) { | |
case 'stan': | |
stanPointsRef.push().set({time: currentTime.getTime()}, respond); | |
break; | |
case 'arthur': | |
arthurPointsRef.push().set({time: currentTime.getTime()}, respond); | |
break; | |
default: | |
hook.res.status(404).json(`${player} does not exist`); | |
} | |
} else { | |
switch(player) { | |
case 'stan': | |
stanPointsRef.orderByKey().limitToLast(1).once('child_added', snap => { | |
snap.ref().remove(respond); | |
}); | |
break; | |
case 'arthur': | |
arthurPointsRef.orderByKey().limitToLast(1).once('child_added', snap => { | |
snap.ref().remove(respond); | |
}); | |
break; | |
default: | |
hook.res.status(404).json(`${player} does not exist`); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment