Continue migrating/refactoring

This commit is contained in:
Calvin Montgomery 2013-08-12 23:23:10 -04:00
parent a112700600
commit 9675edadf5
1 changed files with 119 additions and 18 deletions

View File

@ -228,9 +228,10 @@ Database.prototype.isGlobalIPBanned = function (ip, callback) {
}; };
Database.prototype.getGlobalIPBans = function (callback) { Database.prototype.getGlobalIPBans = function (callback) {
if(typeof callback !== "function")
callback = function () { }
var self = this; var self = this;
if(typeof callback !== "function")
callback = function () { };
self.query("SELECT * FROM global_bans WHERE 1", function (err, res) { self.query("SELECT * FROM global_bans WHERE 1", function (err, res) {
if(err) { if(err) {
callback(err, null); callback(err, null);
@ -247,11 +248,12 @@ Database.prototype.getGlobalIPBans = function (callback) {
}; };
Database.prototype.setGlobalIPBan = function (ip, reason, callback) { Database.prototype.setGlobalIPBan = function (ip, reason, callback) {
var self = this;
if(typeof callback !== "function") if(typeof callback !== "function")
callback = function () { } callback = function () { };
var query = "INSERT INTO global_bans VALUES (?, ?)" + var query = "INSERT INTO global_bans VALUES (?, ?)" +
" ON DUPLICATE KEY UPDATE note=?"; " ON DUPLICATE KEY UPDATE note=?";
var self = this;
self.query(query, [ip, reason, reason], function (err, res) { self.query(query, [ip, reason, reason], function (err, res) {
if(err) { if(err) {
callback(err, null); callback(err, null);
@ -264,9 +266,10 @@ Database.prototype.setGlobalIPBan = function (ip, reason, callback) {
}; };
Database.prototype.clearGlobalIPBan = function (ip, callback) { Database.prototype.clearGlobalIPBan = function (ip, callback) {
if(typeof callback !== "function")
callback = function () { }
var self = this; var self = this;
if(typeof callback !== "function")
callback = function () { };
var query = "DELETE FROM global_bans WHERE ip=?"; var query = "DELETE FROM global_bans WHERE ip=?";
self.query(query, [ip], function (err, res) { self.query(query, [ip], function (err, res) {
@ -282,20 +285,28 @@ Database.prototype.clearGlobalIPBan = function (ip, callback) {
/* END REGION */ /* END REGION */
/* REGION channels */ /* REGION channels */
Database.prototype.registerChannel = function (name, owner, callback) { Database.prototype.channelExists = function (name, callback) {
var self = this;
if(typeof callback !== "function") if(typeof callback !== "function")
callback = function () { } return;
var query = "SELECT name FROM channels WHERE name=?";
self.query(query, [name], function (err, res) {
callback(err, res.length > 0);
});
};
Database.prototype.registerChannel = function (name, owner, callback) {
var self = this;
if(typeof callback !== "function")
callback = function () { };
if(!name.match(/^[\w-_]+$/)) { if(!name.match(/^[\w-_]+$/)) {
callback("Invalid channel name", null); callback("Invalid channel name", null);
return; return;
} }
var self = this; // Messy, but I can't think of a better async solution atm
// I'm tempted to add a promise library to the dependencies
// just to solve this mess
var query = "SELECT * FROM channels WHERE name=?"; var query = "SELECT * FROM channels WHERE name=?";
self.query(query, [name], function (err, res) { self.query(query, [name], function (err, res) {
if(!err && res.length > 0) { if(!err && res.length > 0) {
@ -355,12 +366,15 @@ Database.prototype.registerChannel = function (name, owner, callback) {
}; };
Database.prototype.loadChannelData = function (chan, callback) { Database.prototype.loadChannelData = function (chan, callback) {
var self = this;
if(typeof callback !== "function")
callback = function () { };
if(!chan.name.match(/^[\w-_]+$/)) { if(!chan.name.match(/^[\w-_]+$/)) {
callback("Invalid channel name", null); callback("Invalid channel name", null);
return; return;
} }
var self = this;
var query = "SELECT * FROM channels WHERE name=?"; var query = "SELECT * FROM channels WHERE name=?";
self.query(query, [chan.name], function (err, res) { self.query(query, [chan.name], function (err, res) {
@ -401,12 +415,15 @@ Database.prototype.loadChannelData = function (chan, callback) {
}; };
Database.prototype.dropChannel = function (name, callback) { Database.prototype.dropChannel = function (name, callback) {
var self = this;
if(typeof callback !== "function")
callback = function () { };
if(!name.match(/^[\w-_]+$/)) { if(!name.match(/^[\w-_]+$/)) {
callback("Invalid channel name", null); callback("Invalid channel name", null);
return; return;
} }
var self = this;
var query = "DROP TABLE `chan_?_bans`,`chan_?_ranks`,`chan_?_library`" var query = "DROP TABLE `chan_?_bans`,`chan_?_ranks`,`chan_?_library`"
.replace(/\?/g, name); .replace(/\?/g, name);
@ -428,11 +445,13 @@ Database.prototype.dropChannel = function (name, callback) {
}; };
Database.prototype.getChannelRank = function (channame, names, callback) { Database.prototype.getChannelRank = function (channame, names, callback) {
var self = this;
if(typeof callback !== "function")
return;
if(typeof names === "string") if(typeof names === "string")
names = [names]; names = [names];
var self = this;
// Build the query template (?, ?, ?, ?, ...) // Build the query template (?, ?, ?, ?, ...)
var nlist = []; var nlist = [];
for(var i in names) for(var i in names)
@ -465,12 +484,15 @@ Database.prototype.getChannelRank = function (channame, names, callback) {
}; };
Database.prototype.setChannelRank = function (channame, name, rank, callback) { Database.prototype.setChannelRank = function (channame, name, rank, callback) {
var self = this;
if(typeof callback !== "function")
callback = function () { };
if(!channame.match(/^[\w-_]+$/)) { if(!channame.match(/^[\w-_]+$/)) {
callback("Invalid channel name", null); callback("Invalid channel name", null);
return; return;
} }
var self = this;
var query = "INSERT INTO `chan_" + channame + "_ranks` " + var query = "INSERT INTO `chan_" + channame + "_ranks` " +
"(name, rank) VALUES (?, ?) " + "(name, rank) VALUES (?, ?) " +
"ON DUPLICATE KEY UPDATE rank=?"; "ON DUPLICATE KEY UPDATE rank=?";
@ -480,4 +502,83 @@ Database.prototype.setChannelRank = function (channame, name, rank, callback) {
}); });
}; };
Database.prototype.listChannelRanks = function (channame, callback) {
var self = this;
if(typeof callback !== "function")
return;
if(!channame.match(/^[\w-_]+$/)) {
callback("Invalid channel name", null);
return;
}
var query = "SELECT * FROM `chan_" + channame + "_ranks` WHERE 1";
self.query(query, function (err, res) {
callback(err, res);
});
};
Database.prototype.addToLibrary = function (channame, media, callback) {
var self = this;
if(typeof callback !== "function")
callback = function () { };
if(!channame.match(/^[\w-_]+$/)) {
callback("Invalid channel name");
return;
}
var query = "INSERT INTO `chan_" + channame + "_ranks`" +
"(id, title, seconds, type) " +
"VALUES (?, ?, ?, ?)";
var params = [
media.id,
media.title,
media.seconds,
media.type
];
self.query(query, params, function (err, res) {
callback(err, res);
});
};
Database.prototype.removeFromLibrary = function (channame, id, callback) {
var self = this;
if(typeof callback !== "function")
callback = function () { };
if(!channame.match(/^[\w-_]+$/)) {
callback("Invalid channel name", null);
return;
}
var query = "DELETE FROM `chan_" + channame + "_library` WHERE id=?";
self.query(query, [id], function (err, res) {
callback(err, res);
});
};
Database.prototype.getLibraryItem = function (channame, id, callback) {
var self = this;
if(typeof callback !== "function")
callback = function () { };
if(!channame.match(/^[\w-_]+$/)) {
callback("Invalid channel name", null);
return;
}
var query = "SELECT id, title, seconds, type FROM " +
"`chan_" + channame + "_library` WHERE id=?";
self.query(query, [id], function (err, res) {
if(err) {
callback(err, null);
return;
}
callback(null, res.length > 0 ? res[0] : null);
});
};
module.exports = Database; module.exports = Database;