Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MVP mostly completed #36

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 78 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ const pg = require('pg');
*/

const configs = {
user: 'akira',
host: '127.0.0.1',
database: 'testdb',
port: 5432,
user: 'postgres',
host: '127.0.0.1',
database: 'tweedr_db',
port: 5432,
password: 'postgres'
};

const pool = new pg.Pool(configs);

pool.on('error', function (err) {
console.log('idle client error', err.message, err.stack);
console.log('idle client error', err.message, err.stack);
});

// Init express app
Expand All @@ -30,8 +31,10 @@ const app = express();
app.use(methodOverride('_method'));
app.use(cookieParser());
app.use(express.urlencoded({
extended: true
extended: true
}));
app.use(express.static(__dirname + '/public/'));


// Set react-views to be the default view engine
const reactEngine = require('express-react-views').createEngine();
Expand All @@ -47,29 +50,87 @@ app.engine('jsx', reactEngine);

// Root GET request (it doesn't belong in any controller file)
app.get('/', (request, response) => {
response.send('Welcome To Tweedr.');
var loggedIn = request.cookies['loggedIn'];
if (loggedIn === undefined) {
response.cookie('loggedIn', false);
}

// INNER JOIN users ON user_id=user.id
const queryString = 'SELECT tweeds.id, tweeds.tweed, tweeds.user_id, users.id FROM tweeds JOIN users ON tweeds.user_id=users.id'

pool.query(queryString, (error, queryResult) => {
error ? console.error(error.stack) : null;
response.render('user/allTweeds', {'tweeds':queryResult.rows});
});
});

app.get('/users/new', (request, response) => {
response.render('user/newuser');
response.render('user/newuser');
});

app.post('/users', (request, response) => {
app.get('/users/login', (request, response) => {
response.render('user/login');
});

const queryString = 'INSERT INTO users (name, password) VALUES ($1, $2)';
app.post('/users/login', (request, response) => {
const queryString = 'SELECT * FROM users WHERE username=$1 OR password=$2';
const values = [
request.body.name,
request.body.username,
request.body.password
];
pool.query(queryString, values, (error, queryResult) => {
error ? console.error(error.stack) : null;

let accountFound = queryResult.rows.find(result => {
return result.username === values[0] && result.password === values[1];
})
if (accountFound) {
response.cookie('loggedIn', true);
response.cookie('username', accountFound.username);
response.cookie('id', accountFound.id);
response.render('default');
} else if (!accountFound && queryResult.rows.find(result => {
return result.username === values[0];
})) {
response.render('user/login', {'error': 'password'})
} else {
response.render('user/login', {'error': 'username'})
}
});

})

app.post('/users', (request, response) => {
const queryString = 'INSERT INTO users (username, password) VALUES ($1, $2)';
const values = [
request.body.username,
request.body.password
];
// execute query
pool.query(queryString, values, (error, queryResult) => {
//response.redirect('/');
response.send('user created');
});
});

app.get('/users/tweed', (request, response) => {
if (request.cookies.loggedIn === 'true'){
response.render('user/tweed', request.cookies);
} else {
response.render('user/noTweed');
}
});

app.post('/users/tweed', (request, response) => {
const queryString = 'INSERT INTO tweeds (tweed, user_id) VALUES ($1, $2)';
const values = [
request.body.tweed,
parseInt(request.cookies.id)
];
pool.query(queryString, values, (error, queryResult) => {
response.redirect('/');
});
});

/**
* ===================================
Expand All @@ -79,13 +140,13 @@ app.post('/users', (request, response) => {

const server = app.listen(3000, () => console.log('~~~ Tuning in to the waves of port 3000 ~~~'));

let onClose = function(){
let onClose = function () {

server.close(() => {
console.log('Process terminated')
pool.end( () => console.log('Shut down db connection pool'));
})
server.close(() => {
console.log('Process terminated')
pool.end(() => console.log('Shut down db connection pool'));
})
};

process.on('SIGTERM', onClose);
process.on('SIGINT', onClose);
process.on('SIGINT', onClose);
Loading