Skip to content

Commit

Permalink
Update style a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
ostracod committed Apr 3, 2021
1 parent f25bea0 commit b22a922
Show file tree
Hide file tree
Showing 9 changed files with 1,106 additions and 1,143 deletions.
229 changes: 113 additions & 116 deletions accountUtils.js
Original file line number Diff line number Diff line change
@@ -1,131 +1,128 @@

var bcrypt = require("bcrypt");
const bcrypt = require("bcrypt");

function AccountUtils() {

}

var accountUtils = new AccountUtils();

module.exports = {
accountUtils: accountUtils
};

var dbUtils = require("./dbUtils").dbUtils;

AccountUtils.prototype.generatePasswordHash = function(password, done) {
bcrypt.hash(password, 10, function(error, result) {
if (error) {
done({
success: false,
error: error
});
return;
}
done({
success: true,
hash: result
});
});
}

AccountUtils.prototype.comparePasswordWithHash = function(password, hash, done) {
bcrypt.compare(password, hash, function(error, result) {
if (error) {
done({
success: false,
error: error
});
return;
}
done({
success: true,
isMatch: result
});
});
}

AccountUtils.prototype.addAccount = function(account, done) {
dbUtils.performQuery(
"INSERT INTO Users (username, passwordHash, emailAddress, score) VALUES (?, ?, ?, 0)",
[account.username, account.passwordHash, account.emailAddress],
function (error, results, fields) {
class AccountUtils {

generatePasswordHash(password, done) {
bcrypt.hash(password, 10, (error, result) => {
if (error) {
done(dbUtils.convertSqlErrorToText(error));
done({
success: false,
error: error,
});
return;
}
done(null);
}
);
}

AccountUtils.prototype.getAccountByUsername = function(username, done) {
dbUtils.performQuery(
"SELECT * FROM Users WHERE username = ?",
[username],
function (error, results, fields) {
if (error) {
done(dbUtils.convertSqlErrorToText(error), null);
return;
}
if (results.length > 0) {
done(null, results[0]);
} else {
done(null, null);
}
}
);
}

AccountUtils.prototype.updateAccount = function(uid, valueSet, done) {
var tempQueryTextList = [];
var tempValueList = [];
for (name in valueSet) {
var tempValue = valueSet[name];
tempQueryTextList.push(name + " = ?");
tempValueList.push(tempValue);
done({
success: true,
hash: result,
});
});
}
var tempQueryText = tempQueryTextList.join(", ");
tempValueList.push(uid);
dbUtils.performQuery(
"UPDATE Users SET " + tempQueryText + " WHERE uid = ?",
tempValueList,
function (error, results, fields) {

comparePasswordWithHash(password, hash, done) {
bcrypt.compare(password, hash, (error, result) => {
if (error) {
done(dbUtils.convertSqlErrorToText(error));
done({
success: false,
error: error,
});
return;
}
done(null);
done({
success: true,
isMatch: result,
});
});
}

addAccount(account, done) {
dbUtils.performQuery(
"INSERT INTO Users (username, passwordHash, emailAddress, score) VALUES (?, ?, ?, 0)",
[account.username, account.passwordHash, account.emailAddress],
(error, results, fields) => {
if (error) {
done(dbUtils.convertSqlErrorToText(error));
return;
}
done(null);
},
);
}

getAccountByUsername(username, done) {
dbUtils.performQuery(
"SELECT * FROM Users WHERE username = ?",
[username],
(error, results, fields) => {
if (error) {
done(dbUtils.convertSqlErrorToText(error), null);
return;
}
if (results.length > 0) {
done(null, results[0]);
} else {
done(null, null);
}
},
);
}

updateAccount(uid, valueSet, done) {
const tempQueryTextList = [];
const tempValueList = [];
for (const name in valueSet) {
var tempValue = valueSet[name];
tempQueryTextList.push(name + " = ?");
tempValueList.push(tempValue);
}
);
const tempQueryText = tempQueryTextList.join(", ");
tempValueList.push(uid);
dbUtils.performQuery(
"UPDATE Users SET " + tempQueryText + " WHERE uid = ?",
tempValueList,
(error, results, fields) => {
if (error) {
done(dbUtils.convertSqlErrorToText(error));
return;
}
done(null);
},
);
}

removeAccount(uid, done) {
dbUtils.performQuery(
"DELETE FROM Users WHERE uid = ?",
[uid],
(error, results, fields) => {
if (error) {
done(dbUtils.convertSqlErrorToText(error));
return;
}
done(null);
},
);
}

getLeaderboardAccounts(amount, done) {
dbUtils.performQuery(
"SELECT * FROM Users ORDER BY score DESC LIMIT 20",
[],
(error, results, fields) => {
if (error) {
done(dbUtils.convertSqlErrorToText(error), null);
return;
}
done(null, results);
},
);
}
}

AccountUtils.prototype.removeAccount = function(uid, done) {
dbUtils.performQuery(
"DELETE FROM Users WHERE uid = ?",
[uid],
function (error, results, fields) {
if (error) {
done(dbUtils.convertSqlErrorToText(error));
return;
}
done(null);
}
);
}
const accountUtils = new AccountUtils();

AccountUtils.prototype.getLeaderboardAccounts = function(amount, done) {
dbUtils.performQuery(
"SELECT * FROM Users ORDER BY score DESC LIMIT 20",
[],
function (error, results, fields) {
if (error) {
done(dbUtils.convertSqlErrorToText(error), null);
return;
}
done(null, results);
}
);
}
module.exports = { accountUtils };

const { dbUtils } = require("./dbUtils");


Loading

0 comments on commit b22a922

Please sign in to comment.