forked from foundersandcoders/elevator-mechanic
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodel.js
122 lines (94 loc) · 3.04 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Require mongoose and other node modules \\
var mongoose = require("mongoose");
var http = require("http");
var url = require("url");
var fs = require("fs");
var ecstatic = require("ecstatic")({root: __dirname + "/public"});
var handler = require('./handler.js');
mongoose.connect("mongodb://foundrymatrix:[email protected]:30827/blog");
// Get notification for connection success or failure \\
var db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error"));
db.once("open", function (callback) {
console.log("connection made");
});
// Define database schema which determines which properties we want to store \\
var userSchema = mongoose.Schema({
username: { type : String , unique : true, required : true },
password: { type : String , unique : false, required : true },
email: { type : String , unique : true, required : false }
});
var tokenSchema = mongoose.Schema({
GUID: Date,
record: Object
});
var blogSchema = mongoose.Schema({
author : String,
title : String,
text : String,
date : {type: Date, default: Date.now }
});
// Adding method to the schema. Have to be defined before schema is compiled \\
userSchema.methods.announce = function() {
var author = this.author ? "Another blog post by " + this.author : "Anonymous blog post";
console.log(author);
};
// Compile schema into a model, which defines the database collection \\
// First argument is collection name, second argument is schema name \\
// Example: save new user to database
var User = mongoose.model("User", userSchema);
var BlogPost = mongoose.model("BlogPost", blogSchema);
var Token = mongoose.model("Token", tokenSchema);
/*
new_user = new User({
username: 'per5333rge3rdsz',
password: '123343' ,
email: '[email protected]'
});
new_user.save(function (err,user){
if (err) return console.error(err);
console.log(user);
});
*/
function createBlogPost(clientdata){
new_blogpost = new BlogPost({
author : clientdata.author,
title : clientdata.title,
text : clientdata.text,
});
new_blogpost.save(function (err,post){
if (err) return console.error(err);
console.log('post is ', post);
});
}
function getBlogPost(value, cb){
console.log('running getBlogPost');
BlogPost.find({_id: value}, function (err, posts){
console.log("model.js getblogpost: " + posts);
cb(posts);
});
}
function updateBlogPost(id, clientdata){
BlogPost.findOneAndUpdate({"_id": id} , { $set: { title:clientdata.title, text:clientdata.text }}, function (err, post){
if (err) {
console.log(err);
}
});
}
function deleteBlogPost(id){
BlogPost.findOneAndRemove({"_id": id}, function (err, post){
if (err) {
console.log(err);
}
});
}
module.exports = {
User: User,
Token: Token,
BlogPost:BlogPost,
updateBlogPost: updateBlogPost,
deleteBlogPost: deleteBlogPost,
createBlogPost: createBlogPost,
getBlogPost: getBlogPost,
BlogPost: BlogPost
};