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

Done previous stuff #40

Open
wants to merge 4 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
208 changes: 208 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
const express = require('express');

const pg = require('pg');

const configs = {
user: 'akira',
host: '127.0.0.1',
database: 'testdb',
port: 5432,
};

const pool = new pg.Pool(configs);

pool.on('error', function (err) {
console.log('idle client error', err.message, err.stack);
});

const app = express();

app.use(express.json());
app.use(express.urlencoded({
extended: true
}));


var cookieParser = require('cookie-parser')
app.use(cookieParser())


// Set react-views to be the default view engine
const reactEngine = require('express-react-views').createEngine();
app.set('views', __dirname + '/views');
app.set('view engine', 'jsx');
app.engine('jsx', reactEngine);


////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////

app.get('/user/new', (request, response) => {
response.render('newuser');
})

app.post('/user', (request, response) => {


console.log(request.body);

const values = [
request.body.name,
request.body.password
];

let query = "INSERT INTO users (name, password) VALUES ($1, $2)";

pool.query(query, values, (err, queryResponse) => {

response.cookie('loggedin', 'true');

response.send('hellooooo');
})

})

app.get('/profile', (request, response) => {
var loggedin = request.cookies['loggedin'];

// see if there is a cookie
if( loggedin === undefined ){
response.send("youre not logged in");

}else{
response.send("heres your secret stuff");
}
});

app.get('/user/login', (request, response) => {
response.render('login');
})


app.post('/user/login', (request, response) => {



// if the user name and password are the same as in the DB, log them in



let query = "SELECT * FROM users WHERE name='"+request.body.name+"'";

pool.query(query, (err, queryResponse) => {


//response.send('hellooooo');

console.log( queryResponse.rows );

// if the user doesnt exist
if( queryResponse.rows.length === 0 ){
console.log("user doesnt exist");
}else{
console.log("user exists!!!!!!");

const user = queryResponse.rows[0];

let password = user.password;

if( password == request.body.password ){
//password is correct
console.log('PASS WORD CORRECT TOO');

response.cookie('loggedin', 'true');
}else{
// password is incorrect
console.log('PASS WORD not correct');
}

}

response.send('hellooooo')
})
});

app.get('/user/logout', (request, response) => {

response.clearCookie('loggedin');

response.send('you are logged out');
})






app.get('/', (request, response) => {

response.cookie('strawberry', 'something');

response.send('hello');
})

app.get('/what', (request, response) => {


console.log( "cookies", request.cookies['banana'] );

response.send('WHATTTTTTT');
})

app.get('/countme', (request, response) => {


// get the currently set cookie
var visits = request.cookies['visits'];

// see if there is a cookie
if( visits === undefined ){

// set a default value if it doesn't exist
visits = 1;
}else{

// if a cookie exists, make a value thats 1 bigger
visits = parseInt( visits ) + 1;
}

// set the cookie
response.cookie('visits', visits);

response.send('visited '+visits);
})





app.get('/users/new', (request, response) => {

console.log("hello")
response.render("newuser");
});




////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////


const server = app.listen(3000, () => console.log('~~~ Tuning in to the waves of port 3000 ~~~'));

let onClose = function(){

server.close(() => {
console.log('Process terminated')
pool.end( () => console.log('Shut down db connection pool'));
})
};

process.on('SIGTERM', onClose);
process.on('SIGINT', onClose);
108 changes: 93 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ const pg = require('pg');
*/

const configs = {
user: 'akira',
user: 'ishak',
host: '127.0.0.1',
database: 'testdb',
database: 'tweedr_db',
port: 5432,
};

Expand Down Expand Up @@ -47,26 +47,104 @@ app.engine('jsx', reactEngine);

// Root GET request (it doesn't belong in any controller file)
app.get('/', (request, response) => {
response.send('Welcome To Tweedr.');

response.render('home');
});

