mirror of
https://github.com/ClaytonWWilson/CS307-Team24.git
synced 2025-12-16 10:18:48 +00:00
73 lines
2.2 KiB
JavaScript
73 lines
2.2 KiB
JavaScript
/* eslint-disable prefer-arrow-callback */
|
|
/* eslint-disable promise/always-return */
|
|
const { admin, db } = require("../util/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) => {
|
|
db.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.'})
|
|
})
|
|
};
|
|
|
|
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,
|
|
postId: doc.id,
|
|
});
|
|
});
|
|
return res.status(200).json(posts);
|
|
})
|
|
.catch((err) => {
|
|
console.error(err);
|
|
return res.status(500).json({ error: err.code });
|
|
});
|
|
}; |