192 lines
8.1 KiB
JavaScript
192 lines
8.1 KiB
JavaScript
const db = require('../surreal');
|
|
const router = require('express').Router();
|
|
const {auth} = require('../auth/middleware');
|
|
|
|
router.get('/:id', async (req, res) => {
|
|
try{
|
|
const id = 'posts:' + req.params.id;
|
|
|
|
if(!id) return res.status(400).json({error: "Missing 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 {
|
|
///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});
|
|
}
|
|
catch(err){
|
|
console.log(err);
|
|
res.status(500).json({error: "Internal server error"});
|
|
}
|
|
});
|
|
|
|
router.post('/like/:postId',auth, async (req, res) => {
|
|
const username = req.user;
|
|
const postId = 'posts:' + req.params.postId;
|
|
|
|
if(!username) return res.status(400).json({error: "Missing username"});
|
|
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({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;
|
|
|
|
return res.status(200).json({likes: totalLikes});
|
|
});
|
|
|
|
router.post('/comment/:postId',auth, async (req, res) => {
|
|
const username = req.user;
|
|
const postId = 'posts:' + req.params.postId;
|
|
const comment = req.body.comment;
|
|
|
|
if(!username) return res.status(400).json({error: "Missing username"});
|
|
if(!postId) return res.status(400).json({error: "Missing postId"});
|
|
if(!comment) return res.status(400).json({error: "Missing comment"});
|
|
|
|
const Comment = {
|
|
author: username,
|
|
comment: comment
|
|
}
|
|
|
|
const commentDB = await db.create('comments', Comment);
|
|
|
|
const post = await db.query(`UPDATE posts SET comments += "${commentDB[0].id}" WHERE id = "${postId}"`);
|
|
const user = await db.query(`UPDATE users SET comments += "${commentDB[0].id}" WHERE username = string::lowercase("${username}")`);
|
|
|
|
res.status(200).json({message: "Comment added"});
|
|
});
|
|
|
|
router.post('/comment/:postId/:commentId',auth, async (req, res) => {
|
|
const username = req.user;
|
|
const postId = 'posts:' + req.params.postId;
|
|
const commentId = 'comments:' + req.params.commentId;
|
|
const comment = req.body.comment;
|
|
|
|
if(!username) return res.status(400).json({error: "Missing username"});
|
|
if(!postId) return res.status(400).json({error: "Missing postId"});
|
|
if(!commentId) return res.status(400).json({error: "Missing commentId"});
|
|
if(!comment) return res.status(400).json({error: "Missing comment"});
|
|
|
|
const Comment = {
|
|
author: username,
|
|
comment: comment,
|
|
parent: commentId
|
|
}
|
|
|
|
const commentDB = await db.create('comments', Comment);
|
|
|
|
const post = await db.query(`UPDATE posts SET comments += "${commentDB[0].id}" WHERE id = "${postId}"`);
|
|
const user = await db.query(`UPDATE users SET comments += "${commentDB[0].id}" WHERE username = string::lowercase("${username}")`);
|
|
|
|
res.status(200).json({message: "Comment added"});
|
|
});
|
|
|
|
router.post('/like/:postId/:commentId',auth, async (req, res) => {
|
|
const username = req.user;
|
|
const postId = 'posts:' + req.params.postId;
|
|
const commentId = 'comments:' + req.params.commentId;
|
|
|
|
if(!username) return res.status(400).json({error: "Missing username"});
|
|
if(!postId) return res.status(400).json({error: "Missing postId"});
|
|
if(!commentId) return res.status(400).json({error: "Missing commentId"});
|
|
|
|
const postData = await db.query(`SELECT * FROM posts WHERE id = "${postId}"`);
|
|
|
|
//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 comments SET likes -= "${username}" WHERE id = "${postId}"`);
|
|
const user = await db.query(`UPDATE users SET likedPosts -= "${req.params.postId}" WHERE username = string::lowercase("${username}")`);
|
|
|
|
return res.status(200).json({message: "Post unliked"});
|
|
}
|
|
}
|
|
|
|
const post = await db.query(`UPDATE comments SET likes += "${username}" WHERE id = "${postId}"`);
|
|
const user = await db.query(`UPDATE users SET likedPosts += "${req.params.postId}" WHERE username = string::lowercase("${username}")`);
|
|
|
|
res.status(200).json({message: "Post liked"});
|
|
});
|
|
|
|
router.post('/save/:postId',auth, async (req, res) => {
|
|
const username = req.user;
|
|
const postId = 'posts:' + req.params.postId;
|
|
|
|
if(!username) return res.status(400).json({error: "Missing username"});
|
|
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({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({saves: totalSaves});
|
|
});
|
|
|
|
router.post('/save/:postId/:commentId',auth, async (req, res) => {
|
|
const username = req.user;
|
|
const postId = 'posts:' + req.params.postId;
|
|
const commentId = 'comments:' + req.params.commentId;
|
|
|
|
if(!username) return res.status(400).json({error: "Missing username"});
|
|
if(!postId) return res.status(400).json({error: "Missing postId"});
|
|
if(!commentId) return res.status(400).json({error: "Missing commentId"});
|
|
|
|
const postData = await db.query(`SELECT * FROM posts WHERE id = "${postId}"`);
|
|
|
|
//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 comments SET saves -= "${username}" WHERE id = "${postId}"`);
|
|
const user = await db.query(`UPDATE users SET savedPosts -= "${req.params.postId}" WHERE username = string::lowercase("${username}")`);
|
|
|
|
return res.status(200).json({message: "Post unsaved"});
|
|
}
|
|
}
|
|
|
|
const post = await db.query(`UPDATE comments SET saves += "${username}" WHERE id = "${postId}"`);
|
|
const user = await db.query(`UPDATE users SET savedPosts += "${req.params.postId}" WHERE username = string::lowercase("${username}")`);
|
|
|
|
res.status(200).json({message: "Post saved"});
|
|
});
|
|
|
|
module.exports = router; |