mirror of
https://github.com/ClaytonWWilson/CS307-Team24.git
synced 2025-12-16 10:18:48 +00:00
44 lines
1.3 KiB
JavaScript
44 lines
1.3 KiB
JavaScript
/* eslint-disable prefer-arrow-callback */
|
|
/* eslint-disable promise/always-return */
|
|
const admin = require('firebase-admin');
|
|
exports.putPost = (req, res) => {
|
|
|
|
const newPost = {
|
|
body: req.body.body,
|
|
userHandle: req.body.userHandle,
|
|
userImage: req.body.userImage,
|
|
userID: req.userData.userID,
|
|
microBlogTitle: req.body.microBlogTitle,
|
|
createdAt: new Date().toISOString(),
|
|
likeCount: 0,
|
|
commentCount: 0,
|
|
microBlogTopics: req.body.microBlogTopics
|
|
|
|
};
|
|
|
|
admin.firestore().collection('posts').add(newPost)
|
|
.then((doc) => {
|
|
const resPost = newPost;
|
|
resPost.postId = doc.id;
|
|
return res.status(200).json(resPost);
|
|
})
|
|
.catch((err) => {
|
|
console.error(err);
|
|
return res.status(500).json({ error: 'something is wrong'});
|
|
});
|
|
};
|
|
|
|
exports.getallPostsforUser = (req, res) => {
|
|
admin.firestore().collection('posts').where('userHandle', '==', 'new user' ).get()
|
|
.then((data) => {
|
|
let posts = [];
|
|
data.forEach(function(doc) {
|
|
posts.push(doc.data());
|
|
});
|
|
return res.status(200).json(posts);
|
|
})
|
|
.catch((err) => {
|
|
console.error(err);
|
|
return res.status(500).json({error: 'Failed to fetch all posts written by specific user.'})
|
|
})
|
|
}; |