From 9f5ef9239b5a6a61e1b603d8e835be53f4bb5222 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Thu, 2 Dec 2021 13:13:14 -0700 Subject: [PATCH] Edit account.js database functions to deal with names --- database/accounts/accounts.js | 39 +++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/database/accounts/accounts.js b/database/accounts/accounts.js index d33d11f..8334d27 100644 --- a/database/accounts/accounts.js +++ b/database/accounts/accounts.js @@ -4,10 +4,11 @@ const localStrategy = require('passport-local').Strategy; const bcrypt = require('bcrypt'); class User { - constructor(id, email, isAdmin) { + constructor(id, email, isAdmin, name) { this.id = id; this.email = email; this.isAdmin = isAdmin; + this.name = name; } } @@ -75,62 +76,64 @@ async function generateHash(password) { return bcrypt.hashSync(password, salt); } -async function create(email, password, isAdmin) { +async function create(email, password, isAdmin, name) { const hash = await generateHash(password); - const query = `INSERT INTO accounts.users(email, password, admin) - VALUES($1, $2, $3)`; - await database.executeQuery(query, [email, hash, isAdmin]); + const query = `INSERT INTO accounts.users(email, password, admin, name) + VALUES($1, $2, $3, $4)`; + 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) { const hash = await generateHash(password); const query = `UPDATE accounts.users SET email = $2, password = $3, - admin = $4 + admin = $4, + name = $5 WHERE user_id = $1;`; - await database.executeQuery(query, [id, email, hash, isAdmin]); + await database.executeQuery(query, [id, email, hash, isAdmin, name]); } else { const query = `UPDATE accounts.users SET email = $2, - admin = $3 + admin = $3, + name = $4 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) { const query = `DELETE FROM accounts.users WHERE user_id = $1 - RETURNING email, admin;`; + RETURNING email, admin, name;`; 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() { - const query = `SELECT user_id, email, admin + const query = `SELECT user_id, email, admin, name FROM accounts.users - ORDER BY email;` + ORDER BY name;`; const table = await database.executeQuery(query); const accountsList = []; 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; } async function getFromID(id) { - const query = `SELECT user_id, email, admin + const query = `SELECT user_id, email, admin, name FROM accounts.users WHERE user_id = $1;`; 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;