Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Started implementing comments. Implemented post reading as a normal u…

…ser.
  • Loading branch information...
commit 1ce4b53cd0a51f0e8d2ab4708058add9223f3407 1 parent 092f9ac
Pedro Franceschi authored December 10, 2010
12  blogode.js
@@ -22,12 +22,22 @@ app.get("/", function(req, res){
22 22
     // return posts list
23 23
     
24 24
     posts.getPosts(10, function (posts){
25  
-        res.render('index', {
  25
+        res.render('posts/index', {
26 26
             locals: { 'posts': posts }
27 27
         });
28 28
     });
29 29
 });
30 30
 
  31
+app.get("/:id", function(req, res){
  32
+    // return an specific post (by ID)
  33
+    
  34
+    posts.getPost(req.param('id'), function(post) {
  35
+        res.render('posts/show', {
  36
+            locals: { 'post': post }
  37
+        });
  38
+    });
  39
+});
  40
+
31 41
 app.get("/admin", function(req, res){
32 42
     // return admin panel
33 43
     
14  lib/comments.js
... ...
@@ -0,0 +1,14 @@
  1
+var sys = require('sys'),
  2
+    database = require('./database.js');
  3
+    
  4
+exports.getCommentsOfPost = function(postId, callback) {
  5
+    database.getDatabaseConnection(function (mysql_client) {
  6
+        mysql_client.query("SELECT * FROM comments WHERE id='" + escape(postId) + "';", function (error, results, fields) {
  7
+            if(error) {
  8
+                throw "Error getting comment: " + error;
  9
+            }
  10
+            
  11
+            callback(results[0]);
  12
+        });
  13
+    });
  14
+}
5  lib/database.js
@@ -19,8 +19,9 @@ exports.getDatabaseConnection = function(callback) {
19 19
 }
20 20
 
21 21
 exports._createTables = function(mysql_client, callback) {
22  
-    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);")
23  
-    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);")
  22
+    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);")
  23
+    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);")
  24
+    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);")
24 25
     callback();
25 26
 }
26 27
 
23  lib/posts.js
@@ -50,10 +50,6 @@ exports.getPost = function(postId, callback) {
50 50
                 }
51 51
                 var postInfo = results[0]
52 52
                 postInfo['user_name'] = results1[0].name;
53  
-                var unpreparedBody = exports._unpreparePostBody(unescape(postInfo['body']));
54  
-                sys.puts('unpreparedBody: ' + sys.inspect(unpreparedBody));
55  
-                postInfo['body'] = escape(unpreparedBody);
56  
-                
57 53
                 callback(postInfo);
58 54
             });
59 55
         });
@@ -62,7 +58,7 @@ exports.getPost = function(postId, callback) {
62 58
 
63 59
 exports.createPost = function(title, body, user_id, callback) {
64 60
     database.getDatabaseConnection(function (mysql_client) {
65  
-        mysql_client.query("INSERT INTO posts (title, body, user_id) VALUES ('" + escape(title) + "', '" + escape(exports._preparePostBody(body)) + "', " + user_id + ");", function (error, results, fields) {
  61
+        mysql_client.query("INSERT INTO posts (title, body, user_id) VALUES ('" + escape(title) + "', '" + escape(body) + "', " + user_id + ");", function (error, results, fields) {
66 62
             if(error) {
67 63
                 throw "Error creating post: " + error;
68 64
             }
@@ -73,7 +69,7 @@ exports.createPost = function(title, body, user_id, callback) {
73 69
 
74 70
 exports.updatePost = function(id, title, body, callback) {
75 71
     database.getDatabaseConnection(function (mysql_client) {
76  
-        mysql_client.query("UPDATE posts SET title='" + escape(title) + "', body='" + escape(exports._preparePostBody(body)) + "' WHERE id='" + escape(id) + "';", function (error, results, fields) {
  72
+        mysql_client.query("UPDATE posts SET title='" + escape(title) + "', body='" + escape(body) + "' WHERE id='" + escape(id) + "';", function (error, results, fields) {
77 73
             if(error) {
78 74
                 throw "Error creating post: " + error;
79 75
             }
@@ -91,19 +87,4 @@ exports.destroyPost = function(id, callback) {
91 87
             callback();
92 88
         });
93 89
     });
94  
-}
95  
-
96  
-function replaceAll(string, token, newtoken) {
97  
-	while (string.indexOf(token) != -1) {
98  
- 		string = string.replace(token, newtoken);
99  
-	}
100  
-	return string;
101  
-}
102  
-
103  
-exports._preparePostBody = function(body) {
104  
-    return replaceAll(body, "\n", "<br/>")
105  
-}
106  
-
107  
-exports._unpreparePostBody = function(body) {
108  
-    return replaceAll(body, "<br/>", "\n")
109 90
 }
1  views/layout.ejs
@@ -5,6 +5,7 @@
5 5
 	<body>
6 6
 		<h1>HEADER</h1><br/>
7 7
 		<%- body %>
  8
+		<br/><br/>
8 9
 		<h1>FOOTER</h1>
9 10
 	</body>
10 11
 </html>
2  views/index.ejs → views/posts/index.ejs
... ...
@@ -1,5 +1,5 @@
1 1
 <% for (var i=0; i < posts.length; i++) { %>
2  
-	<h2><%= unescape(posts[i].title) %></h2>
  2
+	<h2><a href="/<%= posts[i].id %>"><%= unescape(posts[i].title) %></a></h2>
3 3
 	<p>by <%= posts[i].user_name %></p>
4 4
 	<% if(unescape(posts[i].body).indexOf("###") >= 0) { %>
5 5
 		<p><%- unescape(posts[i].body).split("###")[0] %></p><br/>
3  views/posts/show.ejs
... ...
@@ -0,0 +1,3 @@
  1
+<h1><%= unescape(post.title) %></h1>
  2
+<p>by <%= post.user_name %></p>
  3
+<p><%- unescape(post.body) %></p>

0 notes on commit 1ce4b53

Please sign in to comment.
Something went wrong with that request. Please try again.