Skip to content

Commit

Permalink
Fixed a bug when loading the MySQL driver (it was being loaded each t…
Browse files Browse the repository at this point in the history
…ime a MySQL request was made).
  • Loading branch information
pedrofranceschi committed Dec 10, 2010
1 parent 1ce4b53 commit e63b560
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 19 deletions.
7 changes: 5 additions & 2 deletions blogode.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ app.configure(function() {

var posts = require('./lib/posts');
var users = require('./lib/users');
var comments = require('./lib/comments');

app.get("/", function(req, res){
// return posts list
Expand All @@ -32,8 +33,10 @@ app.get("/:id", function(req, res){
// return an specific post (by ID)

posts.getPost(req.param('id'), function(post) {
res.render('posts/show', {
locals: { 'post': post }
comments.getCommentsOfPost(req.param('id'), function(comments){
res.render('posts/show', {
locals: { 'post': post, 'comments': comments }
});
});
});
});
Expand Down
6 changes: 4 additions & 2 deletions lib/comments.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
var sys = require('sys'),
database = require('./database.js');

database.initialize();

exports.getCommentsOfPost = function(postId, callback) {
database.getDatabaseConnection(function (mysql_client) {
mysql_client.query("SELECT * FROM comments WHERE id='" + escape(postId) + "';", function (error, results, fields) {
mysql_client.query("SELECT * FROM comments WHERE post_id='" + escape(postId) + "';", function (error, results, fields) {
if(error) {
throw "Error getting comment: " + error;
}

callback(results[0]);
callback(results);
});
});
}
12 changes: 7 additions & 5 deletions lib/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@ var sys = require('sys'),

var client = new Client();

exports.getDatabaseConnection = function(callback) {
exports.initialize = function() {
this._getDatabaseSettingsFromConfig(function (hostname, username, password, database){
client.user = username;
client.password = password;
client.host = hostname;
client.connect();
client.query('USE ' + escape(database));

exports._createTables(client, function() {
callback(client);
});
exports._createTables(client, function(){});
});
}

exports.getDatabaseConnection = function(callback) {
callback(client);
}

exports._createTables = function(mysql_client, callback) {
mysql_client.query("CREATE TABLE IF NOT EXISTS posts (id INT PRIMARY KEY AUTO_INCREMENT, user_id INT, title VARCHAR(200), body TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP);")
mysql_client.query("CREATE TABLE IF NOT EXISTS users (id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100), description VARCHAR(500), email VARCHAR(100), username VARCHAR(100), password VARCHAR(100), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP);")
mysql_client.query("CREATE TABLE IF NOT EXISTS comments (id INT PRIMARY KEY AUTO_INCREMENT, in_reply_to_id INT, author_name VARCHAR(100), author_email VARCHAR(100), comment TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP);")
mysql_client.query("CREATE TABLE IF NOT EXISTS comments (id INT PRIMARY KEY AUTO_INCREMENT, in_reply_to_id INT, post_id INT, author_name VARCHAR(100), author_email VARCHAR(100), comment TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP);")
callback();
}

Expand Down
2 changes: 2 additions & 0 deletions lib/posts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var sys = require('sys'),
database = require('./database.js');

database.initialize();

exports.getPosts = function(numberOfPosts, callback) {
var limitString = "";
Expand Down
2 changes: 2 additions & 0 deletions lib/users.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var sys = require('sys'),
database = require('./database.js');

database.initialize();

exports.verifyCredentials = function(username, password, callback) {
database.getDatabaseConnection(function (mysql_client) {
mysql_client.query("SELECT * FROM users WHERE username='" + escape(username) + "' AND password='" + escape(password) + "';", function (error, results, fields) {
Expand Down
6 changes: 4 additions & 2 deletions views/admin/posts/index.ejs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<h1>Manage posts</h1>

<% for (var i=0; i < posts.length; i++) { %>
<b><%= unescape(posts[i].title) %></b> (by <%= posts[i].user_name %>) - <a href="/admin/posts/<%= posts[i].id %>">Edit</a> - <a href="/admin/posts/destroy/<%= posts[i].id %>">Delete</a><br/><br/>
<% if(posts != undefined) { %>
<% for (var i=0; i < posts.length; i++) { %>
<b><%= unescape(posts[i].title) %></b> (by <%= posts[i].user_name %>) - <a href="/admin/posts/<%= posts[i].id %>">Edit</a> - <a href="/admin/posts/destroy/<%= posts[i].id %>">Delete</a><br/><br/>
<% } %>
<% } %>

<a href="/admin/posts/new">New post</a>
16 changes: 9 additions & 7 deletions views/posts/index.ejs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<% for (var i=0; i < posts.length; i++) { %>
<h2><a href="/<%= posts[i].id %>"><%= unescape(posts[i].title) %></a></h2>
<p>by <%= posts[i].user_name %></p>
<% if(unescape(posts[i].body).indexOf("###") >= 0) { %>
<p><%- unescape(posts[i].body).split("###")[0] %></p><br/>
<% } else { %>
<p><%- unescape(posts[i].body) %></p><br/>
<% if(posts != undefined) { %>
<% for (var i=0; i < posts.length; i++) { %>
<h2><a href="/<%= posts[i].id %>"><%= unescape(posts[i].title) %></a></h2>
<p>by <%= posts[i].user_name %></p>
<% if(unescape(posts[i].body).indexOf("###") >= 0) { %>
<p><%- unescape(posts[i].body).split("###")[0] %></p><br/>
<% } else { %>
<p><%- unescape(posts[i].body) %></p><br/>
<% } %>
<% } %>
<% } %>
9 changes: 8 additions & 1 deletion views/posts/show.ejs
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
<h1><%= unescape(post.title) %></h1>
<p>by <%= post.user_name %></p>
<p><%- unescape(post.body) %></p>
<p><%- unescape(post.body) %></p>

<h1>Comments</h1>

<% for (var i=0; i < comments.length; i++) { %>
<h4><%= comments[i].author_name %></h4>
<p><%= comments[i].comment %></p>
<% } %>

0 comments on commit e63b560

Please sign in to comment.