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

Not fully completed but I like my CSS though~ Damn it I really gotta learn to let go sometimes T~T #28

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
117 changes: 92 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ const pg = require('pg');
*/

const configs = {
user: 'akira',
host: '127.0.0.1',
database: 'testdb',
port: 5432,
user: 'cash',
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);
console.log('idle client error', err.message, err.stack);
});

// Init express app
Expand All @@ -30,7 +30,7 @@ const app = express();
app.use(methodOverride('_method'));
app.use(cookieParser());
app.use(express.urlencoded({
extended: true
extended: true
}));

// Set react-views to be the default view engine
Expand All @@ -45,47 +45,114 @@ app.engine('jsx', reactEngine);
* ===================================
*/

// Root GET request (it doesn't belong in any controller file)
app.get('/', (request, response) => {
response.send('Welcome To Tweedr.');
// Root GET req (it doesn't belong in any controller file)
app.get('/', (req, res) => {
let loggedin = req.cookies['loggedin'];
if (loggedin == undefined) {
res.render('main')
} else {
let tempArr = [];
let getusers = `select * from users;`
let gettweets = `select * from tweets;`
let getfollows = `select * from getfollows;`
pool.query(getusers, (err, result1) => {
tempArr.push(result1.rows);
})
pool.query(gettweets, (err, result2) => {
tempArr.push(result2.rows);
})
pool.query(getfollows, (err, result3) => {
tempArr.push(result3.rows);
})
let passObj = {
tempArr
}
res.render('loggedmain', {
passObj: passObj
});
};
});

app.get('/users/new', (request, response) => {
response.render('user/newuser');
app.post('/login', (req, res) => {
let testlogin = `select * from users where username='${req.body.username}';`
pool.query(testlogin, (err, result) => {
if (result.rows.length == 0) {
console.log(req.body.username)
console.log('username error')
res.render('loginerror');
} else {
if (result.rows[0].password == req.body.password) {
let tempArr = [];
tempArr.push(result.rows)
let getusers = `select * from users;`
let gettweets = `select * from tweets;`
let getfollows = `select * from getfollows;`
pool.query(getusers, (err, result1) => {
tempArr.push(result1.rows);
})
pool.query(gettweets, (err, result2) => {
tempArr.push(result2.rows);
})
pool.query(getfollows, (err, result3) => {
tempArr.push(result3.rows);
})
let passObj = {
tempArr
}
res.render('loggedmain', {
passObj: passObj
});
} else {
console.log('password error')
res.render('loginerror');
};
};
});
});

app.get('/users/new', (req, res) => {
res.render('createacc');
});

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

const queryString = 'INSERT INTO users (name, password) VALUES ($1, $2)';
app.post('/users', (req, res) => {

const queryString = 'INSERT INTO users (username, firstname, lastname, password, email) VALUES ($1, $2, $3, $4, $5)';
const values = [
request.body.name,
request.body.password
req.body.username,
req.body.firstname,
req.body.lastname,
req.body.password,
req.body.email
];

// execute query
pool.query(queryString, values, (error, queryResult) => {
//response.redirect('/');
response.send('user created');
//res.redirect('/');
if (error) {
console.log(error.stack)
}
console.log('user created');
res.render('main');
});
});



/**
* ===================================
* Listen to requests on port 3000
* Listen to reqs on port 3000
* ===================================
*/

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

let onClose = function(){
let onClose = function () {

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

process.on('SIGTERM', onClose);
process.on('SIGINT', onClose);
process.on('SIGINT', onClose);
Empty file added removetables.sql
Empty file.
33 changes: 33 additions & 0 deletions seed.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
INSERT INTO users (firstname, lastname, username, password, email) VALUES ('Cash', 'Teo', 'kingcashthefifth', '12345678', 'kingcashthefifth@gmail.com');
INSERT INTO users (firstname, lastname, username, password, email) VALUES ('Zuei', 'Tam', 'zueilababy', '12345678', 'zueilababy@gmail.com');
INSERT INTO users (firstname, lastname, username, password, email) VALUES ('Beboo', 'Teo', 'princebeboo', '12345678', 'princebeboo@gmail.com');
INSERT INTO users (firstname, lastname, username, password, email) VALUES ('David', 'Ng', 'davidng', '12345678', 'davidng@gmail.com');
INSERT INTO users (firstname, lastname, username, password, email) VALUES ('Teng', 'Teng', 'tengtengteng', '12345678', 'tengtengteng@gmail.com');



INSERT INTO tweets (tweet, user_id) VALUES ('Hi is anybody there?', 1);
INSERT INTO tweets (tweet, user_id) VALUES ('Passion for code still burning strong!', 1);
INSERT INTO tweets (tweet, user_id) VALUES ('I don''t understand what''s so great about programming', 2);
INSERT INTO tweets (tweet, user_id) VALUES ('I hate using the computer!', 2);
INSERT INTO tweets (tweet, user_id) VALUES ('Shopping is my passion', 2);
INSERT INTO tweets (tweet, user_id) VALUES ('I love IU', 3);
INSERT INTO tweets (tweet, user_id) VALUES ('I hope I get a snack from my master today!', 3);
INSERT INTO tweets (tweet, user_id) VALUES ('Bark Bark Bark!', 3);
INSERT INTO tweets (tweet, user_id) VALUES ('Woof! Woof!', 3);



INSERT INTO follows (user_id, following_id) VALUES (1, 2);
INSERT INTO follows (user_id, following_id) VALUES (1, 3);
INSERT INTO follows (user_id, following_id) VALUES (1, 4);
INSERT INTO follows (user_id, following_id) VALUES (1, 5);
INSERT INTO follows (user_id, following_id) VALUES (2, 1);
INSERT INTO follows (user_id, following_id) VALUES (2, 3);
INSERT INTO follows (user_id, following_id) VALUES (2, 4);
INSERT INTO follows (user_id, following_id) VALUES (3, 1);
INSERT INTO follows (user_id, following_id) VALUES (3, 2);
INSERT INTO follows (user_id, following_id) VALUES (5, 1);
INSERT INTO follows (user_id, following_id) VALUES (1, 4);
INSERT INTO follows (user_id, following_id) VALUES (4, 1);
INSERT INTO follows (user_id, following_id) VALUES (4, 2);
25 changes: 25 additions & 0 deletions tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
firstname VARCHAR(25),
lastname VARCHAR(25),
username VARCHAR(25),
password VARCHAR(25),
email VARCHAR(50),
propic VARCHAR(200)
);

CREATE TABLE IF NOT EXISTS tweets (
id SERIAL PRIMARY KEY,
tweet VARCHAR(150),
tweetpic VARCHAR(200),
tweetvid VARCHAR(200),
tweettime TIMESTAMP DEFAULT now(),
user_id INTEGER
);

CREATE TABLE IF NOT EXISTS follows (
id SERIAL PRIMARY KEY,
user_id INTEGER,
following_id INTEGER
);

152 changes: 152 additions & 0 deletions views/createacc.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
var React = require("react");

class Createacc extends React.Component {
render() {
return (
<html>
<head>
<meta charSet="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
<link
rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO"
crossOrigin="anonymous"
/>
<link rel="stylesheet" href="../style.css" />
<title>Tunr Express</title>
</head>
<body>
<ul className="nav nav-pills navHeight sticky-top" style={{ background: "lightgrey", height: "3.5rem" }}>
{/* <li className="nav-item" style={{ margin: "auto 5px" }}>
<a className="nav-link active" href="/artist/">
Login
</a>
</li> */}
</ul>
<br />
<h1 className="ml-5 p-3">Create a new account</h1>
<br />
<div className="m-auto" style={{ width: "95%" }}>
<form action="/users" method="post">
<div className="form-group">
<label htmlFor="titleInput">Username</label>
<input
name="username"
className="form-control"
id="titleInput"
aria-describedby="titleHelp"
placeholder="Choose a username"
/>
<small id="titleHelp" className="form-text text-muted">
Your username must consist of at least 4 alphabet letters.
</small>
</div>
<div className="form-group">
<div className="row">
<div className="col">
<input name="firstname" type="text" className="form-control" placeholder="Input your first name" />
</div>
<div className="col">
<input name="lastname" type="text" className="form-control" placeholder="Input your last name" />
</div>
</div>
</div>
<div className="form-group">
<label htmlFor="ingreInput">Password</label>
<input
name="password"
className="form-control"
id="ingreInput"
aria-describedby="ingreHelp"
placeholder="Choose a password"
/>
<small id="ingreHelp" className="form-text text-muted">
Your password must contain at least 8 alphanumeric letters.
</small>
</div>
<div className="form-group">
<label htmlFor="instrucTextArea">Email address</label>
<input
name="email"
className="form-control"
id="instrucTextArea"
placeholder="Input your email address"
/>
</div>
<button
type="button"
className="btn btn-primary"
data-toggle="modal"
data-target="#exampleModal"
>
Submit
</button>
<div
className="modal fade"
id="exampleModal"
tabIndex="-1"
role="dialog"
aria-labelledby="exampleModalLabel"
aria-hidden="true"
>
<div className="modal-dialog" role="document">
<div className="modal-content">
<div className="modal-header">
<h5 className="modal-title" id="exampleModalLabel">
Confirmation
</h5>
<button
type="button"
className="close"
data-dismiss="modal"
aria-label="Close"
>
<span aria-hidden="true">&times;</span>
</button>
</div>
<div className="modal-body">
Do you wish proceed and create this account?
</div>
<div className="modal-footer">
<button
type="button"
className="btn btn-secondary"
data-dismiss="modal"
>
Back
</button>
<button type="submit" className="btn btn-primary">
Continue
</button>
</div>
</div>
</div>
</div>
</form>
</div>
<script
src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
crossOrigin="anonymous"
/>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"
integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49"
crossOrigin="anonymous"
/>
<script
src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"
integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy"
crossOrigin="anonymous"
/>
</body>
</html >
);
}
}

module.exports = Createacc;
Loading