-
Notifications
You must be signed in to change notification settings - Fork 6
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 event functions #2
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -143,3 +143,183 @@ app.post('/', isAuthenticated ,function(req,response) | |
}); | ||
|
||
exports.signUp = functions.https.onRequest(app); | ||
|
||
|
||
|
||
|
||
|
||
const db = admin.database().ref(); | ||
|
||
exports.addEvent = functions.https.onRequest((req,res) => { | ||
|
||
let eventCategory = req.body.eventCategory; | ||
let eventName = req.body.eventName; | ||
let startTime = req.body.startTime; | ||
let endTime = req.body.endTime; | ||
let eventDescription = req.body.eventDescription; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make generic body design of event data. Many new fields can be introduced later on. So accept form data as json and parse it here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okkk |
||
db.child(`events/${eventCategory}/${eventName}`).set({ | ||
|
||
name : eventName, | ||
startTime : startTime, | ||
endTime : endTime | ||
}) | ||
.then((snapshot) => { | ||
console.log('done') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Modify the success message to "Added {event} to timeline successfully". |
||
}).catch((err) => { | ||
res.send(err) | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Appropriate error statement: Error occurred while adding to timeline |
||
|
||
db.child(`eventDescription/${eventCategory}/${eventName}`).set({ | ||
|
||
name : eventName, | ||
eventDescription : eventDescription, | ||
startTime : startTime, | ||
endTime : endTime | ||
}).then((snapshot) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change to generic implementation |
||
return res.send(snapshot.val()) | ||
}).catch((err) => { | ||
return res.send(err) | ||
}) | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Appropriate log messages. |
||
|
||
exports.getCategories = functions.https.onRequest((req,res) => { | ||
|
||
return db.child('events').once('value') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not hard-code these strings. Declare global variables for references. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no need to declare them global . this is the general uniform format of writing code . |
||
.then((snapshot) => { | ||
|
||
var data = {categories : []} | ||
for(var i in snapshot.val()) | ||
{ | ||
var obj = {}; | ||
obj.name = i; | ||
data.categories.push(obj); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Response should be like : |
||
} | ||
return res.send(data); | ||
}) | ||
.catch((err) => { | ||
return res.send(err); | ||
}) | ||
}) | ||
|
||
exports.getEventNames = functions.https.onRequest((req,res) => { | ||
|
||
if(req.query.category == 'all') | ||
{ | ||
return db.child('events').once('value') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not hard-code these strings. Declare global variables for references. |
||
.then((snapshot) => { | ||
|
||
var data = {events : []} | ||
var database=snapshot.val(); | ||
for(var category in database) | ||
{ | ||
for(let event in database[category]) | ||
{ | ||
var obj = {}; | ||
obj.category = category, | ||
obj.name = database[category][event].name; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change this as previous: { |
||
data.events.push(obj); | ||
} | ||
} | ||
|
||
return res.send(data); | ||
}) | ||
.catch((err) => { | ||
return res.send(err); | ||
}) | ||
} | ||
else if(req.query.category == 'one') | ||
{ | ||
let cat = req.query.eventCategory; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cat? change it |
||
return db.child(`events/${cat}`).once('value') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not hard-code these strings. Declare global variables for references. |
||
.then((snapshot) => { | ||
|
||
return res.send(snapshot); | ||
}) | ||
} | ||
else { | ||
return res.send("Invalid parameters."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Print parameter also. |
||
} | ||
}) | ||
|
||
exports.getEventDescription = functions.https.onRequest((req,res) => { | ||
|
||
if(req.query.events == 'all') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this. Dead code. Never all events are required. Use cases are:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okok |
||
{ | ||
return db.child('eventDescription').once('value') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not hard-code these strings. Declare global variables for references. |
||
.then((snapshot) => { | ||
|
||
var data = {eventDesciption : []} | ||
var database = snapshot.val(); | ||
|
||
for(var category in snapshot.val()){ | ||
|
||
var events=database[category]; | ||
|
||
for(var event in events) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. build separate function for this |
||
{ | ||
var obj = {}; | ||
obj.category = category; | ||
obj.name = events[event].name; | ||
obj.eventDescription = events[event].eventDescription; | ||
obj.startTime = events[event].startTime; | ||
obj.endTime = events[event].endTime; | ||
data.eventDesciption.push(obj); | ||
} | ||
} | ||
return res.send(data); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no processing required |
||
}) | ||
.catch((err) => { | ||
return res.send(err) | ||
}) | ||
} | ||
else if(req.query.events == 'one') | ||
{ | ||
let eventCategory = req.query.eventCategory | ||
let eventName = req.query.eventName | ||
|
||
if(eventCategory == null || eventName == null) { | ||
res.send("Insufficient parameters.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Print event category and event name values |
||
} | ||
|
||
db.child(`eventDescription/${eventCategory}/${eventName}`).once('value') | ||
.then((snapshot) => { | ||
if(snapshot == null) { | ||
return res.send("Event Doesn't Exist."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. print values |
||
} | ||
return res.send(snapshot.val()) | ||
}) | ||
.catch((err) => { | ||
return res.send(err) | ||
}) | ||
} | ||
else if(req.query.events == 'cat') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cat? and hardcoded? |
||
{ | ||
let categoryName = req.query.eventCategory; | ||
if(categoryName == null) | ||
{ | ||
return res.send("Insufficient Parameters."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change error message. |
||
} | ||
|
||
db.child(`eventDescription/${categoryName}`).once('value') | ||
.then((snapshot) => { | ||
if(snapshot == null) { | ||
return res.send("Invalid Category."); | ||
} | ||
|
||
return res.send(snapshot.val()); | ||
}) | ||
} | ||
|
||
}) | ||
|
||
exports.getEventTimeline = functions.https.onRequest((req,res) => { | ||
|
||
return db.child('events').once('value') | ||
.then((snapshot) => { | ||
return res.send(snapshot.val()) | ||
}) | ||
.catch((err) => { | ||
return res.send(err) | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No comments? Please explain this function in comments