From b55d99ccfddc53226d7be23d44a4a6ea63b0e5be Mon Sep 17 00:00:00 2001 From: juakotorres Date: Sun, 15 Mar 2020 00:29:43 -0300 Subject: [PATCH] database --- .gitignore | 3 +-- database.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 database.js diff --git a/.gitignore b/.gitignore index 2bccfbc..ee73f1b 100644 --- a/.gitignore +++ b/.gitignore @@ -110,5 +110,4 @@ dist credentials.json .idea -package-lock.json -database.js \ No newline at end of file +package-lock.json \ No newline at end of file diff --git a/database.js b/database.js new file mode 100644 index 0000000..3e7d9cf --- /dev/null +++ b/database.js @@ -0,0 +1,36 @@ +// Postgres +const Pool = require('pg').Pool; + +// Postgres config. +let postgresConfig = require("./credentials.json").postgres; + +// Postgres initialization +const pool = new Pool(postgresConfig); + +async function createUser(userId, userName) { + await pool.query('INSERT INTO users (user_id, user_name) VALUES ($1, $2)', [userId, userName]); + let res = await pool.query('SELECT * FROM users WHERE user_id = $1', [userId]); + return res.rows; +} + +async function getUser(userId) { + let res = await pool.query('SELECT * FROM users WHERE user_id = $1', [userId]); + return res.rows; +} + +async function userFinishedTheirThesis(userId, date, rt_date) { + let res = await pool.query('UPDATE USERS SET FT_DATE = $1, RT_DATE = $2 WHERE USER_ID = $3', [date, rt_date, userId]); + return res.rows; +} + +async function reviewDateUpdate(userId, date) { + let res = await pool.query('UPDATE USERS SET RT_DATE = $1 WHERE USER_ID = $2', [date, userId]); + return res.rows; +} + +module.exports = { + createUser, + getUser, + userFinishedTheirThesis, + reviewDateUpdate +}; \ No newline at end of file