forked from deadvey/blogger-nodejs
Created a per-post hitcount as well a writedata() function that can
write to a particular index or to a whole data type
This commit is contained in:
47
src/data.js
47
src/data.js
@@ -5,6 +5,26 @@ const func = require('./functions.js')
|
||||
const config = require("../config.json")
|
||||
const fs = require("fs")
|
||||
|
||||
// Literally just +1 to the hitcount
|
||||
export function increment_hitcount(postID = -1) { // -1 Means it will increment the timeline hitcount
|
||||
if (config.data_storage == 'json') {
|
||||
if (postID == -1) {
|
||||
let hitcount = getdata('hitcount');
|
||||
hitcount += 1
|
||||
writedata('hitcount', hitcount);
|
||||
}
|
||||
else {
|
||||
let post = getdata('posts', postID);
|
||||
if (typeof post.hitcount != 'undefined') {
|
||||
post.hitcount += 1;
|
||||
writedata('posts', post, postID)
|
||||
return 0
|
||||
}
|
||||
return 1
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export function searchdata(term, type) { // Searches users and posts for any matches
|
||||
let search_results = {"posts": [], "users": []};
|
||||
// Search users
|
||||
@@ -101,3 +121,30 @@ export function getdata(data, index=-1) {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export function writedata(data, data_to_write, index=-1) {
|
||||
if (config["data_storage"] == "json") {
|
||||
if (data == "posts" || data == 'users' || data == 'comments') {
|
||||
if (index == -1) {
|
||||
fs.writeFileSync(`../data/${data}.json`, JSON.stringify(data_to_write), 'utf-8')
|
||||
return 0
|
||||
}
|
||||
else if (index >= 0) {
|
||||
let result = getdata(data);
|
||||
result[index] = data_to_write;
|
||||
fs.writeFileSync(`../data/${data}.json`, JSON.stringify(result), 'utf-8')
|
||||
return 0
|
||||
}
|
||||
return 1
|
||||
}
|
||||
else if (data == "hitcount") {
|
||||
let other_data = func.require_module('../data/data.json') // This file is actually called data.json
|
||||
other_data.hitcount = data_to_write
|
||||
fs.writeFileSync('../data/data.json', JSON.stringify(other_data), 'utf-8')
|
||||
}
|
||||
else {
|
||||
console.log("Error, invalid requested")
|
||||
return 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user