Skip to content

Commit

Permalink
Merge pull request #47 from fac-13/getqueries
Browse files Browse the repository at this point in the history
write get queries
  • Loading branch information
jennah2121 authored May 11, 2018
2 parents 83f7c33 + abdbf39 commit 7d14fcb
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@
"mocha": "^5.1.1",
"pg-promise": "^8.4.3"
}
}
}
32 changes: 32 additions & 0 deletions src/model/database/db_build.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
2 changes: 1 addition & 1 deletion src/model/database/db_connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
29 changes: 14 additions & 15 deletions src/model/queries/getQueries.js
Original file line number Diff line number Diff line change
@@ -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,
};
4 changes: 2 additions & 2 deletions src/tests/queries.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit 7d14fcb

Please sign in to comment.