Added basic search functionality (no frontend for it yet)
This commit is contained in:
42
src/data.js
42
src/data.js
@@ -5,6 +5,44 @@ const func = require('./functions.js')
|
|||||||
const config = require("../config.json")
|
const config = require("../config.json")
|
||||||
const fs = require("fs")
|
const fs = require("fs")
|
||||||
|
|
||||||
|
export function searchdata(term, type) { // Searches users and posts for any matches
|
||||||
|
let search_results = {"posts": [], "users": []};
|
||||||
|
// Search users
|
||||||
|
if (type == 'post' || type == 'any') {
|
||||||
|
let list = getdata('posts');
|
||||||
|
list.forEach((element,index) => {
|
||||||
|
if (typeof element.deleted == 'undefined' || element.deleted == false) {
|
||||||
|
if (element.content.includes(term)) {
|
||||||
|
search_results.posts.push(element)
|
||||||
|
}
|
||||||
|
else if (element.title.includes(term)) {
|
||||||
|
search_results.posts.push(element)
|
||||||
|
}
|
||||||
|
else if (element.tags.toString().includes(term)) {
|
||||||
|
search_results.posts.push(element)
|
||||||
|
};
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (type == 'user' || type == 'any') {
|
||||||
|
let list = getdata('users');
|
||||||
|
list.forEach((element,index) => {
|
||||||
|
if (typeof element.deleted == 'undefined' || element.deleted == false) {
|
||||||
|
if (element.username.includes(term)) {
|
||||||
|
search_results.posts.push(element)
|
||||||
|
}
|
||||||
|
else if (element.prettyname.includes(term)) {
|
||||||
|
search_results.posts.push(element)
|
||||||
|
}
|
||||||
|
else if (element.description.includes(term)) {
|
||||||
|
search_results.posts.push(element)
|
||||||
|
};
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return search_results;
|
||||||
|
};
|
||||||
|
|
||||||
export function getdata(data, index=-1) {
|
export function getdata(data, index=-1) {
|
||||||
|
|
||||||
if (config["data_storage"] == "json") {
|
if (config["data_storage"] == "json") {
|
||||||
@@ -32,7 +70,7 @@ export function getdata(data, index=-1) {
|
|||||||
|
|
||||||
// NOT YET WORKING!
|
// NOT YET WORKING!
|
||||||
if (config["data_storage"] == "mysql") {
|
if (config["data_storage"] == "mysql") {
|
||||||
const mysql = require('mysql');
|
const mysql = require('mysql');
|
||||||
let con = mysql.createConnection({
|
let con = mysql.createConnection({
|
||||||
host: config.database.host,
|
host: config.database.host,
|
||||||
user: config.database.user,
|
user: config.database.user,
|
||||||
@@ -46,6 +84,7 @@ export function getdata(data, index=-1) {
|
|||||||
if (data == "posts" || data == 'users' || data == 'comments') {
|
if (data == "posts" || data == 'users' || data == 'comments') {
|
||||||
con.query(`SELECT * FROM ${data}`, function (err, result, fields) {
|
con.query(`SELECT * FROM ${data}`, function (err, result, fields) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
|
result = Object.values(JSON.parse(JSON.stringify(result)))
|
||||||
console.log(result)
|
console.log(result)
|
||||||
return result;
|
return result;
|
||||||
});
|
});
|
||||||
@@ -53,6 +92,7 @@ export function getdata(data, index=-1) {
|
|||||||
else if (data == 'hitcount') {
|
else if (data == 'hitcount') {
|
||||||
con.query(`SELECT paramValue FROM params WHERE paramName = '${data}'`, function (err, result, fields) {
|
con.query(`SELECT paramValue FROM params WHERE paramName = '${data}'`, function (err, result, fields) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
|
result = Object.values(JSON.parse(JSON.stringify(result)))
|
||||||
console.log(result)
|
console.log(result)
|
||||||
return result;
|
return result;
|
||||||
});
|
});
|
||||||
|
@@ -200,4 +200,21 @@ router.post("/submit_edit_post", (req,res) => {
|
|||||||
}
|
}
|
||||||
}); // /submit_edit
|
}); // /submit_edit
|
||||||
|
|
||||||
|
router.get('/search', (req, res) => {
|
||||||
|
const search_term = req.query.q; // 'q' is the parameter name
|
||||||
|
let search_type = req.query.type; // eg 'any', 'post', 'user' etc...
|
||||||
|
if (typeof search_type == 'undefined') { // Default to 'any'
|
||||||
|
search_type = 'any';
|
||||||
|
}
|
||||||
|
console.log('searching for: ', search_term);
|
||||||
|
const search_results = data.searchdata(search_term, search_type); // data.searchdata returns an array of search results
|
||||||
|
|
||||||
|
res.render('partials/search', {
|
||||||
|
config,
|
||||||
|
locale,
|
||||||
|
search_results,
|
||||||
|
})
|
||||||
|
|
||||||
|
}); // /search
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
20
views/partials/search.ejs
Normal file
20
views/partials/search.ejs
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<%- include('../partials/head'); %>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id='header'>
|
||||||
|
<%- include('../headers/site_wide'); %>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id='results'>
|
||||||
|
<% search_results.posts.forEach((result, index) => { %>
|
||||||
|
<a href="/post/<%- result.id %>"><%- result.title %></a>
|
||||||
|
<% }); %>
|
||||||
|
<% search_results.users.forEach((result, index) => { %>
|
||||||
|
<a href="/user/<%- result.username %>"><%- result.prettyname %></a>
|
||||||
|
<% }); %>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
Reference in New Issue
Block a user