From 828074da4380fe5846741692bca198fdbabde69e Mon Sep 17 00:00:00 2001 From: Denny Koh Date: Fri, 24 Apr 2020 00:07:10 +0800 Subject: [PATCH 1/2] Started doing homework --- db.js | 45 +++++++++++++++++---------------------------- 1 file changed, 17 insertions(+), 28 deletions(-) diff --git a/db.js b/db.js index 7b48a9e..8da4e3a 100644 --- a/db.js +++ b/db.js @@ -10,45 +10,38 @@ * =================================================== */ - - -const pg = require('pg'); -const url = require('url'); +const pg = require("pg"); +const url = require("url"); var configs; -if( process.env.DATABASE_URL ){ - +if (process.env.DATABASE_URL) { const params = url.parse(process.env.DATABASE_URL); - const auth = params.auth.split(':'); + 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 + database: params.pathname.split("/")[1], + ssl: true, }; - -}else{ +} else { configs = { - user: 'akira', - host: '127.0.0.1', - database: 'testdb', - port: 5432 + user: "jasminelee", + host: "127.0.0.1", + database: "tweeder", + port: 5432, }; } - const pool = new pg.Pool(configs); -pool.on('error', function (err) { - console.log('idle client error', err.message, err.stack); +pool.on("error", function (err) { + console.log("idle client error", err.message, err.stack); }); - - /* * =================================================== * =================================================== @@ -61,12 +54,9 @@ pool.on('error', function (err) { * =================================================== */ +const allPokemonModelsFunction = require("./models/pokemon"); -const allPokemonModelsFunction = require('./models/pokemon'); - -const pokemonModelsObject = allPokemonModelsFunction( pool ); - - +const pokemonModelsObject = allPokemonModelsFunction(pool); /* * =================================================== @@ -80,7 +70,6 @@ const pokemonModelsObject = allPokemonModelsFunction( pool ); * =================================================== */ - module.exports = { //make queries directly from here queryInterface: (text, params, callback) => { @@ -88,12 +77,12 @@ module.exports = { }, // get a reference to end the connection pool at server end - pool:pool, + pool: pool, /* * ADD APP MODELS HERE */ // users: userModelsObject, - pokemon: pokemonModelsObject + pokemon: pokemonModelsObject, }; From f9c8f91af6dbf91dd286bc90417e1a6ee7ef987c Mon Sep 17 00:00:00 2001 From: Denny Koh Date: Fri, 24 Apr 2020 01:56:19 +0800 Subject: [PATCH 2/2] Still doing Part 1 of MVC , just finished displaying all tweets with username --- controllers/pokemon.js | 11 +++----- controllers/tweets.js | 15 +++++++++++ db.js | 46 +++------------------------------- models/pokemon.js | 27 ++++---------------- models/tweets.js | 25 ++++++++++++++++++ routes.js | 21 +++++----------- seed.sql | 24 ++++++++++++++++++ tables.sql | 16 ++++++++++++ views/pokemon/index.jsx | 4 ++- views/tweets/showalltweets.jsx | 30 ++++++++++++++++++++++ 10 files changed, 132 insertions(+), 87 deletions(-) create mode 100644 controllers/tweets.js create mode 100644 models/tweets.js create mode 100644 views/tweets/showalltweets.jsx diff --git a/controllers/pokemon.js b/controllers/pokemon.js index fcd9fac..37306ff 100644 --- a/controllers/pokemon.js +++ b/controllers/pokemon.js @@ -1,5 +1,4 @@ module.exports = (db) => { - /** * =========================================== * Controller logic @@ -7,12 +6,11 @@ module.exports = (db) => { */ let indexControllerCallback = (request, response) => { - db.pokemon.getAll((error, allPokemon) => { - response.render('pokemon/index', { allPokemon }); - }); + db.pokemon.getAll((error, allPokemon) => { + response.render("pokemon/index", { allPokemon }); + }); }; - /** * =========================================== * Export controller functions as a module @@ -21,5 +19,4 @@ module.exports = (db) => { return { index: indexControllerCallback, }; - -} +}; diff --git a/controllers/tweets.js b/controllers/tweets.js new file mode 100644 index 0000000..71bcf4a --- /dev/null +++ b/controllers/tweets.js @@ -0,0 +1,15 @@ +module.exports = (db) => { + let showAllTweetsControllerCallback = (request, response) => { + db.tweets.showAll((error, allTweets) => { + const data = { + allTweets, + }; + console.log(allTweets); + response.render("tweets/showalltweets", data); + }); + }; + + return { + index: showAllTweetsControllerCallback, + }; +}; diff --git a/db.js b/db.js index 8da4e3a..0168d3f 100644 --- a/db.js +++ b/db.js @@ -1,15 +1,3 @@ -/* - * =================================================== - * =================================================== - * =================================================== - * =================================================== - * ====== CONFIGURATION ========= - * =================================================== - * =================================================== - * =================================================== - * =================================================== - */ - const pg = require("pg"); const url = require("url"); @@ -42,47 +30,21 @@ 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 ========= - * =================================================== - * =================================================== - * =================================================== - * =================================================== - */ +const allTweetsModelsFunction = require("./models/tweets"); + +const tweetsModelObject = allTweetsModelsFunction(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 - */ - - // users: userModelsObject, pokemon: pokemonModelsObject, + tweets: tweetsModelObject, }; diff --git a/models/pokemon.js b/models/pokemon.js index 766cf1b..3923623 100644 --- a/models/pokemon.js +++ b/models/pokemon.js @@ -1,32 +1,15 @@ -/** - * =========================================== - * Export model functions as a module - * =========================================== - */ module.exports = (dbPoolInstance) => { - - // `dbPoolInstance` is accessible within this function scope - let getAll = (callback) => { - - let query = 'SELECT * FROM pokemons'; + let query = "SELECT * FROM pokemons"; dbPoolInstance.query(query, (error, queryResult) => { - if( error ){ - - // invoke callback function with results after query has executed + if (error) { callback(error, null); - - }else{ - - // invoke callback function with results after query has executed - - if( queryResult.rows.length > 0 ){ + } else { + if (queryResult.rows.length > 0) { callback(null, queryResult.rows); - - }else{ + } else { callback(null, null); - } } }); diff --git a/models/tweets.js b/models/tweets.js new file mode 100644 index 0000000..aada30c --- /dev/null +++ b/models/tweets.js @@ -0,0 +1,25 @@ +module.exports = (dbPoolInstance) => { + let showAll = (callback) => { + let query = + "SELECT tweets.tweets, users.username FROM tweets INNER JOIN users ON (tweets.tweets_userid = users.id)"; + + dbPoolInstance.query(query, (error, queryResult) => { + if (error) { + console.log("error!"); + callback(error, null); + } else { + console.log("this is working!"); + + if (queryResult.rows.length > 0) { + callback(null, queryResult.rows); + } else { + callback(null, null); + } + } + }); + }; + + return { + showAll, + }; +}; diff --git a/routes.js b/routes.js index efdee08..7194a29 100644 --- a/routes.js +++ b/routes.js @@ -1,20 +1,11 @@ module.exports = (app, allModels) => { + const pokemonControllerCallbacks = require("./controllers/pokemon")( + allModels + ); + const tweetsControllerCallbacks = require("./controllers/tweets")(allModels); - /* - * ========================================= - * ========================================= - * ========================================= - * ========================================= - * ALL ROUTES FOR POKEMON CONTROLLER - * ========================================= - * ========================================= - * ========================================= - */ + app.get("/pokemons", pokemonControllerCallbacks.index); - // require the controller - const pokemonControllerCallbacks = require('./controllers/pokemon')(allModels); - - app.get('/pokemons', pokemonControllerCallbacks.index); - //app.get('/pokemons/:id', pokemons.getPokemon); + app.get("/", tweetsControllerCallbacks.index); }; diff --git a/seed.sql b/seed.sql index e69de29..ff1bc92 100644 --- a/seed.sql +++ b/seed.sql @@ -0,0 +1,24 @@ +--TWEETS + +INSERT INTO tweets + (tweets , tweets_userid) +VALUES + ('Today COVID-19 cases is extremely high and worrying' , 1); + +INSERT INTO tweets + (tweets , tweets_userid) +VALUES + ('SEI-22 is extremely interesting!' , 2); + +-- USERS + +-- INSERT INTO users +-- (username , password , user_intro) +-- VALUES +-- ('Denny' , '123456' , 'I am so fascinated by what coding can do!'); + + +-- INSERT INTO users +-- (username , password , user_intro) +-- VALUES +-- ('KOK CHUAN' , '123456' , 'I just finished CRASH LANDING ON YOU!'); \ No newline at end of file diff --git a/tables.sql b/tables.sql index e69de29..cce0e22 100644 --- a/tables.sql +++ b/tables.sql @@ -0,0 +1,16 @@ +CREATE TABLE +IF NOT EXISTS tweets +( + ID SERIAL PRIMARY KEY, + tweets TEXT, + tweets_userid INTEGER +); + +CREATE TABLE +IF NOT EXISTS users +( + id SERIAL PRIMARY KEY, + username TEXT, + password TEXT, + user_intro TEXT +); \ No newline at end of file diff --git a/views/pokemon/index.jsx b/views/pokemon/index.jsx index b0c8382..4e306da 100644 --- a/views/pokemon/index.jsx +++ b/views/pokemon/index.jsx @@ -2,7 +2,9 @@ var React = require("react"); class Home extends React.Component { render() { - console.log(this.props.types); + console.log("========================"); + console.log(123456); + console.log("========================"); return ( diff --git a/views/tweets/showalltweets.jsx b/views/tweets/showalltweets.jsx new file mode 100644 index 0000000..14fba5d --- /dev/null +++ b/views/tweets/showalltweets.jsx @@ -0,0 +1,30 @@ +var React = require("react"); + +class AllTweets extends React.Component { + render() { + console.log("================================"); + console.log(this.props.allTweets); + console.log("================================"); + + const showAllTweets = this.props.allTweets.map((users) => { + return ( +

+ {users.username}
+ {users.tweets}
+

+ ); + }); + + return ( + + + +

All Tweets

+ {showAllTweets} + + + ); + } +} + +module.exports = AllTweets;