Merge branch 'master' into edit-profile-info

This commit is contained in:
Clayton Wilson
2019-09-29 21:33:41 -04:00
committed by GitHub
36 changed files with 4770 additions and 1947 deletions

View File

@@ -0,0 +1,27 @@
/* eslint-disable promise/always-return */
exports.putPost = (req, res) => {
if (req.body.body.trim() === '') {
return res.status(400).json({ body: 'Body must not be empty!'});
}
const newPost = {
body: req.body.body,
userHandle: req.user.handle,
userImage: req.user.imageUrl,
createdAt: new Date().toISOString(),
likeCount: 0,
commentCount: 0
};
db.collection('post').add(newPost)
.then((doc) => {
const resPost = newPost;
resPost.postId = doc.id;
res.json(resPost);
})
.catch((err) => {
res.status(500).json({ error: 'something is wrong'});
console.error(err);
});
};

View File

@@ -37,4 +37,37 @@ exports.updateProfileInfo = (req, res) => {
error: 'Error updating profile data'
});
})
exports.getUserDetails = (req, res) => {
let userData = {};
db.doc('/users/${req.params.handle}').get().then((doc) => {
if (doc.exists) {
userData.user = doc.data();
return db.collection('post').where('userHandle', '==', req.params.handle)
.orderBy('createdAt', 'desc').get();
} else {
return res.status(404).json({
error: 'User not found'
});
}
})
.then((data) => {
userData.posts = [];
data.forEach((doc) => {
userData.posts.push({
body: doc.data().body,
createAt: doc.data().createAt,
userHandle: doc.data().userHandle,
userImage: doc.data().userImage,
likeCount: doc.data().likeCount,
commentCount: doc.data().commentCount,
postId: doc.id
});
});
return res.json(userData);
})
.catch((err) => {
console.error(err);
return res.status(500).json({ error: err.code});
});
};