Merge pull request #43 from ClaytonWWilson/add_topic_user

created functions retrieving all topics
This commit is contained in:
Leon Liang 2019-10-24 23:13:39 -04:00 committed by GitHub
commit 65ac1c7384
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 2 deletions

View File

@ -0,0 +1,35 @@
/* eslint-disable promise/always-return */
const admin = require('firebase-admin');
exports.putTopic = (req, res) => {
const newTopic = {
topic: req.body.topic
};
admin.firestore().collection('topics').add(newTopic)
.then((doc) => {
const resTopic = newTopic;
return res.status(200).json(resTopic);
})
.catch((err) => {
console.error(err);
return res.status(500).json({ error: 'something is wrong'});
});
};
exports.getAllTopics = (req, res) => {
admin.firestore().collection('topics').get()
.then((data) => {
let topics = [];
data.forEach(function(doc) {
topics.push(doc.data());
});
return res.status(200).json(topics);
})
.catch((err) => {
console.error(err);
return res.status(500).json({error: 'Failed to fetch all posts written by specific user.'})
})
};

View File

@ -55,11 +55,13 @@ app.post("/putPost", fbAuth, putPost);
/*------------------------------------------------------------------*
* handlers/topic.js *
*------------------------------------------------------------------*/
const { putTopic
const { putTopic, getAllTopics
} = require("./handlers/topic");
// add topic to database
app.post("/putTopic", fbAuth, putTopic);
// get all topics from database
app.get("/getAllTopics", fbAuth, getAllTopics);
exports.api = functions.https.onRequest(app);