Skip to content
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

adding query,facts,video functions #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,62 @@ 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.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
});
});
});
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"
})
}
});