-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathreddit.js
114 lines (111 loc) · 3.82 KB
/
reddit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
var bcrypt = require('bcrypt');
var HASH_ROUNDS = 10;
module.exports = function RedditAPI(conn) {
return {
createUser: function(user, callback) {
// first we have to hash the password...
bcrypt.hash(user.password, HASH_ROUNDS, function(err, hashedPassword) {
if (err) {
callback(err);
}
else {
conn.query(
'INSERT INTO `users` (`username`,`password`, `createdAt`) VALUES (?, ?, ?)', [user.username, hashedPassword, null],
function(err, result) {
if (err) {
/*
There can be many reasons why a MySQL query could fail. While many of
them are unknown, there's a particular error about unique usernames
which we can be more explicit about!
*/
if (err.code === 'ER_DUP_ENTRY') {
callback(new Error('A user with this username already exists'));
}
else {
callback(err);
}
}
else {
/*
Here we are INSERTing data, so the only useful thing we get back
is the ID of the newly inserted row. Let's use it to find the user
and return it
*/
conn.query(
'SELECT `id`, `username`, `createdAt`, `updatedAt` FROM `users` WHERE `id` = ?', [result.insertId],
function(err, result) {
if (err) {
callback(err);
}
else {
/*
Finally! Here's what we did so far:
1. Hash the user's password
2. Insert the user in the DB
3a. If the insert fails, report the error to the caller
3b. If the insert succeeds, re-fetch the user from the DB
4. If the re-fetch succeeds, return the object to the caller
*/
callback(null, result[0]);
}
}
);
}
}
);
}
});
},
createPost: function(post, callback) {
conn.query(
'INSERT INTO `posts` (`userId`, `title`, `url`, `createdAt`) VALUES (?, ?, ?, ?)', [post.userId, post.title, post.url, null],
function(err, result) {
if (err) {
callback(err);
}
else {
/*
Post inserted successfully. Let's use the result.insertId to retrieve
the post and send it to the caller!
*/
conn.query(
'SELECT `id`,`title`,`url`,`userId`, `createdAt`, `updatedAt` FROM `posts` WHERE `id` = ?', [result.insertId],
function(err, result) {
if (err) {
callback(err);
}
else {
callback(null, result[0]);
}
}
);
}
}
);
},
getAllPosts: function(options, callback) {
// In case we are called without an options parameter, shift all the parameters manually
if (!callback) {
callback = options;
options = {};
}
var limit = options.numPerPage || 25; // if options.numPerPage is "falsy" then use 25
var offset = (options.page || 0) * limit;
conn.query(`
SELECT \`id\`,\`title\`,\`url\`,\`userId\`, \`createdAt\`, \`updatedAt\`
FROM \`posts\`
ORDER BY \`createdAt\` DESC
LIMIT ? OFFSET ?
`, [limit, offset],
function(err, results) {
if (err) {
callback(err);
}
else {
callback(null, results);
}
}
);
}
}
}