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 1 commit
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
46 changes: 46 additions & 0 deletions functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Owner

@devgrpnitkkr devgrpnitkkr Sep 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remember to change it as per the number of facts.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay.

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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think directly returning snapshot.val() should work. no need to iterate and make separate object

});


//<-----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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

validate query if not null.
Need to add captcha validation.
Directly push into reference, why building a separate object?

var updates = {};
updates['/queries/' + newPostKey] = query;
database.ref().update(updates);
response.status(401).json({
message : "query successfully added"
});
});