added alert to user's page

This commit is contained in:
Leon Liang 2019-12-05 13:45:58 -05:00
parent 2bcf6bfcb3
commit de72bd9223
3 changed files with 280 additions and 220 deletions

View File

@ -66,46 +66,77 @@ exports.getallPosts = (req, res) => {
// Get all the posts // Get all the posts
var postsPromise = new Promise((resolve, reject) => { var postsPromise = new Promise((resolve, reject) => {
db.collection("posts").get() db.collection("posts")
.then((allPosts) => { .get()
allPosts.forEach((post) => { .then(allPosts => {
allPosts.forEach(post => {
posts.push(post.data()); posts.push(post.data());
}); });
resolve(); resolve();
}) })
.catch((error) => { .catch(error => {
reject(error); reject(error);
}) });
}); });
// Get all users // Get all users
var usersPromise = new Promise((resolve, reject) => { var usersPromise = new Promise((resolve, reject) => {
db.collection("users").get() db.collection("users")
.then((allUsers) => { .get()
allUsers.forEach((user) => { .then(allUsers => {
allUsers.forEach(user => {
users[user.data().handle] = user.data(); users[user.data().handle] = user.data();
}) });
resolve(); resolve();
}) })
.catch((error) => { .catch(error => {
reject(error); reject(error);
}) });
}); });
// Wait for the two promises // Wait for the two promises
Promise.all([postsPromise, usersPromise]) Promise.all([postsPromise, usersPromise])
.then(() => { .then(() => {
let newPosts = [] let newPosts = [];
// Add the image url of the person who made the post to all of the post objects // Add the image url of the person who made the post to all of the post objects
posts.forEach((post) => { posts.forEach(post => {
post.profileImage = users[post.userHandle].imageUrl ? users[post.userHandle].imageUrl : null; post.profileImage = users[post.userHandle].imageUrl
? users[post.userHandle].imageUrl
: null;
newPosts.push(post); newPosts.push(post);
}); });
return res.status(200).json(newPosts); return res.status(200).json(newPosts);
}) })
.catch((error) => { .catch(error => {
return res.status(500).json({error}); return res.status(500).json({ error });
});
};
exports.getAlert = (req, res) => {
var post_query = admin
.firestore()
.collection("posts")
.where("microBlogTitle", "==", "Alert");
post_query
.get()
.then(function(myPosts) {
let posts = [];
myPosts.forEach(function(doc) {
posts.push(doc.data());
});
return res.status(200).json(posts);
}) })
.then(function() {
return res
.status(200)
.json("Successfully retrieved all user's posts from database.");
})
.catch(function(err) {
return res
.status(500)
.json("Failed to retrieve user's posts from database.", err);
});
}; };
exports.getOtherUsersPosts = (req, res) => { exports.getOtherUsersPosts = (req, res) => {
@ -137,21 +168,23 @@ exports.getOtherUsersPosts = (req, res) => {
exports.quoteWithPost = (req, res) => { exports.quoteWithPost = (req, res) => {
let quoteData; let quoteData;
const quoteDoc = admin.firestore().collection('quote'). const quoteDoc = admin
where('userHandle', '==', req.user.handle). .firestore()
where('quoteId', '==', req.params.postId).limit(1); .collection("quote")
.where("userHandle", "==", req.user.handle)
.where("quoteId", "==", req.params.postId)
.limit(1);
const postDoc = db.doc(`/posts/${req.params.postId}`); const postDoc = db.doc(`/posts/${req.params.postId}`);
postDoc.get() postDoc
.then((doc) => { .get()
if(doc.exists) { .then(doc => {
if (doc.exists) {
quoteData = doc.data(); quoteData = doc.data();
return quoteDoc.get(); return quoteDoc.get();
} } else {
else return res.status(404).json({ error: "Post not found" });
{
return res.status(404).json({error: 'Post not found'});
} }
}) })
.then(data => { .then(data => {
@ -201,21 +234,23 @@ exports.quoteWithPost = (req, res) => {
exports.quoteWithoutPost = (req, res) => { exports.quoteWithoutPost = (req, res) => {
let quoteData; let quoteData;
const quoteDoc = admin.firestore().collection('quote'). const quoteDoc = admin
where('userHandle', '==', req.user.handle). .firestore()
where('quoteId', '==', req.params.postId).limit(1); .collection("quote")
.where("userHandle", "==", req.user.handle)
.where("quoteId", "==", req.params.postId)
.limit(1);
const postDoc = db.doc(`/posts/${req.params.postId}`); const postDoc = db.doc(`/posts/${req.params.postId}`);
postDoc.get() postDoc
.then((doc) => { .get()
if(doc.exists) { .then(doc => {
if (doc.exists) {
quoteData = doc.data(); quoteData = doc.data();
return quoteDoc.get(); return quoteDoc.get();
} } else {
else return res.status(404).json({ error: "Post not found" });
{
return res.status(404).json({error: 'Post not found'});
} }
}) })
.then(data => { .then(data => {
@ -272,7 +307,9 @@ exports.checkforLikePost = (req, res) => {
.limit(1); .limit(1);
let result; let result;
likedPostDoc.get().then(data => { likedPostDoc
.get()
.then(data => {
if (data.empty) { if (data.empty) {
result = false; result = false;
return res.status(200).json(result); return res.status(200).json(result);
@ -281,49 +318,49 @@ exports.checkforLikePost = (req, res) => {
return res.status(200).json(result); return res.status(200).json(result);
} }
}) })
.catch((err) => { .catch(err => {
console.log(err); console.log(err);
return res.status(500).json({error: err}); return res.status(500).json({ error: err });
}) });
}; };
exports.likePost = (req, res) => { exports.likePost = (req, res) => {
const postId = req.params.postId; const postId = req.params.postId;
let likedPostDoc; let likedPostDoc;
db.doc(`/users/${req.userData.handle}`) db.doc(`/users/${req.userData.handle}`)
.get() .get()
.then((userDoc) => { .then(userDoc => {
let likes = userDoc.data().likes; let likes = userDoc.data().likes;
if (likes === undefined || likes === null) { if (likes === undefined || likes === null) {
likes = []; likes = [];
} }
if (likes.includes(postId)) { if (likes.includes(postId)) {
return res.status(400).json({error: "This user has already liked this post"}); return res
.status(400)
.json({ error: "This user has already liked this post" });
} }
likes.push(postId); likes.push(postId);
return userDoc.ref.update({likes}) return userDoc.ref.update({ likes });
}) })
.then(() => { .then(() => {
return db.doc(`/posts/${postId}`).get() return db.doc(`/posts/${postId}`).get();
}) })
.then((postDoc) => { .then(postDoc => {
let postData = postDoc.data(); let postData = postDoc.data();
postData.likeCount++; postData.likeCount++;
likedPostDoc = postData; likedPostDoc = postData;
return postDoc.ref.update({likeCount : postData.likeCount}) return postDoc.ref.update({ likeCount: postData.likeCount });
}) })
.then(() => { .then(() => {
return res.status(201).json(likedPostDoc); return res.status(201).json(likedPostDoc);
}) })
.catch((err) => { .catch(err => {
console.log(err); console.log(err);
return res.status(500).json({error: err}); return res.status(500).json({ error: err });
}) });
// let postData; // let postData;
// const likeDoc = admin.firestore().collection('likes').where('userHandle', '==', req.user.handle) // const likeDoc = admin.firestore().collection('likes').where('userHandle', '==', req.user.handle)
@ -361,24 +398,23 @@ exports.likePost = (req, res) => {
// .catch((err) => { // .catch((err) => {
// return res.status(500).json({error: 'Something is wrong'}); // return res.status(500).json({error: 'Something is wrong'});
// }) // })
};
}
exports.unlikePost = (req, res) => { exports.unlikePost = (req, res) => {
const postId = req.params.postId; const postId = req.params.postId;
let likedPostDoc; let likedPostDoc;
db.doc(`/users/${req.userData.handle}`) db.doc(`/users/${req.userData.handle}`)
.get() .get()
.then((userDoc) => { .then(userDoc => {
let likes = userDoc.data().likes; let likes = userDoc.data().likes;
if (likes === undefined || likes === null) { if (likes === undefined || likes === null) {
likes = []; likes = [];
} }
if (!likes.includes(postId)) { if (!likes.includes(postId)) {
return res.status(400).json({error: "This user hasn't liked this post yet"}); return res
.status(400)
.json({ error: "This user hasn't liked this post yet" });
} }
let i; let i;
@ -388,25 +424,24 @@ exports.unlikePost = (req, res) => {
} }
} }
return userDoc.ref.update({likes}) return userDoc.ref.update({ likes });
}) })
.then(() => { .then(() => {
return db.doc(`/posts/${postId}`).get() return db.doc(`/posts/${postId}`).get();
}) })
.then((postDoc) => { .then(postDoc => {
let postData = postDoc.data(); let postData = postDoc.data();
postData.likeCount--; postData.likeCount--;
likedPostDoc = postData; likedPostDoc = postData;
return postDoc.ref.update({likeCount : postData.likeCount}) return postDoc.ref.update({ likeCount: postData.likeCount });
}) })
.then(() => { .then(() => {
return res.status(201).json(likedPostDoc); return res.status(201).json(likedPostDoc);
}) })
.catch((err) => { .catch(err => {
console.log(err); console.log(err);
return res.status(500).json({error: err}); return res.status(500).json({ error: err });
}) });
// let postData; // let postData;
// const likeDoc = admin.firestore().collection('likes').where('userHandle', '==', req.user.handle) // const likeDoc = admin.firestore().collection('likes').where('userHandle', '==', req.user.handle)
@ -442,32 +477,28 @@ exports.unlikePost = (req, res) => {
// console.error(err); // console.error(err);
// return res.status(500).json({error: 'Something is wrong'}); // return res.status(500).json({error: 'Something is wrong'});
// }) // })
};
}
exports.getLikes = (req, res) => { exports.getLikes = (req, res) => {
db.doc(`/users/${req.userData.handle}`) db.doc(`/users/${req.userData.handle}`)
.get() .get()
.then((doc) => { .then(doc => {
let likes = doc.data().likes; let likes = doc.data().likes;
if (likes === undefined || likes === null) { if (likes === undefined || likes === null) {
likes = []; likes = [];
} }
return res.status(200).json({likes}); return res.status(200).json({ likes });
}) })
.catch((err) => { .catch(err => {
console.log(err); console.log(err);
return res.status(500).json({error: err}); return res.status(500).json({ error: err });
}) });
} };
exports.getFilteredPosts = (req, res) => { exports.getFilteredPosts = (req, res) => {
admin admin
.firestore() .firestore()
.collection("posts") .collection("posts")
.where("userHandle", "==", "new user") .where("userHandle", "==", "new user")
.where("microBlogTopics", "=="); .where("microBlogTopics", "==");
}; };

View File

@ -100,13 +100,23 @@ app.post("/addSubscription", fbAuth, addSubscription);
// remove one subscription // remove one subscription
app.post("/removeSub", fbAuth, removeSub); app.post("/removeSub", fbAuth, removeSub);
/*------------------------------------------------------------------* /*------------------------------------------------------------------*
* handlers/post.js * * handlers/post.js *
*------------------------------------------------------------------*/ *------------------------------------------------------------------*/
const { getallPostsforUser, getallPosts, putPost, likePost, unlikePost, getLikes, quoteWithPost, quoteWithoutPost, checkforLikePost, getOtherUsersPosts} = require("./handlers/post"); const {
getallPostsforUser,
getallPosts,
putPost,
likePost,
unlikePost,
getLikes,
quoteWithPost,
quoteWithoutPost,
checkforLikePost,
getOtherUsersPosts,
getAlert
} = require("./handlers/post");
app.get("/getallPostsforUser", fbAuth, getallPostsforUser); app.get("/getallPostsforUser", fbAuth, getallPostsforUser);
@ -125,6 +135,8 @@ app.post("/quoteWithoutPost/:postId", fbAuth, quoteWithoutPost);
app.post("/getOtherUsersPosts", fbAuth, getOtherUsersPosts); app.post("/getOtherUsersPosts", fbAuth, getOtherUsersPosts);
app.get("/getAlert", fbAuth, getAlert);
/*------------------------------------------------------------------* /*------------------------------------------------------------------*
* handlers/topic.js * * handlers/topic.js *
*------------------------------------------------------------------*/ *------------------------------------------------------------------*/

View File

@ -149,6 +149,23 @@ class user extends Component {
}); });
}) })
.catch(err => console.log(err)); .catch(err => console.log(err));
axios
.get("/getAlert")
.then(res => {
let temp = this.state.posts;
console.log(res.data);
res.data.forEach(element => {
element ? temp.push(element) : console.err;
});
// temp.push(res.data[0]);
this.setState({
posts: temp
});
})
.catch(function(err) {
console.log(err);
});
} }
render() { render() {
@ -211,7 +228,7 @@ class user extends Component {
) : ( ) : (
<img src={noImage} height="150" width="150" /> <img src={noImage} height="150" width="150" />
); );
console.log(this.state.posts);
let postMarkup = this.state.posts ? ( let postMarkup = this.state.posts ? (
this.state.posts.map(post => ( this.state.posts.map(post => (
<Card className={classes.card}> <Card className={classes.card}>