This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.post('/posts', upload.single('post_image'), (req, res) => { | |
const file = req.file; | |
if (!file) { | |
res.status(200).jsonp({ | |
message: "Please upload your post image", | |
}); | |
} else { | |
const postContent = `/${file.filename}`; | |
const postCategory = req.file && req.file.mimetype.includes('image') ? 1 : 2; | |
const postCreatedDate = new Date(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.get('/posts', (req, res) => { | |
const getPostsSql = "SELECT * FROM post ORDER BY post_created_date DESC"; | |
dbConn.query(getPostsSql, function (error, posts) { | |
if (posts) { | |
res.status(200).jsonp(posts); | |
} else { | |
res.status(200).jsonp({ message: 'Cannot get your posts, please try again' }); | |
} | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.get('/posts/:id', (req, res) => { | |
const id = req.params.id; | |
const getPostSql = "SELECT post.id, post_content, post_category, post_created_date, post_created_by, post_number_of_reactions, user_account.user_avatar, user_account.user_full_name, user_account.user_number_of_followers FROM post INNER JOIN user_account ON post.post_created_by = user_account.id WHERE post.id = ?"; | |
if (!id) { | |
res.status(200).jsonp({ message: 'Cannot load the post detail, please try again' }); | |
} | |
dbConn.query(getPostSql, [id], function (error, response) { | |
if (response && response.length) { | |
res.status(200).jsonp(response); | |
} else { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.post('/posts/categories', (req, res) => { | |
const { userId, postCategory } = req.body; | |
if (!userId || !postCategory) { | |
res.status(200).jsonp({ message: 'Cannot load your posts, please try again' }); | |
} | |
const getPostsSql = "SELECT * FROM post WHERE post_created_by = ? AND post_category = ? ORDER BY post_created_date DESC"; | |
dbConn.query(getPostsSql, [userId, postCategory], function (error, posts) { | |
if (posts) { | |
res.status(200).jsonp(posts); | |
} else { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.post('/posts/reactions', (req, res) => { | |
const { numberOfReactions, id } = req.body; | |
const updateNumberOfReactionsSql = "UPDATE post SET post_number_of_reactions = ? WHERE id = ?"; | |
dbConn.query(updateNumberOfReactionsSql, [numberOfReactions, id], function (err, updatedPost) { | |
if (err) { | |
res.status(200).jsonp({ message: "The system error. Please try again" }); | |
} else if (updatedPost) { | |
res.status(200).jsonp({ id }); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.post('/followers/get', (req, res) => { | |
const { followerId, userId } = req.body; | |
if (!followerId || !userId) { | |
res.status(200).jsonp({ message: 'Not found' }); | |
} | |
const getFollowerSql = "SELECT * FROM user_follower WHERE follower_id = ? AND user_id = ?"; | |
dbConn.query(getFollowerSql, [followerId, userId], function (error, response) { | |
if (response && response.length) { | |
res.status(200).jsonp({ ...response[0] }); | |
} else { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.post('/followers/create', (req, res) => { | |
const { followerId, userId } = req.body; | |
if (!followerId || !userId) { | |
res.status(200).jsonp({ message: 'Cannot create the follower, please try again' }); | |
} | |
const followers = [[followerId, userId]]; | |
const insertFollowerSql = "INSERT INTO user_follower (follower_id, user_id) VALUES ?"; | |
dbConn.query(insertFollowerSql, [followers], function (error, insertedFollower) { | |
if (insertedFollower) { | |
res.status(200).jsonp({ insertId: insertedFollower.insertId, follower_id: followerId, user_id: userId }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.post('/followers/delete', (req, res) => { | |
const { followerId, userId } = req.body; | |
if (!followerId || !userId) { | |
res.status(200).jsonp({ message: 'Cannot delete the follower, please try again' }); | |
} | |
const deleteFollowerSql = "DELETE FROM user_follower WHERE follower_id = ? AND user_id = ?"; | |
dbConn.query(deleteFollowerSql, [followerId, userId], function (error, response) { | |
if (response && response.affectedRows) { | |
res.status(200).jsonp({ followerId, userId }); | |
} else { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.post('/reactions/get', (req, res) => { | |
const { post_id, user_id } = req.body; | |
if (!post_id || !user_id) { | |
res.status(200).jsonp({ message: 'Not found' }); | |
} | |
const getReactionSql = "SELECT * FROM post_reaction WHERE post_id = ? AND user_id = ?"; | |
dbConn.query(getReactionSql, [post_id, user_id], function (error, response) { | |
if (response && response.length) { | |
res.status(200).jsonp({ ...response[0] }); | |
} else { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.post('/reactions/create', (req, res) => { | |
const { postId, userId } = req.body; | |
if (!postId || !userId) { | |
res.status(200).jsonp({ message: 'Cannot create the post reaction, please try again' }); | |
} | |
const reactions = [[postId, userId]]; | |
const insertReactionSql = "INSERT INTO post_reaction (post_id, user_id) VALUES ?"; | |
dbConn.query(insertReactionSql, [reactions], function (error, insertedReaction) { | |
if (insertedReaction) { | |
res.status(200).jsonp({ insertId: insertedReaction.insertId, post_id: postId, user_id: userId }); |