This commit is contained in:
Aditya Sankaran
2019-11-01 13:20:34 -04:00
parent 6dbca16ace
commit cc20e30990
6 changed files with 96 additions and 20 deletions

View File

@@ -50,7 +50,7 @@ exports.getPost = (req, res) => {
}
exports.likePost = (req, res) => {
let postData;
const likeDoc = admin.firestore().collection('likes').where('userHandle', '==', req.userData.handle)
.where('postId', '==', req.params.postId).limit(1);
@@ -59,16 +59,15 @@ exports.likePost = (req, res) => {
likeDoc.get()
.then((data) => {
if (data.empty) {
return admin.firestore().collection('likes').add({
admin.firestore().collection('likes').add({
postId : req.params.postId,
userHandle: req.userData.handle
})
.then(() => {
postData.likeCount++;
return postDoc.update({likeCount : postData.likeCount})
return postDoc.update({likeCount : firebase.firestore.FieldValue.increment(1) })
})
.then(() => {
return res.status(200).json(postData);
return res.status(200).json(postDoc);
})
}
else {
@@ -114,6 +113,30 @@ exports.unlikePost = (re, res) => {
}
exports.quotePost = (req, res) => {
const likeDoc = admin.firestore().collection('posts').where('postId', '==', req.params.postId).limit(1);
const quotedPost = {
quotingUser : req.userData.handle,
quotedAt: new Date().toISOString(),
body: req.body.body,
}
admin.firestore().collection('posts').add(quotedPost)
.then((doc) => {
const resPost = quotedPost;
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.getallPostsforFeed = (req, res) => {
admin.firestore().collection('posts').get()
.then((data) => {

View File

@@ -44,7 +44,7 @@ app.get("/user", fbAuth, getAuthenticatedUser);
/*------------------------------------------------------------------*
* handlers/post.js *
*------------------------------------------------------------------*/
const { getallPostsforUser, putPost, getPost, getallPostsforFeed, likePost, unlikePost
const { getallPostsforUser, putPost, getPost, getallPostsforFeed, likePost, unlikePost, quotePost
} = require("./handlers/post");
app.get("/getallPostsforUser", fbAuth, getallPostsforUser);
@@ -54,6 +54,8 @@ app.get("/getallPostsforFeed", fbAuth, getallPostsforFeed);
app.get("/putPost/:postId", fbAuth, getPost);
app.get("/putPost/:postId/like", fbAuth, likePost);
app.get("/putPost/:postId/unlike", fbAuth, unlikePost);
app.post("/putPost/:postId/quote", fbAuth, quotePost);
// Adds one post to the database