Skip to content

Commit

Permalink
Issue #3: CRUD User Infrastructure
Browse files Browse the repository at this point in the history
- WIP (unfinished and tests are failing)
- Create CRUD functions for database
- Create unit tests for data layer
  • Loading branch information
codebru committed Jul 23, 2019
1 parent c7b2fc2 commit e1a2aa7
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.env
Empty file.
71 changes: 71 additions & 0 deletions Components/User/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/* INCLUDES ******************************* */
const dbConnect = require('../../Utils/Database/connection');
/* **************************************** */

/* DATABASE CONNECTION ******************** */
const db = dbConnect.connect();
/* **************************************** */

/* FUNCTIONS ****************************** */
async function create(passwordHash, email, bio, imageLink) {
const query = {
name: 'addUser',
text: 'INSERT INTO users(passwordhash, email, bio, imagelink, admin) VALUES ($1, $2, $3, $4, FALSE);',
values: [passwordHash, email, bio, imageLink],
};
try {
await db.none(query);
return true;
} catch (err) {
throw new Error(err);
}
}

async function read(email) {
const query = {
name: 'readUser',
text: 'SELECT * FROM users WHERE email = $1',
values: [email],
};
try {
return await db.one(query);
} catch (err) {
throw new Error(err);
}
}

async function update(email, field, value) {
const query = {
name: 'updateUser',
text: 'UPDATE users SET "$1" = "$2" WHERE email = "$3";',
values: [field, value, email],
};
try {
await db.none(query);
return true;
} catch (err) {
throw new Error(err);
}
}

async function remove() {
const query = {
name: 'updateUser',
text: 'DELETE FROM users WHERE email = $1;',
values: [email],
};
try {
await db.none(query);
return true;
} catch (err) {
throw new Error(err);
}
}

/* **************************************** */

/* EXPORTS ******************************** */
module.exports = {
create, read, update, remove,
};
/* **************************************** */
30 changes: 30 additions & 0 deletions Components/User/data.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const data = require('./data');

data.remove('test@test.com');

it('creates a user', async () => {
const response = await data.create('1234', 'test@test.com', 'This is my bio', '');
expect(response).toEqual(true);
});

it('reads a user', async () => {
const response = await data.read('test@test.com');
expect(response.bio).toEqual('This is my bio');
});

it('updates a user', async () => {
await data.create('bio', 'This is my new bio');
const response = await data.read('test@test.com');
expect(response.bio).toEqual('This is my new bio');
});

it('deletes a user', async () => {
let errors = 0;
await data.create('1234', 'test@test.com', 'This is my bio', '');
try {
await data.read('test@test.com');
} catch (err) {
errors += 1;
}
expect(errors).toEqual(1);
});
31 changes: 31 additions & 0 deletions Utils/Database/connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* INCLUDES ******************************* */
const pgPromise = require('pg-promise');
const dotenv = require('dotenv');

const pg = pgPromise();
dotenv.config();
/* **************************************** */

/* CONSTANTS ****************************** */
const {
DB_USER, DB_PASS, DB_HOST, DB_PORT, DB_NAME,
} = process.env;

const connectionInfo = {
host: DB_HOST,
port: DB_PORT,
database: DB_NAME,
user: DB_USER,
password: DB_PASS,
};
/* **************************************** */

/* FUNCTIONS ****************************** */
function connect() {
return pg(connectionInfo);
}
/* **************************************** */

/* EXPORTS ******************************** */
module.exports = { connect };
/* **************************************** */
Empty file removed index.unit.test.js
Empty file.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"helmet": "^3.19.0",
"node-fetch": "^2.6.0",
"passport": "^0.4.0",
"passport-local": "^1.0.0"
"passport-local": "^1.0.0",
"pg-promise": "^8.7.5"
}
}

0 comments on commit e1a2aa7

Please sign in to comment.