-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathposts.js
More file actions
74 lines (48 loc) · 1.9 KB
/
Copy pathposts.js
File metadata and controls
74 lines (48 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
function PostsDAO(db) {
if (false === (this instanceof PostsDAO)) {
console.log('Warning: PostsDAO constructor called without "new" operator');
return new PostsDAO(db);
}
var posts = db.collection("posts");
this.insertEntry = function (title, body, tags, author, callback) {
console.log("inserting blog entry" + title + body);
var permalink = title.replace( /\s/g, '_' );
permalink = permalink.replace( /\W/g, '' );
var post = {"title": title,
"author": author,
"body": body,
"permalink":permalink,
"tags": tags,
"comments": [],
"date": new Date()}
callback(Error("insertEntry NYI"), null);
}
this.getPosts = function(num, callback) {
posts.find().sort('date', -1).limit(num).toArray(function(err, items) {
if (err) return callback(err, null);
console.log("Found " + items.length + " posts");
callback(err, items);
});
}
this.getPostsByTag = function(tag, num, callback) {
posts.find({ tags : tag }).sort('date', -1).limit(num).toArray(function(err, items) {
if (err) return callback(err, null);
console.log("Found " + items.length + " posts");
callback(err, items);
});
}
this.getPostByPermalink = function(permalink, callback) {
posts.findOne({'permalink': permalink}, function(err, post) {
if (err) return callback(err, null);
callback(err, post);
});
}
this.addComment = function(permalink, name, email, body, callback) {
var comment = {'author': name, 'body': body}
if (email != "") {
comment['email'] = email
}
callback(Error("addComment NYI"), null);
}
}
module.exports.PostsDAO = PostsDAO;