some tweaking here and there
This commit is contained in:
@@ -91,4 +91,62 @@ router.get('/mostSaved', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/categories', async (req, res) => {
|
||||
try{
|
||||
const categories = await db.query(`SELECT name, description, count(posts) AS count FROM category`);
|
||||
|
||||
if(categories.length == 0) return res.status(400).json({error: "Categories do not exist"});
|
||||
|
||||
res.status(200).json({categories});
|
||||
}
|
||||
catch(err){
|
||||
console.log(err);
|
||||
res.status(500).json({error: "Internal server error"});
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/category/:name/', async (req, res) => {
|
||||
try{
|
||||
const name = req.params.name;
|
||||
|
||||
const category = await db.query(`SELECT posts FROM category WHERE name = string::lowercase("${name}")`);
|
||||
//reverse the array so the latest posts are first
|
||||
category[0].posts.reverse();
|
||||
|
||||
if(category.length == 0) return res.status(400).json({error: "Category does not exist"});
|
||||
|
||||
postarray = []
|
||||
|
||||
//select woth the id
|
||||
for(const id of category[0].posts){
|
||||
const post = await db.query(`SELECT title, date, viewcount, id, author FROM posts WHERE id = "posts:${id}"`);
|
||||
//remopve posts: from the id
|
||||
post[0].id = post[0].id.split(":")[1];
|
||||
postarray.push(post[0]);
|
||||
}
|
||||
|
||||
res.status(200).json({postarray});
|
||||
}
|
||||
catch(err){
|
||||
console.log(err);
|
||||
res.status(500).json({error: "Internal server error"});
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/category/details/:name/', async (req, res) => {
|
||||
try{
|
||||
const name = req.params.name;
|
||||
|
||||
const category = await db.query(`SELECT description FROM category WHERE name = string::lowercase("${name}")`);
|
||||
|
||||
if(category.length == 0) return res.status(400).json({error: "Category does not exist"});
|
||||
|
||||
res.status(200).json({description: category[0].description});
|
||||
}
|
||||
catch(err){
|
||||
console.log(err);
|
||||
res.status(500).json({error: "Internal server error"});
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user