From 340d3537a66013db75c3a9ecbb28efbd98d10bb1 Mon Sep 17 00:00:00 2001 From: Leon Liang Date: Thu, 24 Oct 2019 23:13:03 -0400 Subject: [PATCH] created functions retrieving all topics --- functions/handlers/topic.js | 35 +++++++++++++++++++++++++++++++++++ functions/index.js | 6 ++++-- 2 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 functions/handlers/topic.js diff --git a/functions/handlers/topic.js b/functions/handlers/topic.js new file mode 100644 index 0000000..bac0322 --- /dev/null +++ b/functions/handlers/topic.js @@ -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.'}) + }) +}; \ No newline at end of file diff --git a/functions/index.js b/functions/index.js index fb60497..737d44f 100644 --- a/functions/index.js +++ b/functions/index.js @@ -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);