Edit account.js database functions to deal with names
parent
10bc7addb7
commit
9f5ef9239b
|
@ -4,10 +4,11 @@ const localStrategy = require('passport-local').Strategy;
|
||||||
const bcrypt = require('bcrypt');
|
const bcrypt = require('bcrypt');
|
||||||
|
|
||||||
class User {
|
class User {
|
||||||
constructor(id, email, isAdmin) {
|
constructor(id, email, isAdmin, name) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.email = email;
|
this.email = email;
|
||||||
this.isAdmin = isAdmin;
|
this.isAdmin = isAdmin;
|
||||||
|
this.name = name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,62 +76,64 @@ async function generateHash(password) {
|
||||||
return bcrypt.hashSync(password, salt);
|
return bcrypt.hashSync(password, salt);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function create(email, password, isAdmin) {
|
async function create(email, password, isAdmin, name) {
|
||||||
const hash = await generateHash(password);
|
const hash = await generateHash(password);
|
||||||
|
|
||||||
const query = `INSERT INTO accounts.users(email, password, admin)
|
const query = `INSERT INTO accounts.users(email, password, admin, name)
|
||||||
VALUES($1, $2, $3)`;
|
VALUES($1, $2, $3, $4)`;
|
||||||
await database.executeQuery(query, [email, hash, isAdmin]);
|
await database.executeQuery(query, [email, hash, isAdmin, name]);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function edit(id, email, password, isAdmin) {
|
async function edit(id, email, password, isAdmin, name) {
|
||||||
if(password) {
|
if(password) {
|
||||||
const hash = await generateHash(password);
|
const hash = await generateHash(password);
|
||||||
|
|
||||||
const query = `UPDATE accounts.users
|
const query = `UPDATE accounts.users
|
||||||
SET email = $2,
|
SET email = $2,
|
||||||
password = $3,
|
password = $3,
|
||||||
admin = $4
|
admin = $4,
|
||||||
|
name = $5
|
||||||
WHERE user_id = $1;`;
|
WHERE user_id = $1;`;
|
||||||
await database.executeQuery(query, [id, email, hash, isAdmin]);
|
await database.executeQuery(query, [id, email, hash, isAdmin, name]);
|
||||||
} else {
|
} else {
|
||||||
const query = `UPDATE accounts.users
|
const query = `UPDATE accounts.users
|
||||||
SET email = $2,
|
SET email = $2,
|
||||||
admin = $3
|
admin = $3,
|
||||||
|
name = $4
|
||||||
WHERE user_id = $1;`;
|
WHERE user_id = $1;`;
|
||||||
await database.executeQuery(query, [id, email, isAdmin]);
|
await database.executeQuery(query, [id, email, isAdmin, name]);
|
||||||
}
|
}
|
||||||
return new User(id, email, isAdmin);
|
return new User(id, email, isAdmin, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function remove(id) {
|
async function remove(id) {
|
||||||
const query = `DELETE FROM accounts.users
|
const query = `DELETE FROM accounts.users
|
||||||
WHERE user_id = $1
|
WHERE user_id = $1
|
||||||
RETURNING email, admin;`;
|
RETURNING email, admin, name;`;
|
||||||
const row = (await database.executeQuery(query, [id]))[0];
|
const row = (await database.executeQuery(query, [id]))[0];
|
||||||
return new User(id, row[0], row[1]);
|
return new User(id, row[0], row[1], row[2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function retrieveAll() {
|
async function retrieveAll() {
|
||||||
const query = `SELECT user_id, email, admin
|
const query = `SELECT user_id, email, admin, name
|
||||||
FROM accounts.users
|
FROM accounts.users
|
||||||
ORDER BY email;`
|
ORDER BY name;`;
|
||||||
const table = await database.executeQuery(query);
|
const table = await database.executeQuery(query);
|
||||||
|
|
||||||
const accountsList = [];
|
const accountsList = [];
|
||||||
table.forEach((row) => {
|
table.forEach((row) => {
|
||||||
accountsList.push(new User(row[0], row[1], row[2]));
|
accountsList.push(new User(row[0], row[1], row[2], row[3]));
|
||||||
});
|
});
|
||||||
return accountsList;
|
return accountsList;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getFromID(id) {
|
async function getFromID(id) {
|
||||||
const query = `SELECT user_id, email, admin
|
const query = `SELECT user_id, email, admin, name
|
||||||
FROM accounts.users
|
FROM accounts.users
|
||||||
WHERE user_id = $1;`;
|
WHERE user_id = $1;`;
|
||||||
const row = (await database.executeQuery(query, [id]))[0];
|
const row = (await database.executeQuery(query, [id]))[0];
|
||||||
|
|
||||||
return new User(id, row[1], row[2]);
|
return new User(id, row[1], row[2], row[3]);
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.create = create;
|
exports.create = create;
|
||||||
|
|
Reference in New Issue