From 3f3a93be8c05c64f20a6b39a6ad0e8248f1d4109 Mon Sep 17 00:00:00 2001 From: shobhitm23 Date: Mon, 28 Oct 2019 14:02:55 -0400 Subject: [PATCH] Added getAllPosts --- functions/handlers/post.js | 26 ++++++++++++++++++++++++++ functions/handlers/topic.js | 1 + functions/handlers/users.js | 1 + 3 files changed, 28 insertions(+) diff --git a/functions/handlers/post.js b/functions/handlers/post.js index 6159e71..19753bc 100644 --- a/functions/handlers/post.js +++ b/functions/handlers/post.js @@ -41,4 +41,30 @@ exports.getallPostsforUser = (req, res) => { console.error(err); return res.status(500).json({error: 'Failed to fetch all posts written by specific user.'}) }) +}; + +exports.getAllPosts = (req, res) => { + db.collection('posts') + .orderBy('createdAt', 'desc') + .get() + .then((data) => { + let posts = []; + data.forEach((doc) => { + posts.push({ + body: doc.data().body, + userHandle: doc.data().userHandle, + createdAt: doc.data().createdAt, + commentCount: doc.data().commentCount, + likeCount: doc.data().likeCount, + userImage: doc.data().userImage, + microBlogTitle: doc.data().microBlogTitle, + microBlogTopics: doc.data().microBlogTopics, + }); + }); + return res.json(posts); + }) + .catch((err) => { + console.error(err); + res.status(500).json({ error: err.code }); + }); }; \ No newline at end of file diff --git a/functions/handlers/topic.js b/functions/handlers/topic.js index bac0322..1dd9e1e 100644 --- a/functions/handlers/topic.js +++ b/functions/handlers/topic.js @@ -1,3 +1,4 @@ +/* eslint-disable prefer-arrow-callback */ /* eslint-disable promise/always-return */ const admin = require('firebase-admin'); exports.putTopic = (req, res) => { diff --git a/functions/handlers/users.js b/functions/handlers/users.js index 1af539d..4417521 100644 --- a/functions/handlers/users.js +++ b/functions/handlers/users.js @@ -1,3 +1,4 @@ +/* eslint-disable prefer-arrow-callback */ /* eslint-disable promise/catch-or-return */ const { admin, db } = require("../util/admin");