Permalink
Browse files

Implemented posts search.

  • Loading branch information...
1 parent 326bc7c commit 1b793ba0ad8012e2b4f9832f89615695a46856eb @pedrofranceschi pedrofranceschi committed Dec 11, 2010
Showing with 70 additions and 4 deletions.
  1. +2 −2 README.markdown
  2. +17 −1 blogode.js
  3. +32 −0 lib/posts.js
  4. +6 −1 views/layout.ejs
  5. +13 −0 views/posts/search.ejs
View
@@ -18,10 +18,10 @@ TODO
----
* Refactor with Tests
* Add other databases
-* Finish implementing comments
+* Implement posts search
* Implement layout management (user view and admin view)
* Implement plugins API
-* Implement posts search
+* Implement statistics
License:
--------
View
@@ -125,7 +125,6 @@ app.get('/admin/posts/:id', adminLoginFilter, function(req, res) {
app.post('/admin/posts/save', adminLoginFilter, function(req, res) {
// saves a post
- sys.puts('saving: ' + sys.inspect(req.param('textEditor')))
if(!req.param('title') || !req.param('body')) {
return res.redirect("/admin/posts/new");
}
@@ -156,6 +155,23 @@ app.get('/admin/posts/destroy/:id', adminLoginFilter, function(req, res) {
});
});
+app.get("/search", function(req, res){
+ // performs a search for a post
+
+ if(!req.param('keywords')) {
+ res.render('posts/search', {
+ locals: { 'posts': undefined }
+ });
+ }
+
+ posts.searchForPosts(req.param('keywords'), function(searchResults){
+ res.render('posts/search', {
+ locals: { 'posts': searchResults }
+ });
+ });
+
+});
+
app.get("/:id", function(req, res){
// return an specific post (by ID)
View
@@ -58,6 +58,38 @@ exports.getPost = function(postId, callback) {
});
}
+exports.searchForPosts = function(keyWords, callback) {
+ var escapedKeyWords = escape(keyWords).replace(/%20/g, "%");
+ database.getDatabaseConnection(function (mysql_client) {
+ mysql_client.query("SELECT * FROM posts WHERE title LIKE '%" + escapedKeyWords + "%' || body LIKE '%" + escapedKeyWords + "%';", function (error, results, fields) {
+ if(error) {
+ throw "Error getting search results: " + error;
+ }
+ mysql_client.query("SELECT * FROM users;", function (error1, results1, fields1) {
+ if(error1) {
+ throw "Error getting posts' authors: " + error1;
+ }
+ var posts = new Array();
+ for (var i=0; i < results.length; i++) {
+ var postInfo = results[i];
+ var authorName = "";
+
+ for (var j=0; j < results1.length; j++) {
+ if(results1[j].id == postInfo.user_id) {
+ authorName = results1[j].name;
+ break;
+ }
+ }
+
+ postInfo['user_name'] = authorName;
+ posts.push(postInfo);
+ }
+ callback(posts);
+ });
+ });
+ });
+}
+
exports.createPost = function(title, body, user_id, callback) {
database.getDatabaseConnection(function (mysql_client) {
mysql_client.query("INSERT INTO posts (title, body, user_id) VALUES ('" + escape(title) + "', '" + escape(body) + "', " + user_id + ");", function (error, results, fields) {
View
@@ -5,7 +5,12 @@
<script src="http://code.jquery.com/jquery-1.4.4.js"></script>
</head>
<body>
- <h1>HEADER</h1><br/>
+ <h1>HEADER</h1>
+ <form method="get" action="/search">
+ <input type="text" name="keywords" placeholder="Search term">
+ <input type="submit" value="search">
+ </form>
+ <br/>
<%- body %>
<br/><br/>
<h1>FOOTER</h1>
View
@@ -0,0 +1,13 @@
+<h1>Search results</h1>
+
+<% 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/>
+ <% } %>
+ <% } %>
+<% } %>

0 comments on commit 1b793ba

Please sign in to comment.