app.get('/users/new', (request, response) => {
response.render('user/newuser');
app.get('/users/login', (request, response) => {
response.render('login');
});

app.post('/users', (request, response) => {
app.post('/users/login', (request, response) => {

//if username and password are the same as in the DB, log them in
const body = request.body;
const queryString = `SELECT * FROM users WHERE name = '${body.name}' `;

pool.query (queryString, (err, queryResponse) => {
if (err) {
console.log("Got error " + err);
} else {
}

//if user does not exist
if (queryResponse.rows.length === 0) {
console.log("User does not exist!")
} else {
console.log("User exist!"); //else if exist
//queryResponse for current array 0, store in const user
const user = queryResponse.rows[0];
//let password be qeuryResponse.row[0].password...the ket
let password = user.password;
// if for password on log in is the same as password in DB
if (password === body.password) {
console.log("Password is correct!");
response.cookie('loggedin', 'true');

let link = `/user/${user.id}`;
response.redirect(link);
} else {
console.log("Incorrect password try again!");
}
}
});

app.get ('/user/:id', (request,response)=> {
let loggedin = request.cookies['loggedin'];

if (loggedin !== undefined) {
const userId = request.params.id;
const queryText = `SELECT * FROM tweets WHERE users_id = ${userId}`;

pool.query (queryText, (err, queryResponse) => {
if (err) {
console.log("Got error " + err);
} else {
console.log ({user:queryResponse.rows});
response.render('user', {user:queryResponse.rows,id:userId});
}
});
} else {
response.send("you are not logged in!")
}
});

app.post ('/user/:id/tweets/new', (request,response)=> {

const body = request.body;
const queryText = `INSERT INTO tweets (post, users_id) VALUES ($1,$2)`;
const value = [body.tweet, request.params.id];
console.log(request.params.id);

pool.query (queryText, value, (err, queryResponse) => {
if (err) {
console.log("Got error " + err);
} else {
let link = `/user/${request.params.id}`;
response.redirect(link);
}

const queryString = 'INSERT INTO users (name, password) VALUES ($1, $2)';
const values = [
request.body.name,
request.body.password
];
})
})

app.get('/user/:id/logout', (request, response) => {

response.clearCookie('loggedin');

response.send('you are logged out');
})


// const values = [
// request.body.name,
// request.body.password
// ];

// execute query
pool.query(queryString, values, (error, queryResult) => {
// pool.query(queryString, values, (error, queryResult) => {
//response.redirect('/');
response.send('user created');
});
// response.send('user created');
// });
});


Expand All @@ -88,4 +166,4 @@ let onClose = function(){
};

process.on('SIGTERM', onClose);
process.on('SIGINT', onClose);
process.on('SIGINT', onClose);
12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,15 @@
"react": "^16.3.2",
"react-dom": "^16.3.2",
"sha256": "^0.2.0"
}
},
"description": "Let's make a cool new app called TWEEDR!! Not Twitter, geez...",
"repository": {
"type": "git",
"url": "git+https://github.com/ishakbhn/tweedr.git"
},
"author": "",
"bugs": {
"url": "https://github.com/ishakbhn/tweedr/issues"
},
"homepage": "https://github.com/ishakbhn/tweedr#readme"
}
7 changes: 7 additions & 0 deletions seed.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
INSERT INTO users (name, photo_url, password) VALUES ('agogo', 'https://bit.ly/2VQIekM', 'pass1234');
INSERT INTO users (name, photo_url, password) VALUES ('abobo', 'https://bit.ly/2sobDoY','pass1235');
INSERT INTO users (name, photo_url, password) VALUES ('abodo', 'https://bit.ly/2FvWLg0', 'pass1236');

INSERT INTO tweets (post, users_id) VALUES ('A lovely day to run!', 2);
INSERT INTO tweets (post, users_id) VALUES ('Where is my car?!', 1);
INSERT INTO tweets (post, users_id) VALUES ('Ohh i miss you, my coffee', 3);
12 changes: 12 additions & 0 deletions tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
name TEXT,
photo_url TEXT,
password VARCHAR
);

CREATE TABLE IF NOT EXISTS tweets (
id SERIAL PRIMARY KEY,
post TEXT,
users_id INT
);
Loading