diff options
| author | mo khan <mo@mokhan.ca> | 2015-01-24 20:30:50 -0700 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2015-01-24 20:30:50 -0700 |
| commit | d8c60d59b9560123d04de3c601cf3aacb5b9fe6b (patch) | |
| tree | 488321c70c344d4f8533fff29f8e6cea9d8da901 /week-3 | |
| parent | e3f19022ebae4ce898fc76c84180fc35611b4930 (diff) | |
insert new posts.
Diffstat (limited to 'week-3')
| -rw-r--r-- | week-3/hw3-2and3-3/blog/posts.js | 132 |
1 files changed, 70 insertions, 62 deletions
diff --git a/week-3/hw3-2and3-3/blog/posts.js b/week-3/hw3-2and3-3/blog/posts.js index 28593ce..c006ac9 100644 --- a/week-3/hw3-2and3-3/blog/posts.js +++ b/week-3/hw3-2and3-3/blog/posts.js @@ -1,89 +1,97 @@ /* The PostsDAO must be constructed with a connected database object */ function PostsDAO(db) { - "use strict"; + "use strict"; - /* If this constructor is called without the "new" operator, "this" points - * to the global object. Log a warning and call it correctly. */ - if (false === (this instanceof PostsDAO)) { - console.log('Warning: PostsDAO constructor called without "new" operator'); - return new PostsDAO(db); - } + /* If this constructor is called without the "new" operator, "this" points + * to the global object. Log a warning and call it correctly. */ + if (false === (this instanceof PostsDAO)) { + console.log('Warning: PostsDAO constructor called without "new" operator'); + return new PostsDAO(db); + } - var posts = db.collection("posts"); + var posts = db.collection("posts"); - this.insertEntry = function (title, body, tags, author, callback) { + this.insertEntry = function (title, body, tags, author, callback) { + "use strict"; + console.log("inserting blog entry" + title + body); + + // fix up the permalink to not include whitespace + var permalink = title.replace( /\s/g, '_' ); + permalink = permalink.replace( /\W/g, '' ); + + // Build a new post + var post = { + "title": title, + "author": author, + "body": body, + "permalink":permalink, + "tags": tags, + "comments": [], + "date": new Date() + }; + + // now insert the post + posts.insert(post, function (err, result) { "use strict"; - console.log("inserting blog entry" + title + body); - - // fix up the permalink to not include whitespace - var permalink = title.replace( /\s/g, '_' ); - permalink = permalink.replace( /\W/g, '' ); - - // Build a new post - var post = {"title": title, - "author": author, - "body": body, - "permalink":permalink, - "tags": tags, - "comments": [], - "date": new Date()} - - // now insert the post - // hw3.2 TODO - callback(Error("insertEntry NYI"), null); - } - this.getPosts = function(num, callback) { - "use strict"; + if (err) { + return callback(err, null); + } + return callback(null, permalink); + }); + }; - posts.find().sort('date', -1).limit(num).toArray(function(err, items) { - "use strict"; + this.getPosts = function(num, callback) { + "use strict"; - if (err) return callback(err, null); + posts.find().sort('date', -1).limit(num).toArray(function(err, items) { + "use strict"; - console.log("Found " + items.length + " posts"); + if (err) return callback(err, null); - callback(err, items); - }); - } + console.log("Found " + items.length + " posts"); - this.getPostsByTag = function(tag, num, callback) { - "use strict"; + callback(err, items); + }); + } - posts.find({ tags : tag }).sort('date', -1).limit(num).toArray(function(err, items) { - "use strict"; + this.getPostsByTag = function(tag, num, callback) { + "use strict"; - if (err) return callback(err, null); + posts.find({ tags : tag }).sort('date', -1).limit(num).toArray(function(err, items) { + "use strict"; - console.log("Found " + items.length + " posts"); + if (err) return callback(err, null); - callback(err, items); - }); - } + console.log("Found " + items.length + " posts"); - this.getPostByPermalink = function(permalink, callback) { - "use strict"; - posts.findOne({'permalink': permalink}, function(err, post) { - "use strict"; + callback(err, items); + }); + } - if (err) return callback(err, null); + this.getPostByPermalink = function(permalink, callback) { + "use strict"; + posts.findOne({'permalink': permalink}, function(err, post) { + "use strict"; - callback(err, post); - }); - } + if (err) return callback(err, null); - this.addComment = function(permalink, name, email, body, callback) { - "use strict"; + callback(err, post); + }); + } - var comment = {'author': name, 'body': body} + this.addComment = function(permalink, name, email, body, callback) { + "use strict"; - if (email != "") { - comment['email'] = email - } + var comment = {'author': name, 'body': body} - // hw3.3 TODO - callback(Error("addComment NYI"), null); + if (email != "") { + comment['email'] = email } + + // hw3.3 TODO + callback(Error("addComment NYI"), null); + } } module.exports.PostsDAO = PostsDAO; |
