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-jarryl-chia #129

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

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

let seeAllTweeds = (req, res) => {
if (req.cookies['loggedIn']) {
db.tweeds.getAll((err, result) => {
res.send(result.rows)
})
} else {
res.render('login')
}
}

let newTweed = (req, res) => {
if (!req.cookies['loggedIn']) {
res.render('login')
} else {
res.render('new')
}
}

let postTweed = (req, res) => {
let values = [req.body.tweed, req.cookies["userID"]]
db.tweeds.addTweed(values, (err, result) => {
res.redirect("/")
})
}

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

let userCheck = (req, res) => {
let values = [req.body['username'], req.body['password']];
db.users.authenticateUser(values, (err, result) => {
if (err) {
res.send(err.stack)
} else {
if (result) {
res.cookie('loggedIn', true)
res.cookie('userID', result.rows[0]["id"])
res.redirect("/")
} else {
res.send("Invalid username or password")
}
}
})
}

return {
userCheck
}
}
14 changes: 8 additions & 6 deletions db.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ if( process.env.DATABASE_URL ){

}else{
configs = {
user: 'akira',
user: 'jarryl',
host: '127.0.0.1',
database: 'testdb',
port: 5432
Expand Down Expand Up @@ -62,10 +62,11 @@ pool.on('error', function (err) {
*/


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

const pokemonModelsObject = allPokemonModelsFunction( pool );
const usersModels = require('./models/users');
const tweedsModels = require('./models/tweeds');

const usersModelsObj = usersModels(pool);
const tweedsModelsObj = tweedsModels(pool);


/*
Expand Down Expand Up @@ -95,5 +96,6 @@ module.exports = {
*/

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

This file was deleted.

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

let getAll = (callback) => {
dbPoolInstance
.query('SELECT tweeds.tweed, users.username FROM tweeds INNER JOIN users ON tweeds.user_id = users.id;')
.then(result => callback(null, result))
.catch(err => callback(err, null))
}

let addTweed = (values, callback) => {
dbPoolInstance.query('INSERT INTO tweeds (tweed, user_id) VALUES ($1, $2);', values, (err, result) => {
if (err) {
console.log(err)
} else {
callback(null, null)
}
})
}
return {
getAll,
addTweed
}
}
20 changes: 20 additions & 0 deletions models/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module.exports = (dbPoolInstance) => {
console.log("users models accessed");

let authenticateUser = (values, callback) => {
dbPoolInstance.query("SELECT * FROM users WHERE username = $1 AND password = $2;", values, (err, result) => {
if (err) {
callback(err, null)
} else {
if (result.rowCount > 0) {
callback(null, result)
} else {
callback(null, null)
}
}
})
}
return {
authenticateUser
}
}
Loading