mirror of
https://github.com/ClaytonWWilson/CS307-Team24.git
synced 2026-03-10 21:25:04 +00:00
liking and quoting work
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
/* eslint-disable promise/always-return */
|
||||
const admin = require('firebase-admin');
|
||||
const { db } = require('../util/admin');
|
||||
|
||||
exports.putPost = (req, res) => {
|
||||
|
||||
const newPost = {
|
||||
@@ -27,6 +29,106 @@ exports.putPost = (req, res) => {
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
exports.getPost = (req, res) => {
|
||||
let postData = {};
|
||||
db.doc(`/posts/${req.params.postId}`)
|
||||
.get()
|
||||
.then((doc) => {
|
||||
if (!doc.exists) {
|
||||
return res.status(404).json({error: 'Post is not found'});
|
||||
|
||||
}
|
||||
postData = doc.data();
|
||||
postData.postId = doc.id;
|
||||
return res.status(200).json(postData);
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(err);
|
||||
return res.status(500).json({error: 'Something is wrong'});
|
||||
})
|
||||
}
|
||||
|
||||
exports.likePost = (req, res) => {
|
||||
let postData;
|
||||
const likeDoc = admin.firestore().collection('likes').where('userHandle', '==', req.userData.handle)
|
||||
.where('postId', '==', req.params.postId).limit(1);
|
||||
|
||||
const postDoc = db.doc(`/posts/${req.params.postId}`);
|
||||
|
||||
likeDoc.get()
|
||||
.then((data) => {
|
||||
if (data.empty) {
|
||||
return admin.firestore().collection('likes').add({
|
||||
postId : req.params.postId,
|
||||
userHandle: req.userData.handle
|
||||
})
|
||||
.then(() => {
|
||||
postData.likeCount++;
|
||||
return postDoc.update({likeCount : postData.likeCount})
|
||||
})
|
||||
.then(() => {
|
||||
return res.status(200).json(postData);
|
||||
})
|
||||
}
|
||||
else {
|
||||
return res.status(400).json({error: 'Post has already been liked.'})
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(err);
|
||||
return res.status(500).json({error: 'Something is wrong'});
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
exports.unlikePost = (re, res) => {
|
||||
|
||||
let postData;
|
||||
const likeDoc = admin.firestore().collection('likes').where('userHandle', '==', req.userData.handle)
|
||||
.where('postId', '==', req.params.postId).limit(1);
|
||||
|
||||
const postDoc = db.doc(`/posts/${req.params.postId}`);
|
||||
|
||||
likeDoc.get()
|
||||
.then((data) => {
|
||||
if (data.empty) {
|
||||
return res.status(400).json({ error: 'Post cannot be unliked because it is not liked at the moment.' });
|
||||
} else {
|
||||
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'});
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
exports.getallPostsforFeed = (req, res) => {
|
||||
admin.firestore().collection('posts').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 all other users.'})
|
||||
})
|
||||
};
|
||||
|
||||
exports.getallPostsforUser = (req, res) => {
|
||||
admin.firestore().collection('posts').where('userHandle', '==', req.userData.handle ).get()
|
||||
.then((data) => {
|
||||
|
||||
Reference in New Issue
Block a user