mirror of
https://github.com/ClaytonWWilson/CS307-Team24.git
synced 2026-03-10 13:15:05 +00:00
Liking posts with Redux and Material UI
This commit is contained in:
@@ -213,85 +213,178 @@ exports.checkforLikePost = (req, res) => {
|
||||
}
|
||||
|
||||
exports.likePost = (req, res) => {
|
||||
let postData;
|
||||
const likeDoc = admin.firestore().collection('likes').where('userHandle', '==', req.user.handle)
|
||||
.where('postId', '==', req.params.postId).limit(1);
|
||||
const postId = req.params.postId;
|
||||
let likedPostDoc;
|
||||
db.doc(`/users/${req.userData.handle}`)
|
||||
.get()
|
||||
.then((userDoc) => {
|
||||
let likes = userDoc.data().likes;
|
||||
if (likes === undefined || likes === null) {
|
||||
likes = [];
|
||||
}
|
||||
|
||||
const postDoc = db.doc(`/posts/${req.params.postId}`);
|
||||
if (likes.includes(postId)) {
|
||||
return res.status(400).json({error: "This user has already liked this post"});
|
||||
}
|
||||
|
||||
postDoc.get()
|
||||
.then((doc) => {
|
||||
if(doc.exists) {
|
||||
postData = doc.data();
|
||||
return likeDoc.get();
|
||||
}
|
||||
else
|
||||
{
|
||||
return res.status(404).json({error: 'Post not found'});
|
||||
}
|
||||
})
|
||||
.then((data) => {
|
||||
if (data.empty) {
|
||||
return admin.firestore().collection('likes').add({
|
||||
postId : req.params.postId,
|
||||
userHandle: req.user.handle
|
||||
likes.push(postId);
|
||||
|
||||
})
|
||||
.then(() => {
|
||||
postData.likeCount++;
|
||||
return postDoc.update({likeCount : postData.likeCount})
|
||||
})
|
||||
.then(() => {
|
||||
return res.status(200).json(postData);
|
||||
})
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
return res.status(500).json({error: 'Something is wrong'});
|
||||
})
|
||||
return userDoc.ref.update({likes})
|
||||
})
|
||||
.then(() => {
|
||||
return db.doc(`/posts/${postId}`).get()
|
||||
|
||||
})
|
||||
.then((postDoc) => {
|
||||
let postData = postDoc.data();
|
||||
postData.likeCount++;
|
||||
likedPostDoc = postData;
|
||||
return postDoc.ref.update({likeCount : postData.likeCount})
|
||||
})
|
||||
.then(() => {
|
||||
return res.status(201).json(likedPostDoc);
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
return res.status(500).json({error: err});
|
||||
})
|
||||
|
||||
// let postData;
|
||||
// const likeDoc = admin.firestore().collection('likes').where('userHandle', '==', req.user.handle)
|
||||
// .where('postId', '==', req.params.postId).limit(1);
|
||||
|
||||
// const postDoc = db.doc(`/posts/${req.params.postId}`);
|
||||
|
||||
// postDoc.get()
|
||||
// .then((doc) => {
|
||||
// if(doc.exists) {
|
||||
// postData = doc.data();
|
||||
// return likeDoc.get();
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// return res.status(404).json({error: 'Post not found'});
|
||||
// }
|
||||
// })
|
||||
// .then((data) => {
|
||||
// if (data.empty) {
|
||||
// return admin.firestore().collection('likes').add({
|
||||
// postId : req.params.postId,
|
||||
// userHandle: req.user.handle
|
||||
|
||||
// })
|
||||
// .then(() => {
|
||||
// postData.likeCount++;
|
||||
// return postDoc.update({likeCount : postData.likeCount})
|
||||
// })
|
||||
// .then(() => {
|
||||
// return res.status(200).json(postData);
|
||||
// })
|
||||
// }
|
||||
// })
|
||||
// .catch((err) => {
|
||||
// return res.status(500).json({error: 'Something is wrong'});
|
||||
// })
|
||||
|
||||
}
|
||||
|
||||
|
||||
exports.unlikePost = (req, res) => {
|
||||
|
||||
let postData;
|
||||
const likeDoc = admin.firestore().collection('likes').where('userHandle', '==', req.user.handle)
|
||||
.where('postId', '==', req.params.postId).limit(1);
|
||||
const postId = req.params.postId;
|
||||
let likedPostDoc;
|
||||
db.doc(`/users/${req.userData.handle}`)
|
||||
.get()
|
||||
.then((userDoc) => {
|
||||
let likes = userDoc.data().likes;
|
||||
if (likes === undefined || likes === null) {
|
||||
likes = [];
|
||||
}
|
||||
|
||||
const postDoc = db.doc(`/posts/${req.params.postId}`);
|
||||
if (!likes.includes(postId)) {
|
||||
return res.status(400).json({error: "This user hasn't liked this post yet"});
|
||||
}
|
||||
|
||||
postDoc.get()
|
||||
.then((doc) => {
|
||||
if(doc.exists) {
|
||||
postData = doc.data();
|
||||
return likeDoc.get();
|
||||
}
|
||||
else
|
||||
{
|
||||
return res.status(404).json({error: 'Post not found'});
|
||||
}
|
||||
})
|
||||
.then((data) => {
|
||||
return db
|
||||
.doc(`/likes/${data.docs[0].id}`)
|
||||
.delete()
|
||||
.then(() => {
|
||||
postData.likeCount--;
|
||||
return postDoc.update({ likeCount: postData.likeCount });
|
||||
})
|
||||
.then(() => {
|
||||
res.status(200).json(postData);
|
||||
});
|
||||
let i;
|
||||
for (i = 0; i < likes.length; i++) {
|
||||
if (likes[i] === postId) {
|
||||
likes.splice(i, 1);
|
||||
}
|
||||
}
|
||||
|
||||
return userDoc.ref.update({likes})
|
||||
})
|
||||
.then(() => {
|
||||
return db.doc(`/posts/${postId}`).get()
|
||||
|
||||
})
|
||||
.then((postDoc) => {
|
||||
let postData = postDoc.data();
|
||||
postData.likeCount--;
|
||||
likedPostDoc = postData;
|
||||
return postDoc.ref.update({likeCount : postData.likeCount})
|
||||
})
|
||||
.then(() => {
|
||||
return res.status(201).json(likedPostDoc);
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
return res.status(500).json({error: err});
|
||||
})
|
||||
|
||||
// let postData;
|
||||
// const likeDoc = admin.firestore().collection('likes').where('userHandle', '==', req.user.handle)
|
||||
// .where('postId', '==', req.params.postId).limit(1);
|
||||
|
||||
// const postDoc = db.doc(`/posts/${req.params.postId}`);
|
||||
|
||||
// postDoc.get()
|
||||
// .then((doc) => {
|
||||
// if(doc.exists) {
|
||||
// postData = doc.data();
|
||||
// return likeDoc.get();
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// return res.status(404).json({error: 'Post not found'});
|
||||
// }
|
||||
// })
|
||||
// .then((data) => {
|
||||
// return db
|
||||
// .doc(`/likes/${data.docs[0].id}`)
|
||||
// .delete()
|
||||
// .then(() => {
|
||||
// postData.likeCount--;
|
||||
// return postDoc.update({ likeCount: postData.likeCount });
|
||||
// })
|
||||
// .then(() => {
|
||||
// res.status(200).json(postData);
|
||||
// });
|
||||
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(err);
|
||||
return res.status(500).json({error: 'Something is wrong'});
|
||||
})
|
||||
// })
|
||||
// .catch((err) => {
|
||||
// console.error(err);
|
||||
// return res.status(500).json({error: 'Something is wrong'});
|
||||
// })
|
||||
|
||||
}
|
||||
|
||||
exports.getLikes = (req, res) => {
|
||||
db.doc(`/users/${req.userData.handle}`)
|
||||
.get()
|
||||
.then((doc) => {
|
||||
let likes = doc.data().likes;
|
||||
if (likes === undefined || likes === null) {
|
||||
likes = [];
|
||||
}
|
||||
return res.status(200).json({likes});
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
return res.status(500).json({error: err});
|
||||
})
|
||||
}
|
||||
|
||||
exports.getFilteredPosts = (req, res) => {
|
||||
admin.firestore().collection('posts').where('userHandle', '==', 'new user').where('microBlogTopics', '==')
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user