tuned some things and added some to user route
This commit is contained in:
+146
-1
@@ -1,7 +1,26 @@
|
||||
const db = require('../surreal');
|
||||
const router = require('express').Router();
|
||||
const {auth} = require('../auth/middleware');
|
||||
|
||||
router.post('/like/:postId', async (req, res) => {
|
||||
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 * FROM posts WHERE id = "${id}"`);
|
||||
|
||||
if(post.length == 0) return res.status(400).json({error: "Post does not exist"});
|
||||
|
||||
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;
|
||||
|
||||
@@ -26,4 +45,130 @@ router.post('/like/:postId', async (req, res) => {
|
||||
res.status(200).json({message: "Post liked"});
|
||||
});
|
||||
|
||||
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 posts 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 posts 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}"`);
|
||||
|
||||
//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}")`);
|
||||
|
||||
return res.status(200).json({message: "Post unsaved"});
|
||||
}
|
||||
}
|
||||
|
||||
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}")`);
|
||||
|
||||
res.status(200).json({message: "Post saved"});
|
||||
});
|
||||
|
||||
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 posts 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 posts 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;
|
||||
Reference in New Issue
Block a user