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

SEI-24 Joey #126

Open
wants to merge 2 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
25 changes: 0 additions & 25 deletions controllers/pokemon.js

This file was deleted.

10 changes: 10 additions & 0 deletions controllers/tweed_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = (db) => {



return {

};

}

82 changes: 82 additions & 0 deletions controllers/tweedr_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
module.exports = (db) => {

let welcome = (req, res) => {
res.render('index');
}

let login = (req, res) => {
res.render('login');
}

let verifyInfo = (req, res) => {

let username = req.body.username;
let password = req.body.password;

db.tweedr.getVerifyInfo(username, password, (err, result) => {
if (err) {
console.log('error at tweedr_controller, verifyInfo_cb ---', err.messsage);
}
else {
if (result == 'wrong username') {
res.send('user does not exist');
}
else if (result == 'wrong password') {
res.send('incorrect password');
}
else {
res.cookie('username', result.rows[0].username);
res.cookie('user_id', result.rows[0].id);
res.cookie('loggedIn', true);
res.redirect(`/${result.rows[0].id}`);
}
}
})
}

let register = (req, res) => {
res.render('register');
}

let registerInfo = (req, res) => {

let username = req.body.username;
let password = req.body.password;

db.tweedr.getRegisterInfo(username, password, (err, result) => {
if (err) {
console.log('error at tweedr_controller, registerInfo ---', err.message);
}
else {
res.cookie('username', result.username);
res.cookie('user_id', result.id);
res.cookie('loggedIn', true);
res.send('registration successful');
}
})
}

let userTweeds = (req, res) => {

let id = req.cookies.user_id;

db.tweedr.getUserTweeds(id, (err, result) => {
if (err) {
console.log('error at tweedr_controller, userTweeds ---', err.message);
}
else {
res.render('user', result);
}
})
}

return {
welcome,
login,
verifyInfo,
register,
registerInfo,
userTweeds
};

}
128 changes: 33 additions & 95 deletions db.js
Original file line number Diff line number Diff line change
@@ -1,99 +1,37 @@
/*
* ===================================================
* ===================================================
* ===================================================
* ===================================================
* ====== CONFIGURATION =========
* ===================================================
* ===================================================
* ===================================================
* ===================================================
*/



const pg = require('pg');
const url = require('url');

var configs;

if( process.env.DATABASE_URL ){

const params = url.parse(process.env.DATABASE_URL);
const auth = params.auth.split(':');

configs = {
user: auth[0],
password: auth[1],
host: params.hostname,
port: params.port,
database: params.pathname.split('/')[1],
ssl: true
};

}else{
configs = {
user: 'akira',
const configs = {
user: 'wongjoey',
host: '127.0.0.1',
database: 'testdb',
port: 5432
database: 'tweedr',
port: 5432,
};

const pool = new pg.Pool(configs);

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

const tweedrModelsFunction = require('./models/tweedr_model');
const tweedModelsFunction = require('./models/tweed_model');

const poolTweedrModelsObject = tweedrModelsFunction(pool);
const poolTweedModelsObject = tweedModelsFunction(pool);

module.exports = {
//make queries directly from here
queryInterface: (text, params, callback) => {
return pool.query(text, params, callback);
},

// get a reference to end the connection pool at server end
pool:pool,

/*
* ADD APP MODELS HERE
*/

tweedr: poolTweedrModelsObject,
tweed: poolTweedModelsObject
};
}


const pool = new pg.Pool(configs);

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



/*
* ===================================================
* ===================================================
* ===================================================
* ===================================================
* ====== REQUIRE MODEL FILES =========
* ===================================================
* ===================================================
* ===================================================
* ===================================================
*/


const allPokemonModelsFunction = require('./models/pokemon');

const pokemonModelsObject = allPokemonModelsFunction( pool );



/*
* ===================================================
* ===================================================
* ===================================================
* ===================================================
* ====== MODULE EXPORTS =========
* ===================================================
* ===================================================
* ===================================================
* ===================================================
*/


module.exports = {
//make queries directly from here
queryInterface: (text, params, callback) => {
return pool.query(text, params, callback);
},

// get a reference to end the connection pool at server end
pool:pool,

/*
* ADD APP MODELS HERE
*/

// users: userModelsObject,
pokemon: pokemonModelsObject
};
57 changes: 13 additions & 44 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,76 +2,45 @@ const express = require('express');
const methodOverride = require('method-override');
const cookieParser = require('cookie-parser');

/**
* ===================================
* Configurations and set up
* ===================================
*/

// Init express app
const app = express();

// Set up middleware
app.use(methodOverride('_method'));

app.use(cookieParser());

app.use(express.static('public'));

app.use(express.json());

app.use(express.urlencoded({
extended: true
}));

app.use(methodOverride('_method'));

app.use(cookieParser());

// Set react-views to be the default view engine
const reactEngine = require('express-react-views').createEngine();

app.set('views', __dirname + '/views');
app.set('view engine', 'jsx');
app.engine('jsx', reactEngine);

/**
* ===================================
* ===================================
* DB
* ===================================
* ===================================
*/

// db contains *ALL* of our models
const allModels = require('./db');

/**
* ===================================
* ===================================
* Routes
* ===================================
* ===================================
*/

// get the thing that contains all the routes
const setRoutesFunction = require('./routes');

// call it and pass in the "app" so that we can set routes on it (also models)
setRoutesFunction(app, allModels);

/**
* ===================================
* Listen to requests on port 3000
* ===================================
*/
const PORT = process.env.PORT || 3000;

const server = app.listen(PORT, () => console.log('~~~ Tuning in to the waves of port '+PORT+' ~~~'));
// LISTENING TO REQUESTS ON PORT 3000
const server = app.listen(3000, () => console.log('~~~ Tuning in to the waves of port 3000 ~~~'));

let onClose = function(){


console.log("closing");

server.close(() => {
console.log('Process terminated')

console.log('Process terminated');

allModels.pool.end( () => console.log('Shut down db connection pool'));
})
};

process.on('SIGTERM', onClose);
process.on('SIGINT', onClose);
process.on('SIGINT', onClose);
38 changes: 0 additions & 38 deletions models/pokemon.js

This file was deleted.

9 changes: 9 additions & 0 deletions models/tweed_model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = (dbPoolInstance) => {



return {

};
};

Loading