tweaking and adding homepage with trending and so on
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
const db = require('../surreal');
|
||||
const router = require('express').Router();
|
||||
|
||||
router.post('/:search', async (req, res) => {
|
||||
try{
|
||||
const {type, category} = req.body;
|
||||
const search = req.params.search;
|
||||
|
||||
if (type === 'user') {
|
||||
const users = await db.query(`SELECT username, array::len(posts) AS posts_len, search::score(1) AS score FROM users WHERE username @1@ '${search}' ORDER BY score DESC;`);
|
||||
res.status(200).json(users);
|
||||
}
|
||||
|
||||
else if (type === 'post') {
|
||||
let query = `SELECT category, title, content, author, date, array::len(likes) AS likes_len, array::len(comments) AS comments_len, array::len(saves) AS saves_len, search::score(1) AS score FROM posts WHERE title @1@ '${search}')`;
|
||||
if (category) {
|
||||
//check if category is json array
|
||||
if(category instanceof Array) {
|
||||
query += ' AND (';
|
||||
for(let i = 0; i < category.length; i++) {
|
||||
if(i != 0) query += ' OR ';
|
||||
query += `category = '${category[i]}'`;
|
||||
}
|
||||
query += ')';
|
||||
}
|
||||
else query += ` AND category = '${category}'`;
|
||||
}
|
||||
console.log(query)
|
||||
query += ' ORDER BY score DESC;';
|
||||
const posts = await db.query(query);
|
||||
res.status(200).json(posts);
|
||||
}
|
||||
|
||||
else {
|
||||
res.status(400).json({error: 'Invalid type'});
|
||||
}
|
||||
}
|
||||
catch(err){
|
||||
res.status(500).json({error: err});
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user