diff --git a/package.json b/package.json index 536a3cb..e3b3014 100644 --- a/package.json +++ b/package.json @@ -39,4 +39,4 @@ "mocha": "^5.1.1", "pg-promise": "^8.4.3" } -} +} \ No newline at end of file diff --git a/src/model/database/db_build.sql b/src/model/database/db_build.sql index 2c118a1..ff6b127 100644 --- a/src/model/database/db_build.sql +++ b/src/model/database/db_build.sql @@ -83,14 +83,30 @@ INSERT into users (username, password) VALUES ('eade', 'passworddd'); +INSERT into users + (username, password) +VALUES + ('stanley', 'passworddd'); INSERT into symptoms (symptom, user_id) VALUES ('rash', 1); +INSERT into symptoms + (symptom, user_id) +VALUES + ('itch', 1); +INSERT into symptoms + (symptom, user_id) +VALUES + ('hyper', 2); INSERT into factors (factor, user_id) VALUES ('water', 1); +INSERT into factors + (factor, user_id) +VALUES + ('sleep', 1); INSERT into symptom_scale (symptom_id, user_id, comment_1, comment_6) VALUES @@ -103,4 +119,20 @@ INSERT into factor_data (factor_id, user_id, rating) VALUES (1, 1, 8); +INSERT into symptom_data + (symptom_id, user_id, rating) +VALUES + (1, 1, 8); +INSERT into symptom_data + (symptom_id, user_id, rating) +VALUES + (1, 1, 6); +INSERT into symptom_data + (symptom_id, user_id, rating) +VALUES + (2, 1, 6); +INSERT into symptom_data + (symptom_id, user_id, rating) +VALUES + (3, 2, 8); COMMIT; \ No newline at end of file diff --git a/src/model/database/db_connect.js b/src/model/database/db_connect.js index 4ca5c01..b21044e 100644 --- a/src/model/database/db_connect.js +++ b/src/model/database/db_connect.js @@ -4,7 +4,7 @@ require('env2')('./config.env'); const DB_URL = process.env.ENV === 'test' - ? process.env.DB_TEST_URL + ? process.env.TEST_DB_URL : process.env.DB_URL; if (!DB_URL) { diff --git a/src/model/queries/getQueries.js b/src/model/queries/getQueries.js index 57aa6c3..3aee5cb 100644 --- a/src/model/queries/getQueries.js +++ b/src/model/queries/getQueries.js @@ -1,28 +1,27 @@ const dbConnect = require('../database/db_connect.js'); + // get username and password from users table const getUserData = username => dbConnect.query('SELECT password FROM users WHERE username=$1', [username]) .then(res => res[0]); -// get a symptom from symptoms table -const getSymptoms = userId => dbConnect.query('SELECT * FROM symptoms WHERE user_id=$1', [userId]); -// get scale comments for symptom scale - -// get a factor from factors table -const getFactors = userId => dbConnect.query('SELECT * FROM factors WHERE user_id=$1', [userId]); -// get username and password from users table +// get all user symptoms from symptoms table +const getSymptoms = username => dbConnect.query('select symptom from symptoms where user_id = (select id from users where username=$1)', [username]); +// get all user factors from factor table -// get a symptom from symptoms table +const getFactors = username => dbConnect.query('select factor from factors where user_id = (select id from users where username=$1)', [username]); // get scale comments for symptom scale - -// get a factor from factors table +const getSymptomScale = (symptom, username) => dbConnect.query('select * from symptom_scale where symptom_id = (select id from symptoms where symptom=$1) and user_id = (select id from users where username=$2);)', [symptom, username]); // get scale comment for factor scale +const getFactorScale = (factor, username) => dbConnect.query('select * from factor_scale where factor_id = (select id from factors where factor=$1) and user_id = (select id from users where username=$2);)', [factor, username]); -// get specific symptom ratings - -// get specific factor ratings - -module.exports = { getUserData, getSymptoms, getFactors }; +// get specific symptom data +const getSymptomRatings = username => dbConnect.query('select symptom_data.rating, symptom_data.date_entered, symptoms.symptom from symptom_data left join symptoms on symptoms.id = symptom_data.symptom_id where symptom_data.user_id = (select id from users where username=$1)', [username]); +// get specific factor data +const getFactorRatings = username => dbConnect.query('select factor_data.rating, factor_data.date_entered, factors.factor from factor_data left join factors on factors.id = factor_data.factor_id where factor_data.user_id = (select id from users where username=$1)', [username]); +module.exports = { + getUserData, getSymptoms, getFactors, getSymptomScale, getFactorScale, getSymptomRatings, getFactorRatings, +}; diff --git a/src/tests/queries.test.js b/src/tests/queries.test.js index 5aeecc9..598e429 100644 --- a/src/tests/queries.test.js +++ b/src/tests/queries.test.js @@ -32,7 +32,7 @@ test('testing that getUserData returns an object with user password', (t) => { // test query to get a symptom from symptoms table test('testing symptoms query returns something', (t) => { runDbBuild() - .then(() => getSymptoms(1)) + .then(() => getSymptoms('eade')) .then((queryResult) => { t.ok(queryResult); t.end(); @@ -48,7 +48,7 @@ test('testing symptoms query returns something', (t) => { // test query to get a factor from factors table test('testing factors query returns something', (t) => { runDbBuild() - .then(() => getFactors(1)) + .then(() => getFactors('eade')) .then((queryResult) => { t.ok(queryResult); t.end();