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

node-express-reddit-clone -- Armel -- (unfinished) #19

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
44 changes: 32 additions & 12 deletions controllers/auth.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,43 @@
var express = require('express');
var bodyParser = require('body-parser'); // added
var mysql = require('promise-mysql'); //
var parse_middle = bodyParser.urlencoded({extended:false});


module.exports = function(myReddit) {
var authController = express.Router();

authController.get('/login', function(request, response) {
response.send("TO BE IMPLEMENTED");
response.render("login-form"); // CHECK
});

authController.post('/login', function(request, response) {
response.send("TO BE IMPLEMENTED");

authController.post('/login', parse_middle, function(req, res) {
var username = req.body.username;
var password = req.body.password;

myReddit.checkUserLogin(username, password)
.then(user => {
return myReddit.createUserSession(user.id);
})
.then(token => {console.log("here is the token : " + token);
res.cookie("SESSION", token);})
.then(() => res.redirect(302, '/'))
.catch( e =>{
res.status(401).send(e.message);
})

});

authController.get('/signup', function(request, response) {
response.send("TO BE IMPLEMENTED");
response.render("signup-form"); // CHECK
});

authController.post('/signup', function(request, response) {
response.send("TO BE IMPLEMENTED");

authController.post('/signup', parse_middle, function(req,res){
myReddit.createUser({
username : req.body.username,
password : req.body.password
}).then(res.redirect(302, '/auth/login')); // CHECK
});

return authController;
}
}
1 change: 1 addition & 0 deletions cookies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//
35 changes: 32 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
var express = require('express');
// > npm install --save express
var mysql = require('promise-mysql');
// > npm install --save promise-mysql
// ++ > npm install --save bcrypt-as-promised
// ++ > npm install --save pug

// Load express and create a new web server
// Load all the Express middlewares we will use and adds them to the pipeline with app.use
// Loads the RedditAPI, created a database connection and sets up the API
// Delegates anything under /static to the static middleware
// Delegates anything under /auth to a custom Express Router
// Sets up a few other request handlers for the homepage, subreddits, creating posts and voting. These functionalities could be further split up in their own modules
// Finally, makes the web server listen on process.env.PORT, which is set to 8080 on Cloud9.

// Express middleware
var bodyParser = require('body-parser'); // reads request bodies from POST requests
// > npm install --save body-parser
var cookieParser = require('cookie-parser'); // parses cookie from Cookie request header into an object
// > npm install --save cookie-parser
var morgan = require('morgan'); // logs every request on the console
// > npm install --save morgan
var checkLoginToken = require('./lib/check-login-token.js'); // checks if cookie has a SESSION token and sets request.user
var onlyLoggedIn = require('./lib/only-logged-in.js'); // only allows requests from logged in users

Expand All @@ -18,6 +33,7 @@ var authController = require('./controllers/auth.js');
var RedditAPI = require('./lib/reddit.js');
var connection = mysql.createPool({
user: 'root',
password : 'root',
database: 'reddit'
});
var myReddit = new RedditAPI(connection);
Expand Down Expand Up @@ -55,7 +71,7 @@ This custom middleware checks in the cookies if there is a SESSION token and val
NOTE: This middleware is currently commented out! Uncomment it once you've implemented the RedditAPI
method `getUserFromSession`
*/
// app.use(checkLoginToken(myReddit));
app.use(checkLoginToken(myReddit));



Expand Down Expand Up @@ -111,13 +127,26 @@ app.get('/subreddits', function(request, response) {
1. Get all subreddits with RedditAPI
2. Render some HTML that lists all the subreddits
*/

response.send("TO BE IMPLEMENTED");
});

// Subreddit homepage, similar to the regular home page but filtered by sub.
app.get('/r/:subreddit', function(request, response) {
response.send("TO BE IMPLEMENTED");
return myReddit.getSubredditByName(request.params.subreddit)
.then(subreddit => {
console.log(subreddit.subreddit_id);
if (!subreddit) {
throw new Error('404');
} else {
return myReddit.getAllPosts(subreddit.subreddit_id);
}
}).then(function(posts) {
response.render('homepage', {posts: posts});
})
.catch(function(error) {
response.render('error', {error: error});
})
});

// Sorted home page
Expand Down
186 changes: 138 additions & 48 deletions lib/reddit.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class RedditAPI {
});
}

getAllPosts() {
getAllPosts(subredditId) {
/*
strings delimited with ` are an ES2015 feature called "template strings".
they are more powerful than what we are using them for here. one feature of
Expand All @@ -93,41 +93,45 @@ class RedditAPI {
therefore template strings make it very easy to write SQL queries that span multiple
lines without having to manually split the string line by line.
*/
var subreddit_string = '';
if (subredditId){
subreddit_string += '\n WHERE p.subredditId = 15 \n';
}

return this.conn.query(
`
SELECT
p.id AS posts_id,
p.title AS posts_title,
p.url AS posts_url,
p.createdAt AS posts_createdAt,
p.updatedAt AS posts_updatedAt,

u.id AS users_id,
u.username AS users_username,
u.createdAt AS users_createdAt,
u.updatedAt AS users_updatedAt,

s.id AS subreddits_id,
s.name AS subreddits_name,
s.description AS subreddits_description,
s.createdAt AS subreddits_createdAt,
s.updatedAt AS subreddits_updatedAt,

COALESCE(SUM(v.voteDirection), 0) AS voteScore,
SUM(IF(v.voteDirection = 1, 1, 0)) AS numUpvotes,
SUM(IF(v.voteDirection = -1, 1, 0)) AS numDownvotes

FROM posts p
JOIN users u ON p.userId = u.id
JOIN subreddits s ON p.subredditId = s.id
LEFT JOIN votes v ON p.id = v.postId

GROUP BY p.id
ORDER BY p.createdAt DESC
LIMIT 25`
)
var query1 = `SELECT
p.id AS posts_id,
p.title AS posts_title,
p.url AS posts_url,
p.createdAt AS posts_createdAt,
p.updatedAt AS posts_updatedAt,

u.id AS users_id,
u.username AS users_username,
u.createdAt AS users_createdAt,
u.updatedAt AS users_updatedAt,

s.id AS subreddits_id,
s.name AS subreddits_name,
s.description AS subreddits_description,
s.createdAt AS subreddits_createdAt,
s.updatedAt AS subreddits_updatedAt,

COALESCE(SUM(v.voteDirection), 0) AS voteScore,
SUM(IF(v.voteDirection = 1, 1, 0)) AS numUpvotes,
SUM(IF(v.voteDirection = -1, 1, 0)) AS numDownvotes

FROM posts p
JOIN users u ON p.userId = u.id
JOIN subreddits s ON p.subredditId = s.id
LEFT JOIN votes v ON p.id = v.postId `;

var query2 = `GROUP BY p.id
ORDER BY p.createdAt DESC
LIMIT 25;`

return this.conn.query(query1 + subreddit_string + query2) // HERE
.then(function(posts) {
console.log(posts);
return posts.map(transformPost)
});
}
Expand All @@ -141,28 +145,28 @@ class RedditAPI {
p.title AS posts_title,
p.url AS posts_url,
p.createdAt AS posts_createdAt,
p.updatedAt AS posts_updatedAt,
p.updatedAt AS posts_updatedAt,

u.id AS users_id,
u.username AS users_username,
u.createdAt AS users_createdAt,
u.updatedAt AS users_updatedAt,

s.id AS subreddits_id,
s.name AS subreddits_name,
s.description AS subreddits_description,
s.createdAt AS subreddits_createdAt,
s.updatedAt AS subreddits_updatedAt,

COALESCE(SUM(v.voteDirection), 0) AS voteScore,
SUM(IF(v.voteDirection = 1, 1, 0)) AS numUpvotes,
SUM(IF(v.voteDirection = -1, 1, 0)) AS numDownvotes

FROM posts p
JOIN users u ON p.userId = u.id
JOIN subreddits s ON p.subredditId = s.id
LEFT JOIN votes v ON p.id = v.postId

WHERE p.id = ?`,
[postId]
)
Expand Down Expand Up @@ -241,13 +245,13 @@ class RedditAPI {
c.text as comments_text,
c.createdAt as comments_createdAt,
c.updatedAt as comments_updatedAt,

u.id as users_id,
u.username as users_username

FROM comments c
JOIN users u ON c.userId = u.id

WHERE c.postId = ?
ORDER BY c.createdAt DESC
LIMIT 25`,
Expand All @@ -269,9 +273,51 @@ class RedditAPI {
});
});
}

