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 Ray #125

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
25 changes: 0 additions & 25 deletions controllers/pokemon.js

This file was deleted.

88 changes: 88 additions & 0 deletions controllers/twitter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const { response } = require("express");

module.exports = (db) => {

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

let indexControllerCallback = (request, response) => {
db.twitter.getAll((error, allTweets) => {
response.render('./twitter/login', { allTweets });
});
};

let register =(request,response)=>{
let name = request.body.name;
let pw = request.body.pw;
db.twitter.getRegister(name,pw,(error,result)=>{
response.redirect('/')
})
}

let login =(request,res)=>{
let name = request.body.name;
let pw = request.body.pw;
db.twitter.getLogin(name,pw,(err,result)=>{
if (result === "KEY IN SOMETHING THAT WORKS RETARD" || result === "ERROR YOUR FACE"){
res.status(404).send(result)
} else{
res.cookie('username',result.username);
res.cookie('id',result.id);
res.cookie('logIn', 'true');
res.redirect('/home')
}
})
}

let logout = (request,response) =>{
console.log('oing');
response.clearCookie('username');
response.clearCookie('id');
response.cookie('logIn','false');
response.send('GO TO HELL')
}

let tweet = (request,response) =>{
let tweet = request.body.tweets;
let id = request.cookies.id;
db.twitter.getTweet(tweet,id,(err,result)=>{
if (err){
console.log("err in tweet controller", err.message)
} else {
response.redirect('/home')
}
})
}

let home = (request,response)=>{
if(request.cookies.logIn === 'true'){
let id = request.cookies.id
db.twitter.getHome(id,(err,result)=>{
if (err){
response.status(404).send('fucking hell')
} else {response.render('./twitter/home',result)}
})
} else{
response.send("DO YOU KNOW HOW TO LOG IN????")
}
}

/**
* ===========================================
* Export controller functions as a module
* ===========================================
*/
return {
index: indexControllerCallback,
register,
login:login,
logout,
home,
tweet,

};

}
10 changes: 5 additions & 5 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: 'postgres',
host: '127.0.0.1',
database: 'testdb',
database: 'twdr',
port: 5432
};
}
Expand All @@ -62,9 +62,9 @@ pool.on('error', function (err) {
*/


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

const pokemonModelsObject = allPokemonModelsFunction( pool );
const twitterModelsObject = allTwitterModelsFunction( pool );



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

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

This file was deleted.

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

// `dbPoolInstance` is accessible within this function scope

let getAll = (callback) => {

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

}
}
});
};

let getRegister = (name,pw,callback)=>{
console.log(name)
let query = `INSERT INTO users (username,pw) VALUES ('${name}','${pw}')`
dbPoolInstance.query(query, (err,res)=>{
if(err){
console.log("err in getLogin models", err.message)
} else {
callback(err,res)
}
})
}

let getLogin = (name,pw,callback) =>{
console.log(pw)
let query = `SELECT * FROM users where username = '${name}'`
dbPoolInstance.query(query,(err,queryResult)=>{
if(err){
console.log('test')
callback(err,null)
} else {
if( queryResult.rows.length<1){
console.log('hello1')
callback(err,"KEY IN SOMETHING THAT WORKS RETARD")
} else{
if(queryResult.rows[0].pw !== `${pw}`){
console.log('hello')
callback(err,"ERROR YOUR FACE")
} else{callback(err,queryResult.rows[0]);}
}
}
})
}

let getTweet = (tweet,id,callback) =>{
let query = `INSERT INTO tweets (tweets,user_id) VALUES ($1,$2)`
values = [tweet,id]
dbPoolInstance.query(query,values,(err,results)=>{
callback(err,results)
})
}

let getHome = (id,callback) =>{
let query = `SELECT users.id, tweets.tweets FROM users INNER JOIN tweets ON (users.id = tweets.user_id) WHERE users.id = $1`
values = [id]
dbPoolInstance.query(query,values,(err,result)=>{
callback(err,result)
})
}

return {
getAll,
getRegister,
getLogin,
getHome,
getTweet
};
};
Loading