Updated to include channel and guild IDs

This commit is contained in:
Logan Cusano
2023-02-25 04:50:51 -05:00
parent 895a9cefbc
commit 541148311a
3 changed files with 28 additions and 23 deletions

View File

@@ -36,12 +36,16 @@ class RSSRecord {
* @param {*} _title
* @param {*} _link
* @param {*} _category
* @param {*} _guild_id
* @param {*} _channel_id
*/
constructor(_id, _title, _link, _category) {
constructor(_id, _title, _link, _category, _guild_id, _channel_id) {
this.id = _id;
this.title = _title;
this.link= _link;
this.category = _category;
this.category = _category;
this.guild_id = _guild_id;
this.channel_id = _channel_id;
}
getId() {
@@ -82,7 +86,7 @@ exports.Storage = class Storage {
for (var entry of toBeSaved) {
entry = entry.fields
log.DEBUG("Entry:", entry);
this.returnRecord(undefined, entry.title, entry.link, entry.category, (err, record) => {
this.returnRecord(undefined, entry.title, entry.link, entry.category, entry.guild_id, entry.channel_id, (err, record) => {
if (err) callback(err, undefined);
newRecords.push(record);
if (toBeSaved.length === 1) {
@@ -119,13 +123,8 @@ exports.Storage = class Storage {
*/
checkForTitle(title, callback) {
if (!title) callback(new Error("No title given when checking for title"), undefined);
const sqlQuery = `SELECT * FROM ${this.dbTable} WHERE title = '${title}'`;
runSQL(sqlQuery, this.connection, (err, rows) => {
if (err) callback(err, undefined);
if (rows[0]?.title) callback(undefined, true);
else callback(undefined, false);
})
this.getRecordBy("title", title, callback);
}
/**
@@ -157,7 +156,7 @@ exports.Storage = class Storage {
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}');`;
const sqlQuery = `INSERT INTO ${this.dbTable} (title, link, category, guild_id, channel_id) VALUES ('${entryObject.title}', '${entryObject.link}', '${entryObject.category}', '${entryObject.guild_id}', '${entryObject.channel_id}');`;
log.DEBUG(`Adding new entry with SQL query: '${sqlQuery}'`)
@@ -179,6 +178,8 @@ exports.Storage = class Storage {
if (!entryObject.link) callback(new Error("No link given before updating"), undefined);
queryParams.push(`link = '${entryObject.link}'`);
if (entryObject.category) queryParams.push(`category = '${entryObject.category}'`);
if (entryObject.guild_id) queryParams.push(`guild_id = '${entryObject.guild_id}'`);
if (entryObject.channel_id) queryParams.push(`channel_id = '${entryObject.channel_id}'`);
let sqlQuery = `UPDATE ${this.dbTable} SET`;
@@ -230,12 +231,14 @@ exports.Storage = class Storage {
* @param {*} _category The category of the record
* @param {*} callback Callback function to return an error or the record
*/
returnRecord(_id, _title, _link, _category, callback) {
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);
returnRecord(_id, _title, _link, _category, _guild_id, _channel_id, callback) {
log.DEBUG(`Return record for these values: ID: '${_id}', Title: '${_title}', Category: '${_category}', Link: '${_link}', Guild: '${_guild_id}', Channel:'${_channel_id}'`)
if (!_link && !_title && !_guild_id && !_channel_id) callback(new Error("No link or title given when creating a record"), undefined);
let entryObject = {
"title": _title,
"link": _link
"link": _link,
"guild_id": _guild_id,
"channel_id": _channel_id
}
if (_category) entryObject.category = _category;
@@ -245,7 +248,7 @@ exports.Storage = class Storage {
if (err) callback(err, undefined);
this.getRecordBy('id', entryObject.id, (err, record) => {
if (err) callback(err, undefined);
callback(undefined, new RSSRecord(record.id, record.title, record.link, record.category));
callback(undefined, new RSSRecord(record.id, record.title, record.link, record.category, record.guild_id, record.channel_id));
})
})
}
@@ -257,7 +260,7 @@ exports.Storage = class Storage {
if (err) callback(err, undefined);
this.getRecordBy("title", entryObject.title, (err, record) => {
if (err) callback(err, undefined);
callback(undefined, new RSSRecord(record.id, record.title, record.link, record.category));
callback(undefined, new RSSRecord(record.id, record.title, record.link, record.category, record.guild_id, record.channel_id));
})
});
}
@@ -266,7 +269,7 @@ exports.Storage = class Storage {
if (err) callback(err, undefined);
this.getRecordBy('title', entryObject.title, (err, record) => {
if (err) callback(err, undefined);
callback(undefined, new RSSRecord(record.id, record.title, record.link, record.category));
callback(undefined, new RSSRecord(record.id, record.title, record.link, record.category, record.guild_id, record.channel_id));
})
})
}
@@ -288,7 +291,7 @@ exports.Storage = class Storage {
if (err) callback(err, undefined);
for (const row of rows) {
log.DEBUG("Row from SQL query:", row);
rssRecords.push(new RSSRecord(row.id, row.title, row.link, row.category));
rssRecords.push(new RSSRecord(row.id, row.title, row.link, row.category, row.guild_id, row.channel_id));
}
log.DEBUG("All records:", rssRecords);
callback(undefined, rssRecords);