-
Notifications
You must be signed in to change notification settings - Fork 3
/
model.js
93 lines (77 loc) · 2.42 KB
/
model.js
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//var db = require ('mongojs').connect('mongodb://gregaubs:[email protected]:41157/hapiblog',['blogcollection', 'usercollection']);
var db = require ('mongojs').connect('mongodb://foundry:[email protected]:37581/hapiblog', ['blogcollection', 'usercollection']);
function user (name,auth_method,auth_id,password){
this.username = name;
this.auth_method = auth_method;
this.auth_id = auth_id;
this.password = password;
}
//var blogpost1 = new blogpost("Second", "lorem ispum2", "18 March 2015", "Greg", 2);
/*
var user1 = new user("per","123","");
db.usercollection.save(user1, function(err,user){
if (err){
console.log(err);
}
else{
console.log(user,' has been saved');
}
})
*/
/*
db.blogcollection.save(blogpost1, function(err, savedBlog){
if(err || !savedBlog) console.log("not saved because of ", err);
else console.log(savedBlog.title, " has been saved");
});
*/
function blogpost (title, text, date, author, category, auth_id, blog_id){
this.title = title;
this.text = text;
this.date = date;
this.author = author;
this.category = category;
this.auth_id = auth_id;
this.blog_id = blog_id;
}
// var blogpost1 = new blogpost("Second", "lorem ispum2", "18 March 2015", "Greg", 2);
// db.blogcollection.save(blogpost1, function(err, savedBlog){
// if(err || !savedBlog) console.log("not saved because of ", err);
// else console.log(savedBlog.title, " has been saved");
// });
function saveBlog(title, text, author, category, auth_id){
var blog_id;
var options = {"sort": [["blog_id",-1]], "limit": 1}
db.blogcollection.find({}, options, function(err,blog){
blog_id = blog[0].blog_id + 1;
var newBlogObject = new blogpost(title, text, '', author, category, auth_id, blog_id);
db.blogcollection.save(newBlogObject, function(err, savedBlog){
if(err || !savedBlog) console.log("not saved because of ", err);
});
});
}
function readBlog(blogid, reply){
db.blogcollection.find( { blog_id: blogid }, function(err, fetchedBlog){
if( err || !fetchedBlog) {
console.log("No such blog found");
} else {
reply(fetchedBlog);
}
});
}
function viewBlogs(category, reply){
db.blogcollection.find( { category: category }, function(err, fetchedBlog){
if( err || !fetchedBlog){
console.log("No such blog found");
} else {
reply(fetchedBlog);
}
});
}
module.exports = {
saveBlog : saveBlog,
readBlog : readBlog,
db:db,
user:user,
blogpost:blogpost,
viewBlogs:viewBlogs
}