forked from deadvey/blogger-nodejs
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 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) {
|
||||
|
||||
if (config["data_storage"] == "json") {
|
||||
@@ -32,7 +70,7 @@ export function getdata(data, index=-1) {
|
||||
|
||||
// NOT YET WORKING!
|
||||
if (config["data_storage"] == "mysql") {
|
||||
const mysql = require('mysql');
|
||||
const mysql = require('mysql');
|
||||
let con = mysql.createConnection({
|
||||
host: config.database.host,
|
||||
user: config.database.user,
|
||||
@@ -46,6 +84,7 @@ export function getdata(data, index=-1) {
|
||||
if (data == "posts" || data == 'users' || data == 'comments') {
|
||||
con.query(`SELECT * FROM ${data}`, function (err, result, fields) {
|
||||
if (err) throw err;
|
||||
result = Object.values(JSON.parse(JSON.stringify(result)))
|
||||
console.log(result)
|
||||
return result;
|
||||
});
|
||||
@@ -53,6 +92,7 @@ export function getdata(data, index=-1) {
|
||||
else if (data == 'hitcount') {
|
||||
con.query(`SELECT paramValue FROM params WHERE paramName = '${data}'`, function (err, result, fields) {
|
||||
if (err) throw err;
|
||||
result = Object.values(JSON.parse(JSON.stringify(result)))
|
||||
console.log(result)
|
||||
return result;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user