Skip to content

Commit

Permalink
Merge pull request #4 from adelecalvo/master
Browse files Browse the repository at this point in the history
fixed postgres tables and routes
  • Loading branch information
victorvrv authored Feb 12, 2019
2 parents 24fa17b + eb02dcd commit b55ecbf
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 85 deletions.
1 change: 1 addition & 0 deletions server/controllers/cardController.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ cardController.deleteCard = async (req, res, next) => {
// should return an array of results
cardController.getCards = async (req, res, next) => {
const result = await Cards.getCards(req);
console.log('the result is', result);
res.locals.result = JSON.stringify(result);
next();
};
Expand Down
115 changes: 68 additions & 47 deletions server/db/cardModel.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const pg = require('pg');

const uri = 'postgres://pfa:pfa@localhost/jobs_db';
const uri = 'postgres://pfa:pfa@localhost/jobs';
const client = new pg.Client(uri);

client.connect((err) => {
Expand All @@ -13,72 +13,94 @@ client.connect((err) => {

const cardModel = {};

client.query(`
client
.query(
`
CREATE TABLE IF NOT EXISTS cards
(
card_id SERIAL PRIMARY KEY,
title VARCHAR(100) NOT NULL,
title TEXT NOT NULL,
company VARCHAR(100) NOT NULL,
description VARCHAR(500),
location TEXT,
link TEXT,
salary TEXT,
notes VARCHAR(100),
notes TEXT,
contact TEXT,
priority INTEGER,
username TEXT,
created_date TIMESTAMP DEFAULT NOW(),
last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
`)
.then((res) => {
return res;
})
`,
)
.then(res => res)
.catch(e => console.error(e.stack));

// create a new card that is tied to a unique user
cardModel.createCard = async (req, res) => {
const { jobTitle, company, jobDescription, jobLocation, url, salaryRange, note } = req.body;
return client.query(`
const {
title,
company,
description,
location,
link,
salary,
notes,
contact,
priority,
username,
} = req.body;
return client
.query(
`
INSERT INTO cards
(title, company, description, location, link, salary, notes)
(title, company, description, location, link, salary, notes, contact, priority, username)
VALUES (
'${jobTitle}',
'${title}',
'${company}',
'${jobDescription}',
'${jobLocation}',
'${url}',
'${salaryRange}',
'${note}'
'${description}',
'${location}',
'${link}',
'${salary}',
'${notes}',
'${contact}',
'${priority}',
'${username}'
);
`)
.then(() => {
return true;
})
`,
)
.then(() => true)
.catch((err) => {
console.log('ERROR with creating card in database', err);
return false;
});
};

cardModel.updateCard = async (req, res) => {
const { jobTitle, company, jobDescription, jobLocation, url, salaryRange, note } = req.body;
let updated = Date();
return client.query(`
const {
jobTitle, company, jobDescription, jobLocation, url, salaryRange, note,
} = req.body;
const updated = Date();
return client
.query(
`
UPDATE cards SET
jobTitle = title, company = company, jobDescription = jobDescription,
jobLocation = jobLocation, url = url, salaryRange = salaryRange, note = note)
VALUES (
'${jobTitle}',
'${company}',
'${jobDescription}',
'${jobLocation}',
'${url}',
'${salaryRange}',
'${note}',
'${updated}'
${jobTitle},
${company},
${jobDescription},
${jobLocation},
${url},
${salaryRange},
${note},
${updated}
);
`)
.then((res) => {
return true;
})
`,
)
.then(res => true)
.catch((err) => {
console.log('ERROR with updating card in database', err);
return false;
Expand All @@ -88,10 +110,10 @@ cardModel.updateCard = async (req, res) => {
// DELETE row in cards that match card_id
cardModel.deleteCard = async (req, res) => {
const { card_id } = req.body;
return client.query(`DELETE FROM cards WHERE card_id = card_id;`)
.then((res) => {
return true;
})

return client
.query(`DELETE FROM cards WHERE card_id = ${card_id};`)
.then(res => true)
.catch((err) => {
console.log('ERROR with deleting card in database', err);
return false;
Expand All @@ -112,14 +134,13 @@ cardModel.deleteAllCards = async (req, res) => {

// retrieve all rows in cards that match given uuid
cardModel.getCards = async (req, res) => {
return client.query(`SELECT * FROM cards WHERE uuid = uuid;`)
.then((res) => {
return res.rows;
})
const { username } = req.body;
return client
.query(`SELECT * FROM cards WHERE username = '${username}';`)
.then(res => res.rows)
.catch((err) => {
console.log('ERROR with getting cards from database', err);
console.log('ERROR with getting cards from database');
return false;
});
};

module.exports = cardModel;
67 changes: 39 additions & 28 deletions server/db/userModel.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const pg = require('pg');
const bcrypt = require('bcrypt');

const saltRounds = 10;

const uri = 'postgres://pfa:pfa@localhost/jobs_db';
const uri = 'postgres://pfa:pfa@localhost/jobs';
const client = new pg.Client(uri);

client.connect((err) => {
Expand All @@ -15,25 +16,29 @@ client.connect((err) => {

const userModel = {};

client.query(`
client
.query(
`
CREATE TABLE IF NOT EXISTS users
(
id serial PRIMARY KEY,
_id SERIAL PRIMARY KEY,
f_name VARCHAR(100),
l_name VARCHAR(100),
username text UNIQUE NOT NULL,
email text UNIQUE,
password text NOT NULL,
created TIMESTAMP NOT NULL
);
`)
`,
)
.then(res => console.log(res.rows[0]))
.catch(e => console.error(e.stack))
.catch(e => console.error(e.stack));

userModel.verify = async (req) => {
const { username, password, email } = req.body;
console.log(req.body, 'in sign in');
return client.query(`SELECT * FROM users WHERE username = '${username}'`)
return client
.query(`SELECT * FROM users WHERE username = '${username}'`)
.then((res) => {
if (bcrypt.compareSync(password, res.rows[0].password)) {
return true;
Expand All @@ -46,18 +51,23 @@ userModel.verify = async (req) => {
});
};

// By default node-postgres reads rows and collects them into
// JavaScript objects with the keys matching the column names
// By default node-postgres reads rows and collects them into
// JavaScript objects with the keys matching the column names
// and the values matching the corresponding row value for each column
userModel.createUser = async (req, res) => {
const { f_name, l_name, username, email, password } = req.body;
const {
f_name, l_name, username, email, password,
} = req.body;
const salt = bcrypt.genSaltSync(saltRounds);
const hash = bcrypt.hashSync(password, salt);

// CREATE TABLE users if it doesn't exist
// a unique psql id and date_created value should be returned
const created = '014-03-11 19:39:40';
return client.query(`
return (
client
.query(
`
INSERT INTO users
(f_name, l_name, username, email, password, created)
VALUES
Expand All @@ -69,25 +79,26 @@ userModel.createUser = async (req, res) => {
'${hash}',
'${created}'
)
RETURNING id, created;
`)
.then((result) => {
//req object is read-only
//create property on res object to store psql-generated uuid
return true;
RETURNING _id, created;
`,
)
.then((result) => {
true;
})

// return client.query(`INSERT INTO users (f_name, l_name, username, email, password) VALUES ('${f_name}', '${l_name}', '${username}', '${email}', '${hash}')`)
// .then((res) => {
// return true;
// })
// .catch((err) => {
// console.log('ERROR with creating user in database', err);
// return false;
})
.catch((err) => {
console.log('ERROR with creating user in database', err);
return false;
});
// return client.query(`INSERT INTO users (f_name, l_name, username, email, password) VALUES ('${f_name}', '${l_name}', '${username}', '${email}', '${hash}')`)
// .then((res) => {
// return true;
// })
// .catch((err) => {
// console.log('ERROR with creating user in database', err);
// return false;

.catch((err) => {
console.log('ERROR with creating user in database', err);
return false;
})
);
};

module.exports = userModel;
20 changes: 10 additions & 10 deletions server/server.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
const express = require('express');

const app = express();
const path = require('path');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const db = require('./db/userModel');
const userController = require('./controllers/userController');
const sessionController = require('./controllers/sessionController');
const cardController = require('./controllers/cardController')
const cardController = require('./controllers/cardController');

app.use(express.static(path.join(__dirname, './../')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());


app.get('/', (req, res, next) => {
res.sendFile(path.join(__dirname, '../index.html'));
});

//if user is authenticated, fetch his job cards from db
// if user is authenticated, fetch his job cards from db
// app.post('/signin', userController.verify, cardModel.getCards, (req, res, next) => {
// if (res.locals.result) res.status(200).redirect('../userpage.html');
// else res.status(404).send('could not find username and/or password');
// });

//user should be presented with a new card page after successful signup
// user should be presented with a new card page after successful signup
// app.post('/signin', userController.verify, sessionController.startSession, cardController.getCards, (req, res, next) => {
// if (res.locals.result) res.status(200).send();
app.post('/signin', userController.verify, sessionController.startSession, (req, res, next) => {
if (res.locals.result) res.status(200).redirect(req.baseUrl + '/secret');

if (res.locals.result) res.status(200).redirect(`${req.baseUrl}/secret`);
else res.status(404).send('could not find username and/or password');
});

app.get('/secret', sessionController.isLoggedIn, (req, res, next) => {
res.status(200).send('secret page!');
})
});

app.post('/signup', userController.signup, (req, res, next) => {
if (res.locals.result) res.status(200).redirect(req.baseUrl + '/secret');
if (res.locals.result) res.status(200).redirect(`${req.baseUrl}/secret`);
else res.status(404).send('SHENANIGANS :(');
});

app.post('/newjobcard', cardController.addCard, (req, res) => {
if (res.locals.result) res.status(200).send('New Job card created');
else res.status(404).send('No good');
})
});

app.get('/getCards', cardController.getCards, (req, res, next) => {
if (res.locals.result) res.status(200).send();
console.log(res.locals.result);
if (res.locals.result) res.status(200).send(res.locals.result);
else res.status(404).send('SHENANIGANS :(');
});

Expand Down

0 comments on commit b55ecbf

Please sign in to comment.