Implement Discord CnC Server into Emmelia
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
// Update the functions here to change the storage medium
|
||||
|
||||
// Import modules
|
||||
const { DebugBuilder } = require("./utilities/debugBuilder");
|
||||
const log = new DebugBuilder("server", "libStorage");
|
||||
|
||||
// Storage Specific Modules
|
||||
// MySQL
|
||||
@@ -11,17 +13,17 @@ const mysql = require("mysql");
|
||||
// Helper Functions
|
||||
// Function to run and handle SQL errors
|
||||
function runSQL(sqlQuery, connection, callback = (err, rows) => {
|
||||
console.log(err);
|
||||
log.ERROR(err);
|
||||
throw err;
|
||||
}) {
|
||||
// Start the MySQL Connection
|
||||
//connection.connect();
|
||||
connection.query(sqlQuery, (err, rows) => {
|
||||
if (err) {
|
||||
console.log("SQL Error:", err)
|
||||
log.ERROR("SQL Error:", err)
|
||||
callback(err, undefined);
|
||||
}
|
||||
console.log("RunSQL Returned Rows:", rows);
|
||||
log.DEBUG("RunSQL Returned Rows:", rows);
|
||||
callback(undefined, rows);
|
||||
})
|
||||
//connection.
|
||||
@@ -73,18 +75,18 @@ exports.Storage = class Storage {
|
||||
* @param {function} callback The callback function to be called with the record when saved
|
||||
*/
|
||||
create(toBeSaved, callback) {
|
||||
console.log("To be saved:", toBeSaved);
|
||||
console.log("to be saved length:", toBeSaved.length);
|
||||
log.DEBUG("To be saved:", toBeSaved);
|
||||
log.DEBUG("to be saved length:", toBeSaved.length);
|
||||
if (!toBeSaved[0].fields?.title) callback(Error("No title given"), undefined);
|
||||
let newRecords = []
|
||||
for (var entry of toBeSaved) {
|
||||
entry = entry.fields
|
||||
console.log("Entry:", entry);
|
||||
log.DEBUG("Entry:", entry);
|
||||
this.returnRecord(undefined, entry.title, entry.link, entry.category, (err, record) => {
|
||||
if (err) callback(err, undefined);
|
||||
newRecords.push(record);
|
||||
if (toBeSaved.length === 1) {
|
||||
console.log("One record to callback with:", record);
|
||||
log.DEBUG("One record to callback with:", record);
|
||||
callback(undefined, record);
|
||||
}
|
||||
})
|
||||
@@ -150,14 +152,14 @@ exports.Storage = class Storage {
|
||||
* @param {function} callback The callback to be called with either an error or undefined if successful
|
||||
*/
|
||||
saveEntry(entryObject, callback) {
|
||||
console.log("Saving entry:", entryObject);
|
||||
log.DEBUG("Saving entry:", entryObject);
|
||||
if (!entryObject?.title || !entryObject?.link || !entryObject?.category) {
|
||||
callback(new Error("Entry object malformed, check the object before saving it"), undefined)
|
||||
}
|
||||
|
||||
const sqlQuery = `INSERT INTO ${this.dbTable} (title, link, category) VALUES ('${entryObject.title}', '${entryObject.link}', '${entryObject.category}');`;
|
||||
|
||||
console.log(`Adding new entry with SQL query: '${sqlQuery}'`)
|
||||
log.DEBUG(`Adding new entry with SQL query: '${sqlQuery}'`)
|
||||
|
||||
runSQL(sqlQuery, this.connection, (err, rows) => {
|
||||
if (err) callback(err, undefined);
|
||||
@@ -194,7 +196,7 @@ exports.Storage = class Storage {
|
||||
|
||||
sqlQuery = `${sqlQuery} WHERE title = '${entryObject.title}';`
|
||||
|
||||
console.log(`Updating entry with SQL query: '${sqlQuery}'`)
|
||||
log.DEBUG(`Updating entry with SQL query: '${sqlQuery}'`)
|
||||
|
||||
runSQL(sqlQuery, this.connection, (err, rows) => {
|
||||
if (err) callback(err, undefined);
|
||||
@@ -229,7 +231,7 @@ exports.Storage = class Storage {
|
||||
* @param {*} callback Callback function to return an error or the record
|
||||
*/
|
||||
returnRecord(_id, _title, _link, _category, callback) {
|
||||
console.log(`Return record for these values: ID: '${_id}', Title: '${_title}', Category: '${_category}', Link: '${_link}'`)
|
||||
log.DEBUG(`Return record for these values: ID: '${_id}', Title: '${_title}', Category: '${_category}', Link: '${_link}'`)
|
||||
if (!_link && !_title) callback(new Error("No link or title given when creating a record"), undefined);
|
||||
let entryObject = {
|
||||
"title": _title,
|
||||
@@ -250,7 +252,7 @@ exports.Storage = class Storage {
|
||||
else {
|
||||
this.checkForTitle(_title, (err, titleExists) => {
|
||||
if (!titleExists) {
|
||||
console.log("Entry doesn't exist, making one now", entryObject);
|
||||
log.DEBUG("Entry doesn't exist, making one now", entryObject);
|
||||
this.saveEntry(entryObject, (err, rows) => {
|
||||
if (err) callback(err, undefined);
|
||||
this.getRecordBy("title", entryObject.title, (err, record) => {
|
||||
@@ -277,7 +279,7 @@ exports.Storage = class Storage {
|
||||
* @param {function} callback
|
||||
*/
|
||||
getAllRecords(callback) {
|
||||
console.log("Getting all records");
|
||||
log.INFO("Getting all records");
|
||||
const sqlQuery = `SELECT * FROM ${this.dbTable}`
|
||||
|
||||
let rssRecords = [];
|
||||
@@ -285,10 +287,10 @@ exports.Storage = class Storage {
|
||||
runSQL(sqlQuery, this.connection, (err, rows) => {
|
||||
if (err) callback(err, undefined);
|
||||
for (const row of rows) {
|
||||
console.log("Row from SQL query:", row);
|
||||
log.DEBUG("Row from SQL query:", row);
|
||||
rssRecords.push(new RSSRecord(row.id, row.title, row.link, row.category));
|
||||
}
|
||||
console.log("All records:", rssRecords);
|
||||
log.DEBUG("All records:", rssRecords);
|
||||
callback(undefined, rssRecords);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user