diff --git a/controllers/tweedrC.js b/controllers/tweedrC.js new file mode 100644 index 0000000..20a503e --- /dev/null +++ b/controllers/tweedrC.js @@ -0,0 +1,28 @@ +module.exports = (db) => { + + /** + * =========================================== + * Controller logic + * =========================================== + */ + + let index = (request, response) => { + db.tweedr.getAllTweets((error, allTweets) => { + response.render('testpage', { + allTweets + }); + }); + }; + + + /** + * =========================================== + * Export controller functions as a module + * =========================================== + */ + + return { + index, + }; + +} \ No newline at end of file diff --git a/db.js b/db.js new file mode 100644 index 0000000..ee44e30 --- /dev/null +++ b/db.js @@ -0,0 +1,52 @@ +const pg = require('pg'); +const tweedr = require('./models/tweedrM'); +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: 'cash', + host: '127.0.0.1', + database: 'testdb', + port: 5432 + }; +} + + +const pool = new pg.Pool(configs); + +pool.on('error', function (err) { + console.log('idle client error', err.message, err.stack); +}); + +module.exports = { + /* + * ADD APP MODELS HERE + */ + tweedr: tweedr(pool), + + + //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 +}; \ No newline at end of file diff --git a/index.js b/index.js index 4d5d595..3a84c3f 100644 --- a/index.js +++ b/index.js @@ -11,16 +11,16 @@ const pg = require('pg'); */ const configs = { - user: 'akira', - host: '127.0.0.1', - database: 'testdb', - port: 5432, + user: 'cash', + host: '127.0.0.1', + database: 'testdb', + port: 5432, }; 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 @@ -30,7 +30,7 @@ const app = express(); app.use(methodOverride('_method')); app.use(cookieParser()); app.use(express.urlencoded({ - extended: true + extended: true })); // Set react-views to be the default view engine @@ -45,27 +45,94 @@ app.engine('jsx', reactEngine); * =================================== */ -// Root GET request (it doesn't belong in any controller file) -app.get('/', (request, response) => { - response.send('Welcome To Tweedr.'); +// Root GET req (it doesn't belong in any controller file) +app.get('/', (req, res) => { + let loggedin = req.cookies['loggedin']; + if (loggedin == undefined) { + res.render('main') + } else { + let tempArr = []; + let getusers = `select * from users;` + let gettweets = `select * from tweets;` + let getfollows = `select * from getfollows;` + pool.query(getusers, (err, result1) => { + tempArr.push(result1.rows); + }) + pool.query(gettweets, (err, result2) => { + tempArr.push(result2.rows); + }) + pool.query(getfollows, (err, result3) => { + tempArr.push(result3.rows); + }) + let passObj = { + tempArr + } + res.render('loggedmain', { + passObj: passObj + }); + }; }); -app.get('/users/new', (request, response) => { - response.render('user/newuser'); +app.post('/login', (req, res) => { + let testlogin = `select * from users where username='${req.body.username}';` + pool.query(testlogin, (err, result) => { + if (result.rows.length == 0) { + console.log(req.body.username) + console.log('username error') + res.render('loginerror'); + } else { + if (result.rows[0].password == req.body.password) { + let tempArr = []; + tempArr.push(result.rows) + let getusers = `select * from users;` + let gettweets = `select * from tweets;` + let getfollows = `select * from getfollows;` + pool.query(getusers, (err1, result1) => { + tempArr.push(result1.rows); + pool.query(gettweets, (err2, result2) => { + tempArr.push(result2.rows); + pool.query(getfollows, (err3, result3) => { + tempArr.push(result3.rows); + }) + }) + }) + let passObj = { + tempArr + } + res.render('loggedmain', { + passObj: passObj + }); + } else { + console.log('password error') + res.render('loginerror'); + }; + }; + }); +}); + +app.get('/users/new', (req, res) => { + res.render('createacc'); }); -app.post('/users', (request, response) => { - const queryString = 'INSERT INTO users (name, password) VALUES ($1, $2)'; +app.post('/users', (req, res) => { + + const queryString = 'INSERT INTO users (username, firstname, lastname, password, email) VALUES ($1, $2, $3, $4, $5)'; const values = [ - request.body.name, - request.body.password + req.body.username, + req.body.firstname, + req.body.lastname, + req.body.password, + req.body.email ]; - // execute query pool.query(queryString, values, (error, queryResult) => { - //response.redirect('/'); - response.send('user created'); + //res.redirect('/'); + if (error) { + console.log(error.stack) + } + console.log('user created'); + res.render('main'); }); }); @@ -73,19 +140,19 @@ app.post('/users', (request, response) => { /** * =================================== - * Listen to requests on port 3000 + * Listen to reqs on port 3000 * =================================== */ 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); \ No newline at end of file diff --git a/models/tweedrM.js b/models/tweedrM.js new file mode 100644 index 0000000..c60abc9 --- /dev/null +++ b/models/tweedrM.js @@ -0,0 +1,39 @@ +/** + * =========================================== + * Export model functions as a module + * =========================================== + */ +module.exports = (dbPoolInstance) => { + + // `dbPoolInstance` is accessible within this function scope + + let getAllTweets = (callback) => { + // const values = [type_id]; + let query = 'SELECT * FROM tweets;'; + + dbPoolInstance.query(query, (error, queryResult) => { + if (error) { + + // invoke callback function with results after query has executed + callback(error, null); + + } else { + + // invoke callback function with results after query has executed + + if (queryResult.rows.length > 0) { + callback(null, queryResult.rows); + + } else { + callback(null, null); + + } + } + }); + }; + + return { + getAllTweets, + //get, + }; +}; \ No newline at end of file diff --git a/newindex.js b/newindex.js new file mode 100644 index 0000000..c5c3783 --- /dev/null +++ b/newindex.js @@ -0,0 +1,65 @@ +const express = require('express'); +const methodOverride = require('method-override'); +const cookieParser = require('cookie-parser'); +const db = require('./db'); + +/** + * =================================== + * Configurations and set up + * =================================== + */ + +// Init express app +const app = express(); + +// Set up middleware +app.use(methodOverride('_method')); +app.use(cookieParser()); +app.use(express.urlencoded({ + extended: true +})); + +// 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); + +/** + * =================================== + * Routes + * =================================== + */ + +// Import routes to match incoming requests +require('./routes')(app, db); + +// Root GET request (it doesn't belong in any controller file) +app.get('/', (request, response) => { + response.render('home'); +}); + +// Catch all unmatched requests and return 404 not found page +app.get('*', (request, response) => { + response.render('notfound'); +}); + +/** + * =================================== + * 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 + ' ~~~')); + +let onClose = function () { + + server.close(() => { + console.log('Process terminated') + db.pool.end(() => console.log('Shut down db connection pool')); + }) +}; + +process.on('SIGTERM', onClose); +process.on('SIGINT', onClose); \ No newline at end of file diff --git a/removetables.sql b/removetables.sql new file mode 100644 index 0000000..e69de29 diff --git a/routes.js b/routes.js new file mode 100644 index 0000000..f0d3293 --- /dev/null +++ b/routes.js @@ -0,0 +1,12 @@ +module.exports = (app, db) => { + + const tweedr = require('./controllers/tweedrC')(db); + + /* + * ========================================= + * Routes for one controller + * ========================================= + */ + + app.get('/', tweedr.index); +}; \ No newline at end of file diff --git a/seed.sql b/seed.sql new file mode 100644 index 0000000..5cff39a --- /dev/null +++ b/seed.sql @@ -0,0 +1,33 @@ +INSERT INTO users (firstname, lastname, username, password, email) VALUES ('Cash', 'Teo', 'kingcashthefifth', '12345678', 'kingcashthefifth@gmail.com'); +INSERT INTO users (firstname, lastname, username, password, email) VALUES ('Zuei', 'Tam', 'zueilababy', '12345678', 'zueilababy@gmail.com'); +INSERT INTO users (firstname, lastname, username, password, email) VALUES ('Beboo', 'Teo', 'princebeboo', '12345678', 'princebeboo@gmail.com'); +INSERT INTO users (firstname, lastname, username, password, email) VALUES ('David', 'Ng', 'davidng', '12345678', 'davidng@gmail.com'); +INSERT INTO users (firstname, lastname, username, password, email) VALUES ('Teng', 'Teng', 'tengtengteng', '12345678', 'tengtengteng@gmail.com'); + + + +INSERT INTO tweets (tweet, user_id) VALUES ('Hi is anybody there?', 1); +INSERT INTO tweets (tweet, user_id) VALUES ('Passion for code still burning strong!', 1); +INSERT INTO tweets (tweet, user_id) VALUES ('I don''t understand what''s so great about programming', 2); +INSERT INTO tweets (tweet, user_id) VALUES ('I hate using the computer!', 2); +INSERT INTO tweets (tweet, user_id) VALUES ('Shopping is my passion', 2); +INSERT INTO tweets (tweet, user_id) VALUES ('I love IU', 3); +INSERT INTO tweets (tweet, user_id) VALUES ('I hope I get a snack from my master today!', 3); +INSERT INTO tweets (tweet, user_id) VALUES ('Bark Bark Bark!', 3); +INSERT INTO tweets (tweet, user_id) VALUES ('Woof! Woof!', 3); + + + +INSERT INTO follows (user_id, following_id) VALUES (1, 2); +INSERT INTO follows (user_id, following_id) VALUES (1, 3); +INSERT INTO follows (user_id, following_id) VALUES (1, 4); +INSERT INTO follows (user_id, following_id) VALUES (1, 5); +INSERT INTO follows (user_id, following_id) VALUES (2, 1); +INSERT INTO follows (user_id, following_id) VALUES (2, 3); +INSERT INTO follows (user_id, following_id) VALUES (2, 4); +INSERT INTO follows (user_id, following_id) VALUES (3, 1); +INSERT INTO follows (user_id, following_id) VALUES (3, 2); +INSERT INTO follows (user_id, following_id) VALUES (5, 1); +INSERT INTO follows (user_id, following_id) VALUES (1, 4); +INSERT INTO follows (user_id, following_id) VALUES (4, 1); +INSERT INTO follows (user_id, following_id) VALUES (4, 2); \ No newline at end of file diff --git a/tables.sql b/tables.sql new file mode 100644 index 0000000..43c9d15 --- /dev/null +++ b/tables.sql @@ -0,0 +1,25 @@ +CREATE TABLE IF NOT EXISTS users ( + id SERIAL PRIMARY KEY, + firstname VARCHAR(25), + lastname VARCHAR(25), + username VARCHAR(25), + password VARCHAR(25), + email VARCHAR(50), + propic VARCHAR(200) +); + +CREATE TABLE IF NOT EXISTS tweets ( + id SERIAL PRIMARY KEY, + tweet VARCHAR(150), + tweetpic VARCHAR(200), + tweetvid VARCHAR(200), + tweettime TIMESTAMP DEFAULT now(), + user_id INTEGER +); + +CREATE TABLE IF NOT EXISTS follows ( + id SERIAL PRIMARY KEY, + user_id INTEGER, + following_id INTEGER +); + diff --git a/views/createacc.jsx b/views/createacc.jsx new file mode 100644 index 0000000..70cee20 --- /dev/null +++ b/views/createacc.jsx @@ -0,0 +1,152 @@ +var React = require("react"); + +class Createacc extends React.Component { + render() { + return ( + +
+ + + + +{obj.tweet}
+ ) + }) + return ( + + + + + + +Not Twitter, geez...
+Not Twitter, geez...
+{obj.tweet}
+ // ) + // }) + + const allTweets = this.props.allTweets.map((obj, index) => { + return ( +{obj.tweet}
+ ) + }) + return ( + + + + + + +