forked from deadvey/blogger-nodejs
Changed how comments are stored and how data is retrieved
This commit is contained in:
102
src/data.js
102
src/data.js
@@ -14,7 +14,7 @@ export function increment_hitcount(postID = -1) { // -1 Means it will increment
|
||||
writedata('hitcount', hitcount);
|
||||
}
|
||||
else {
|
||||
let post = getdata('posts', postID);
|
||||
let post = getdata('posts','id', postID);
|
||||
if (typeof post.hitcount != 'undefined') {
|
||||
post.hitcount += 1;
|
||||
writedata('posts', post, postID)
|
||||
@@ -68,60 +68,62 @@ export function searchdata(term, type) { // Searches users and posts for any mat
|
||||
return search_results;
|
||||
};
|
||||
|
||||
export function getdata(data, index=-1) {
|
||||
|
||||
if (config["data_storage"] == "json") {
|
||||
if (data == "posts" || data == 'users' || data == 'comments') {
|
||||
let result = func.require_module(`../data/${data}.json`)
|
||||
if (index != -1) {
|
||||
if (index < result.length) {
|
||||
return result[index]
|
||||
}
|
||||
return 1 // This index doesn't exist
|
||||
export function getdata(data_type, key=-1, value=-1) {
|
||||
let result = undefined
|
||||
switch (config["data_storage"]) {
|
||||
case 'json':
|
||||
switch (data_type) {
|
||||
case 'users':
|
||||
case 'posts':
|
||||
case 'comments':
|
||||
result = func.require_module(`../data/${data_type}.json`)
|
||||
if (key != -1) {
|
||||
return result[func.find_key_value_pair(result, key, value)]
|
||||
return -1 // This index doesn't exist
|
||||
}
|
||||
return result
|
||||
break;
|
||||
case 'hitcount':
|
||||
result = func.require_module('../data/data.json') // This file is actually called data.json
|
||||
return result["hitcount"]
|
||||
break;
|
||||
default:
|
||||
console.log("Error, invalid requested")
|
||||
return -1
|
||||
break;
|
||||
}
|
||||
return result
|
||||
}
|
||||
else if (data == "hitcount") {
|
||||
let result = func.require_module('../data/data.json') // This file is actually called data.json
|
||||
return result["hitcount"]
|
||||
}
|
||||
else {
|
||||
console.log("Error, invalid requested")
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
// NOT YET WORKING!
|
||||
if (config["data_storage"] == "mysql") {
|
||||
const mysql = require('mysql');
|
||||
let con = mysql.createConnection({
|
||||
host: config.database.host,
|
||||
user: config.database.user,
|
||||
password: config.database.password,
|
||||
database: config.database.database,
|
||||
});
|
||||
case 'mysql':
|
||||
const mysql = require('mysql');
|
||||
let con = mysql.createConnection({
|
||||
host: config.database.host,
|
||||
user: config.database.user,
|
||||
password: config.database.password,
|
||||
database: config.database.database,
|
||||
});
|
||||
|
||||
con.connect(function(err) {
|
||||
if (err) throw err;
|
||||
con.connect(function(err) {
|
||||
if (err) throw err;
|
||||
|
||||
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;
|
||||
});
|
||||
}
|
||||
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;
|
||||
});
|
||||
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;
|
||||
});
|
||||
}
|
||||
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