some tweaking here and there

This commit is contained in:
2024-03-11 18:12:43 +01:00
parent 411cc4b8fe
commit 7d2acdec27
6 changed files with 107 additions and 11 deletions
+21 -6
View File
@@ -8,11 +8,20 @@ router.get('/:id', async (req, res) => {
if(!id) return res.status(400).json({error: "Missing id"});
const post = await db.query(`SELECT * FROM posts WHERE id = "${id}"`);
const post = await db.query(`SELECT author, category, comments, content, date, count(likes) as likes, count(saves) as saves, title, viewcount FROM posts WHERE id = "${id}"`);
if(post.length == 0) return res.status(400).json({error: "Post does not exist"});
else {
const viewPost = await db.query(`UPDATE posts SET viewcount += 1 WHERE id = "${postId}"`);
///select all comments from their ids
let temp = [];
for(const commentId of post[0].comments){
const comment = await db.query(`SELECT * FROM comments WHERE id = "${commentId}"`);
temp.push(comment[0]);
}
//remove all comments from post.comments and replace it with temp
post[0].comments = temp;
// const viewPost = await db.query(`UPDATE posts SET viewcount += .5 WHERE id = "${id}"`); //nextjs sends each request twice, so this is a workaround needs fix
}
res.status(200).json({post});
@@ -31,21 +40,24 @@ router.post('/like/:postId',auth, async (req, res) => {
if(!postId) return res.status(400).json({error: "Missing postId"});
const postData = await db.query(`SELECT * FROM posts WHERE id = "${postId}"`);
let totalLikes = postData[0].likes.length;
//check if user already liked post
for(let i = 0; i < postData[0].likes.length; i++){
if(postData[0].likes[i] === username){
const post = await db.query(`UPDATE posts SET likes -= "${username}" WHERE id = "${postId}"`);
const user = await db.query(`UPDATE users SET likedPosts -= "${req.params.postId}" WHERE username = string::lowercase("${username}")`);
totalLikes -= 1;
return res.status(200).json({message: "Post unliked"});
return res.status(200).json({likes: totalLikes});
}
}
const post = await db.query(`UPDATE posts SET likes += "${username}" WHERE id = "${postId}"`);
const user = await db.query(`UPDATE users SET likedPosts += "${req.params.postId}" WHERE username = string::lowercase("${username}")`);
totalLikes += 1;
res.status(200).json({message: "Post liked"});
return res.status(200).json({likes: totalLikes});
});
router.post('/comment/:postId',auth, async (req, res) => {
@@ -130,21 +142,24 @@ router.post('/save/:postId',auth, async (req, res) => {
if(!postId) return res.status(400).json({error: "Missing postId"});
const postData = await db.query(`SELECT * FROM posts WHERE id = "${postId}"`);
let totalSaves = postData[0].saves.length;
//check if user already saved post
for(let i = 0; i < postData[0].saves.length; i++){
if(postData[0].saves[i] === username){
const post = await db.query(`UPDATE posts SET saves -= "${username}" WHERE id = "${postId}"`);
const user = await db.query(`UPDATE users SET savedPosts -= "${req.params.postId}" WHERE username = string::lowercase("${username}")`);
totalSaves -= 1;
return res.status(200).json({message: "Post unsaved"});
return res.status(200).json({saves: totalSaves});
}
}
const post = await db.query(`UPDATE posts SET saves += "${username}" WHERE id = "${postId}"`);
const user = await db.query(`UPDATE users SET savedPosts += "${req.params.postId}" WHERE username = string::lowercase("${username}")`);
totalSaves += 1;
res.status(200).json({message: "Post saved"});
res.status(200).json({saves: totalSaves});
});
router.post('/save/:postId/:commentId',auth, async (req, res) => {