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

Denny's MVC TWEEDER PART 1 #121

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
11 changes: 4 additions & 7 deletions controllers/pokemon.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
module.exports = (db) => {

/**
* ===========================================
* Controller logic
* ===========================================
*/

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
Expand All @@ -21,5 +19,4 @@ module.exports = (db) => {
return {
index: indexControllerCallback,
};

}
};
15 changes: 15 additions & 0 deletions controllers/tweets.js
Original file line number Diff line number Diff line change
@@ -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,
};
};
89 changes: 20 additions & 69 deletions db.js
Original file line number Diff line number Diff line change
@@ -1,99 +1,50 @@
/*
* ===================================================
* ===================================================
* ===================================================
* ===================================================
* ====== CONFIGURATION =========
* ===================================================
* ===================================================
* ===================================================
* ===================================================
*/



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);
});

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

const pokemonModelsObject = allPokemonModelsFunction(pool);

/*
* ===================================================
* ===================================================
* ===================================================
* ===================================================
* ====== 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
*/
pool: pool,

// users: userModelsObject,
pokemon: pokemonModelsObject
pokemon: pokemonModelsObject,
tweets: tweetsModelObject,
};
27 changes: 5 additions & 22 deletions models/pokemon.js
Original file line number Diff line number Diff line change
@@ -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);

}
}
});
Expand Down
25 changes: 25 additions & 0 deletions models/tweets.js
Original file line number Diff line number Diff line change
@@ -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,
};
};
21 changes: 6 additions & 15 deletions routes.js
Original file line number Diff line number Diff line change
@@ -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);
};
24 changes: 24 additions & 0 deletions seed.sql
Original file line number Diff line number Diff line change
@@ -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!');
16 changes: 16 additions & 0 deletions tables.sql
Original file line number Diff line number Diff line change
@@ -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
);
4 changes: 3 additions & 1 deletion views/pokemon/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<html>
<head />
Expand Down
30 changes: 30 additions & 0 deletions views/tweets/showalltweets.jsx
Original file line number Diff line number Diff line change
@@ -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 (
<p>
{users.username} <br />
{users.tweets} <br />
</p>
);
});

return (
<html>
<head />
<body>
<h3>All Tweets</h3>
{showAllTweets}
</body>
</html>
);
}
}

module.exports = AllTweets;