getSubredditByName(name){ // self introduced API
return this.conn.query(`
SELECT
subreddits.id as subreddit_id
FROM subreddits
WHERE subreddits.name = ?;
`,
[name]
).then(subreddit => {return subreddit[0]});
}
checkUserLogin(username, password) {
return Promise.reject(new Error("TODO: You have to implement the RedditAPI.checkUserLogin function."))
// if(username.length !=7){
// return Promise.reject(new Error('not 7'));
// }
var user = {};
var er = "username or password incorrect";
return this.conn.query(`
SELECT
users.id as id,
users.username as username,
users.password as password

FROM users

WHERE users.username = ?`,
[username]
)
.then(myTable => {
if (myTable.length == 0) {
throw new Error('404');
} else {
user = myTable[0];
return user;
}
})
.then(u => bcrypt.compare(password, u.password))
.then(correct => {
if(correct){
return {id:user.id,username:user.username}
}
else{
throw new Error(er);
//return Promise.reject(new Error("voteDirection must be one of -1, 0, 1"));
}
});

/*
Here are the steps you should follow:
Expand All @@ -286,8 +332,22 @@ class RedditAPI {
}

createUserSession(userId) {
return Promise.reject(new Error("TODO: You have to implement the RedditAPI.createUserSession function."))


// return Promise.reject(new Error("TODO: You have to implement the RedditAPI.createUserSession function."))

var the_token;

return bcrypt.genSalt(10).then(token => {
the_token = token;
return token;
}).then(token => {
this.conn.query(`
INSERT INTO sessions (userId, token) VALUES (?,?)`
,
[userId, token]);
return the_token;
});
/*
Here are the steps you should follow:

Expand All @@ -297,9 +357,39 @@ class RedditAPI {
*/
}

// createUserSession(userId) {
// // return Promise.reject(new Error("TODO: You have to implement the RedditAPI.createUserSession function."))
// var resToken = "";
// return bcrypt.genSalt(10)
// .then(data2 =>{
// var token = data2;
// //console.log("The Token:", token," the user id:", userId.id)
// resToken = token;
// return token;
//
// })
// .then(token => this.conn.query(`INSERT INTO sessions (userId,token) VALUES (?,?)`,[userId.id,token]))
// .then(result => {
// console.log("query result", result)
// console.log(resToken, "the result token")
// return resToken;
// })
// }

getUserFromSession(sessionId) {
return Promise.reject(new Error("TODO: You have to implement the RedditAPI.getUserFromSession function."));
return this.conn.query(`
SELECT
u.id AS id,
u.username AS username,
u.createdAt AS createdAt,
u.updatedAt AS updatedAt
FROM users u join sessions s
ON u.id = s.userId
WHERE s.token = ?`, [sessionId])
.then(result => {
return result[0];
});
}
}

module.exports = RedditAPI;
module.exports = RedditAPI;
Loading