Skip to content

Commit

Permalink
Merge pull request #62 from Cavdy/ft-connect-signup-endpoint-to-datab…
Browse files Browse the repository at this point in the history
…ase-165406314

#165406314 connect signup form to database
  • Loading branch information
Cavdy committed Apr 17, 2019
2 parents b50babc + 842dc46 commit a6bb29a
Show file tree
Hide file tree
Showing 13 changed files with 689 additions and 660 deletions.
16 changes: 14 additions & 2 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
{
"presets": [
"@babel/preset-env"
"presets": [
"@babel/preset-env"
],
"plugins": [
[
"@babel/plugin-transform-runtime",
{
"absoluteRuntime": false,
"corejs": false,
"helpers": true,
"regenerator": true,
"useESModules": false
}
]
]
}
55 changes: 29 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"start": "node ./server/build/app.js",
"build": "babel -d ./server/build ./server/v1 -s",
"dev": "nodemon --exec babel-node ./server/v1/app.js",
"test": "mocha ./server/v1 --recursive --require @babel/register --exit || true",
"test": "mocha --timeout 20000 ./server/v1 --recursive --require @babel/register --exit || true",
"generate-lcov": "nyc report --reporter=text-lcov > lcov.info",
"coveralls-coverage": "coveralls < lcov.info",
"codeclimate-coverage": "codeclimate-test-reporter < lcov.info",
Expand All @@ -31,7 +31,9 @@
},
"homepage": "https://github.com/Cavdy/Banka#readme",
"dependencies": {
"@babel/polyfill": "^7.4.3",
"@babel/register": "^7.4.0",
"@babel/runtime": "^7.4.3",
"bcryptjs": "^2.4.3",
"body-parser": "^1.18.3",
"chai": "^4.2.0",
Expand All @@ -51,14 +53,15 @@
"@babel/cli": "^7.2.3",
"@babel/core": "^7.2.2",
"@babel/node": "^7.2.2",
"@babel/plugin-transform-runtime": "^7.4.3",
"@babel/preset-env": "^7.3.1",
"codeclimate-test-reporter": "^0.5.1",
"coveralls": "^3.0.3",
"mocha-lcov-reporter": "^1.3.0",
"nyc": "^13.3.0",
"eslint": "^5.13.0",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-plugin-import": "^2.16.0",
"nodemon": "^1.18.10"
"mocha-lcov-reporter": "^1.3.0",
"nodemon": "^1.18.10",
"nyc": "^13.3.0"
}
}
42 changes: 32 additions & 10 deletions server/v1/config/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,35 @@ const conString = parse(process.env.DB_CONFIG);

const pool = new Pool(conString);

// async/await - check out a client
(async () => {
const client = await pool.connect();
try {
const res = await client.query('SELECT NOW() AS "theTime"');
debug('query')(res.rows[0].theTime);
} finally {
client.release();
}
})().catch(e => debug('query')(e.stack));
const dbConnection = {
async dbConnect(passedQuery, passedData) {
try {
return (async () => {
const client = await pool.connect();
try {
return await client.query(passedQuery, passedData);
} finally {
client.release();
}
})();
} catch (e) {
return debug('query')(e.stack);
}
},
async dbTesting(passedQuery) {
try {
return (async () => {
const client = await pool.connect();
try {
return await client.query(passedQuery);
} finally {
client.release();
}
})();
} catch (e) {
return debug('query')(e.stack);
}
},
};

export default dbConnection;
11 changes: 7 additions & 4 deletions server/v1/config/user.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

-- create users table
CREATE TABLE users (
id SERIAL PRIMARY KEY,
email VARCHAR(80),
id SERIAL PRIMARY KEY UNIQUE,
email VARCHAR(80) UNIQUE,
firstName VARCHAR(20),
lastName VARCHAR(20),
password VARCHAR(80),
Expand All @@ -15,7 +15,10 @@ CREATE TABLE users (
SELECT * FROM "users" LIMIT 10

-- insert into users table
INSERT into "users" values($1), ['value']
INSERT into users values($1), ['value']

-- update into users
update users
update firstname

-- delete from users table
DELETE FROM users;
4 changes: 2 additions & 2 deletions server/v1/controllers/register.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import RegisterService from '../services/register';

const RegisterController = {
registerUser(req, res) {
async registerUser(req, res) {
const userData = req.body;
const createdUser = RegisterService.registerUser(userData);
const createdUser = await RegisterService.registerUser(userData);
return res.json({
status: 'success',
data: createdUser,
Expand Down
23 changes: 2 additions & 21 deletions server/v1/helper/registrationHelper.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,16 @@
/* eslint-disable one-var */
/* eslint-disable one-var-declaration-per-line */
const registrationHelper = {
registrationHelper(users, userData) {
async registrationHelper(userData) {
const firstnameAndLastnameRegex = /^[a-zA-Z ]{2,15}$/;
const emailRegex = /^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,10})$/;
const passwordRegex = /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$/;
const returnValue = [];
let emailPassed, firstnamePassed, lastnamePassed, passwordPassed;

const usersLength = users.length;
const newId = usersLength + 1;
// eslint-disable-next-line no-param-reassign
userData.id = newId;

// Check if email is valid
if (emailRegex.test(userData.email)) {
let checkEmail = false;

// check if email is exist
for (let i = 0; i <= usersLength - 1; i++) {
if (users[i].email === userData.email) {
checkEmail = true;
}
}

// gives output
if (checkEmail) {
returnValue.push('email already exist');
} else {
emailPassed = true;
}
emailPassed = true;
} else {
returnValue.push('Email is required');
}
Expand Down
25 changes: 18 additions & 7 deletions server/v1/services/register.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
/* eslint-disable no-param-reassign */
import usersData from '../../dummyJson/users';
import bcrypt from 'bcryptjs';
import dbConnection from '../config/database';
import registrationHelper from '../helper/registrationHelper';

const { users } = usersData;

const RegisterService = {
registerUser(userData) {
const returnData = registrationHelper.registrationHelper(users, userData);
async registerUser(userData) {
const returnData = await registrationHelper.registrationHelper(userData);
let returnValue;

// eslint-disable-next-line max-len
if (returnData[0] === true && returnData[1] === true && returnData[2] === true && returnData[3] === true) {
const salt = bcrypt.genSaltSync(10);
const hash = bcrypt.hashSync(userData.password, salt);
userData.type = 'client';
userData.isAdmin = false;
users.push(userData);
returnValue = userData;
// checks if email exist
const emailresponse = await dbConnection.dbConnect('SELECT email FROM users WHERE email=$1', [userData.email]);
if (emailresponse.rows.length >= 1) {
returnValue = 'email already exist';
} else {
// email does not exist... you can insert data
const response = await dbConnection.dbConnect('INSERT into users(email, firstName, lastName, password, type, isAdmin) values($1, $2, $3, $4, $5, $6)',
[userData.email, userData.firstName, userData.lastName, hash, userData.type, userData.isAdmin]);
if (response.command === 'INSERT') returnValue = 'Successfully signed up';
else returnValue = 'Something happened';
}
} else {
// eslint-disable-next-line prefer-destructuring
returnValue = returnData[4];
Expand Down
Loading

0 comments on commit a6bb29a

Please sign in to comment.