From 87ac0a883040a023fc2aeaa007c9101e19a0e2a3 Mon Sep 17 00:00:00 2001 From: rtchiya28 Date: Sun, 30 Sep 2018 10:28:30 +0530 Subject: [PATCH 1/2] adding query,facts,video functions --- functions/index.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/functions/index.js b/functions/index.js index 4b078a9d..747d4664 100644 --- a/functions/index.js +++ b/functions/index.js @@ -143,3 +143,49 @@ app.post('/', isAuthenticated ,function(req,response) }); exports.signUp = functions.https.onRequest(app); + + + +//<--------Random Fact Generation----------> +//Generates random fact about techspardha. +exports.randomFact = functions.https.onRequest((request , response) => { + const numberOfLines = 8; + const randomIndex = Math.floor(Math.random() * numberOfLines); + database.ref('/facts/' + randomIndex).on('value',function(snapshot){ + console.log(snapshot.val()); + response.status(401).json({ + message : snapshot.val() + }); + }); +}); + + +//<------Returning the array of all the videos------> +//Returns the array of videos containing title and url of a video. +exports.getVideo = functions.https.onRequest((request , response) => { + let items = []; + database.ref('/videos').on('value', function(snapshot) { + snapshot.forEach(function(childSnapshot) { + items.push(childSnapshot.key,{ + title : childSnapshot.val().title, + url : childSnapshot.val().url + }); + }); + }); + response.status(401).json(items); +}); + + +//<-----Adding query to database-------> +//only add newly asked query to the database. +exports.addQuery = functions.https.onRequest((request,response)=>{ + const query = request.query.text; + var newPostKey = admin.database().ref().child('queries').push().key; + var updates = {}; + updates['/queries/' + newPostKey] = query; + database.ref().update(updates); + response.status(401).json({ + message : "query successfully added" + }); +}); + From a1498e3969891cb0c8dc2734229c4c56850bbdb6 Mon Sep 17 00:00:00 2001 From: rtchiya28 Date: Wed, 3 Oct 2018 18:56:26 +0530 Subject: [PATCH 2/2] adding changes in query,video and fact functions --- functions/index.js | 83 +++++++++++++++++++++++++++------------------- 1 file changed, 48 insertions(+), 35 deletions(-) diff --git a/functions/index.js b/functions/index.js index 747d4664..26a970f1 100644 --- a/functions/index.js +++ b/functions/index.js @@ -149,43 +149,56 @@ exports.signUp = functions.https.onRequest(app); //<--------Random Fact Generation----------> //Generates random fact about techspardha. exports.randomFact = functions.https.onRequest((request , response) => { - const numberOfLines = 8; - const randomIndex = Math.floor(Math.random() * numberOfLines); - database.ref('/facts/' + randomIndex).on('value',function(snapshot){ - console.log(snapshot.val()); - response.status(401).json({ - message : snapshot.val() + const numberOfLines = 8; + const randomIndex = Math.floor(Math.random() * numberOfLines); + database.ref('/facts/' + randomIndex).on('value',function(snapshot){ + console.log(snapshot.val()); + response.set('Cache-Control', 'public, max-age=300 , s-maxage=600'); + response.status(401).json({ + message : snapshot.val() + }); }); }); -}); - - -//<------Returning the array of all the videos------> -//Returns the array of videos containing title and url of a video. -exports.getVideo = functions.https.onRequest((request , response) => { - let items = []; - database.ref('/videos').on('value', function(snapshot) { - snapshot.forEach(function(childSnapshot) { - items.push(childSnapshot.key,{ - title : childSnapshot.val().title, - url : childSnapshot.val().url + + + //<------Returning the array of all the videos------> + //Returns the array of videos containing title and url of a video. + exports.getVideo = functions.https.onRequest((request , response) => { + let items = []; + database.ref('/videos').on('value', function(snapshot) { + snapshot.forEach(function(childSnapshot) { + items.push(childSnapshot.key,{ + title : childSnapshot.val().title, + url : childSnapshot.val().url + }); }); }); + response.set('Cache-Control', 'public, max-age=300 , s-maxage=600'); + response.status(401).json(items); + }); + + + + // <-----Adding query to database-------> + // only add newly asked query to the database, if query will be null then it will return the empty query message else query will be added to database. + exports.addQuery = functions.https.onRequest((request,response)=>{ + const query = request.query.text; + const email1=req.body.email1; + const email=email1.replace(/\./g, ','); + const email_child='queries/'+email; + if(query!==null) + { + database.ref().child(email_child).push(query); + response.set('Cache-Control', 'public, max-age=300 , s-maxage=600'); + response.status(401).json({ + message : "query successfully added" + }); + } + else + { + response.status(500).json({ + message: "empty query" + }) + } }); - response.status(401).json(items); -}); - - -//<-----Adding query to database-------> -//only add newly asked query to the database. -exports.addQuery = functions.https.onRequest((request,response)=>{ - const query = request.query.text; - var newPostKey = admin.database().ref().child('queries').push().key; - var updates = {}; - updates['/queries/' + newPostKey] = query; - database.ref().update(updates); - response.status(401).json({ - message : "query successfully added" - }); -}); - + \ No newline at end of file