Skip to content
Browse files

Fixed a bug when loading the MySQL driver (it was being loaded each t…

…ime a MySQL request was made).
  • Loading branch information...
1 parent 1ce4b53 commit e63b5604becf71474b7abf71e18dafaba8354e74 @pedrofranceschi pedrofranceschi committed Dec 10, 2010
Showing with 41 additions and 19 deletions.
  1. +5 −2 blogode.js
  2. +4 −2 lib/comments.js
  3. +7 −5 lib/database.js
  4. +2 −0 lib/posts.js
  5. +2 −0 lib/users.js
  6. +4 −2 views/admin/posts/index.ejs
  7. +9 −7 views/posts/index.ejs
  8. +8 −1 views/posts/show.ejs
View
7 blogode.js
@@ -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
@@ -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 }
+ });
});
});
});
View
6 lib/comments.js
@@ -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);
});
});
}
View
12 lib/database.js
@@ -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();
}
View
2 lib/posts.js
@@ -1,5 +1,7 @@
var sys = require('sys'),
database = require('./database.js');
+
+database.initialize();
exports.getPosts = function(numberOfPosts, callback) {
var limitString = "";
View
2 lib/users.js
@@ -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) {
View
6 views/admin/posts/index.ejs
@@ -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>
View
16 views/posts/index.ejs
@@ -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/>
+ <% } %>
<% } %>
<% } %>
View
9 views/posts/show.ejs
@@ -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.
Something went wrong with that request. Please try again.