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 Vincent #124

Open
wants to merge 1 commit 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.

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

let userPage = (req, res) => {
let user = req.params.user;

db.tweeds.userHomepage(user, (err, result) => {
if (err) {
console.log("-- Error in userHomepage controller", err.message)
} else {
if (req.cookies.loggedIn === "true" && req.cookies.username === user) {
res.render('tweeds/user_homepage', result)
} else {
res.send("PLEASE LOG IN")
}
}
})
}

return {
userPage
}
};
61 changes: 61 additions & 0 deletions controllers/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
module.exports = (db) => {

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

let welcome = (req, res) => {
if (req.cookies.loggedIn === 'true') {
// res.send("WORKING")
res.redirect(`tweed/${req.cookies.username}`)
} else {
res.render('users/homepage')
}
}

let loginPage = (req, res) => {
res.clearCookie('username');
res.clearCookie('loggedIn');
res.render('users/login');
};

let login = (req, res) => {
let returnInfo = req.body;
let username = returnInfo.username;
let password = returnInfo.password;
db.users.userLogin(username, password, (err, result) => {
if (err) {
console.log("-- Error in login controller", err.message)
} else {
if (result === false) {
res.send("User not exist! Please try again")
} else if (result === "wrong password") {
res.send("Wrong Password!")
} else {
res.cookie('username', result.username);
res.cookie('loggedIn', true)
res.redirect(`tweed/${result.username}`)
}
}
})
};






/**
* ===========================================
* Export controller functions as a module
* ===========================================
*/
return {
welcome,
loginPage,
login,
};

}
17 changes: 10 additions & 7 deletions db.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ if( process.env.DATABASE_URL ){

}else{
configs = {
user: 'akira',
user: 'vincent',
host: '127.0.0.1',
database: 'testdb',
database: 'tweedr',
port: 5432
};
}
Expand All @@ -62,9 +62,13 @@ pool.on('error', function (err) {
*/


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

const pokemonModelsObject = allPokemonModelsFunction( pool );
const userModelsObject = allUserModelsFunction(pool);

const allTweedModelsFunction = require('./models/tweeds');

const tweedModelsObject = allTweedModelsFunction(pool);



Expand Down Expand Up @@ -93,7 +97,6 @@ module.exports = {
/*
* ADD APP MODELS HERE
*/

// users: userModelsObject,
pokemon: pokemonModelsObject
users: userModelsObject,
tweeds: tweedModelsObject
};
38 changes: 0 additions & 38 deletions models/pokemon.js

This file was deleted.

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

let userHomepage = (user, callback) => {

let query = `select users.username, tweets.tweet from tweets inner join users on tweets.users_id = users.id where users.username = '${user}'`

dbPoolInstance.query(query, (queryErr, queryResult) => {
if (queryErr) {
console.log("-- Error in userHomeoage model", queryErr.message)
} else {
// console.log(queryResult)
callback(null, queryResult)
}
})
}

return {
userHomepage
}
}
34 changes: 34 additions & 0 deletions models/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* ===========================================
* Export model functions as a module
* ===========================================
*/
module.exports = (dbPoolInstance) => {

// `dbPoolInstance` is accessible within this function scope

let userLogin = (enteredUsername, enteredPassword, callback) => {
let query = `select * from users where username=$1`;
let values = [enteredUsername];
dbPoolInstance.query(query, values, (queryErr, queryResult) => {
if (queryErr) {
console.log("-- Error in userLogin model", queryErr.message)
} else {
if (queryResult.rows.length < 1) {
callback(null, false)
} else {
if (queryResult.rows[0].password !== enteredPassword) {
callback(null, "wrong password")
} else {
callback(null, queryResult.rows[0])
}
}
}
})
};

return {
userLogin,

};
};
Loading