Skip to content

Commit 6a0a41a

Browse files
committed
Merge remote-tracking branch 'origin/create_db'
2 parents f60079e + 60c5808 commit 6a0a41a

File tree

5 files changed

+365
-3
lines changed

5 files changed

+365
-3
lines changed

app.js

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/**
32
* Module dependencies.
43
*/
@@ -13,6 +12,7 @@ var express = require('express')
1312
, LocalStrategy = require('passport-local').Strategy;
1413

1514
var app = express();
15+
var ModelProvider = require('./models/model').ModelProvider;
1616

1717
app.configure(function(){
1818
app.set('port', process.env.PORT || 3000);
@@ -27,13 +27,73 @@ app.configure(function(){
2727
app.use(passport.initialize());
2828
app.use(passport.session());
2929
app.use(app.router);
30+
app.use(require('stylus').middleware(__dirname + '/public'));
3031
app.use(express.static(path.join(__dirname, 'public')));
3132
});
3233

3334
app.configure('development', function(){
3435
app.use(express.errorHandler());
3536
});
3637

38+
app.get('/topic', function(req, res){
39+
modelProvider.findAllTopic(function(error, topics){
40+
res.render('topic_show.jade', {
41+
title: 'Blog',
42+
topic: topics
43+
});
44+
});
45+
})
46+
47+
app.get('/tut/new', function(req, res) {
48+
res.render('new_tut.jade', {
49+
title: 'New Post'
50+
51+
});
52+
});
53+
54+
app.post('/tut/new', function(req, res){
55+
var tut = {};
56+
tut.name = req.body.title;
57+
tut.description = req.body.description;
58+
tut.author = req.body.author;
59+
tut.content = req.body.url;
60+
tut.imageUrl = req.body.imageUrl;
61+
tut.enableTopics = req.body.acquires;
62+
tut.requiredTopics = req.body.requires;
63+
modelProvider.createTutorial(tut, function(err, tut_id) {
64+
var title;
65+
if (err)
66+
title = err;
67+
else
68+
title = tut_id;
69+
console.log(title);
70+
res.render('topic_show.jade', {
71+
title: title,
72+
topic: []
73+
});
74+
});
75+
});
76+
77+
app.get('/blog/:id', function(req, res) {
78+
articleProvider.findById(req.params.id, function(error, article) {
79+
res.render('blog_show.jade',
80+
{
81+
title: article.title,
82+
article:article
83+
});
84+
});
85+
});
86+
87+
app.post('/blog/addComment', function(req, res) {
88+
articleProvider.addCommentToArticle(req.param('_id'), {
89+
person: req.param('person'),
90+
comment: req.param('comment'),
91+
created_at: new Date()
92+
} , function( error, docs) {
93+
res.redirect('/blog/' + req.param('_id'))
94+
});
95+
});
96+
3797
// to use persistent login session
3898
passport.serializeUser(function(user, done) {
3999
console.log('serialize ', user);
@@ -58,7 +118,9 @@ passport.use(new LocalStrategy(
58118
app.get('/', routes.index);
59119

60120
app.get('/tutorial/new', routes.newTutorial);
61-
app.post('/tutorial/new', routes.postNewTutorial);
121+
122+
// TESTING DATABASE, ENABLE IT LATER
123+
//app.post('/tutorial/new', routes.postNewTutorial);
62124

63125
app.get('/learnpath/new', routes.newLearnpath);
64126
app.post('/learnpath/new', routes.postNewLearnpath);
@@ -83,6 +145,10 @@ app.get('/auth',
83145
}
84146
);
85147

148+
86149
http.createServer(app).listen(app.get('port'), function(){
87150
console.log("Express server listening on port " + app.get('port'));
88151
});
152+
153+
var mongoose = require('mongoose');
154+
var modelProvider = new ModelProvider(mongoose, '/localhost', 27017);

models/model.js

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
2+
ModelProvider = function(mongoose, host, port){
3+
console.log("Trying to connect to db");
4+
this.db = mongoose.connect('mongodb://localhost/learnpath');
5+
var self = this;
6+
console.log(typeof self);
7+
createSchema(mongoose, function () {
8+
self.User = mongoose.model('User');
9+
console.log("it runs here!!!!!");
10+
console.log(typeof self);
11+
console.log(typeof self.User);
12+
self.Topic = mongoose.model('Topic');
13+
self.Tutorial = mongoose.model('Tutorial');
14+
self.Path = mongoose.model('Path');
15+
});
16+
};
17+
18+
exports.ModelProvider = ModelProvider;
19+
20+
function createSchema(mongoose, fn) {
21+
var Schema = mongoose.Schema;
22+
var ObjectId = Schema.ObjectId;
23+
var userSchema = new Schema({
24+
user_name: String,
25+
user_email: String,
26+
listLearnedPath:
27+
[
28+
{
29+
pathId: ObjectId,
30+
numFinished: Array,
31+
numSkipped: Array
32+
}
33+
],
34+
listLearnedTopic: [
35+
{
36+
topicId: ObjectId
37+
}
38+
],
39+
listLearnedTutorial: [
40+
{
41+
tutorialId: ObjectId
42+
}
43+
],
44+
listLearnedPath: [
45+
{
46+
pathId: ObjectId
47+
}
48+
]});
49+
50+
userSchema.virtual('id')
51+
.get(function() {
52+
return this._id.toHexString();
53+
});
54+
55+
var User = mongoose.model('User', userSchema);
56+
57+
var topicSchema = new Schema( {
58+
name: String,
59+
description: String,
60+
listRelatedTutorial: [
61+
{
62+
tutorialId: ObjectId,
63+
}
64+
],
65+
});
66+
67+
topicSchema.virtual('id')
68+
.get(function() {
69+
return this._id.toHexString();
70+
});
71+
72+
var Topic = mongoose.model('Topic', topicSchema);
73+
74+
var tutorialSchema = new Schema( {
75+
name: String,
76+
description: String,
77+
author: String,
78+
content: String,
79+
type: String,
80+
imageUrl: String,
81+
level: Number,
82+
enableTopics: [
83+
{
84+
topicId: ObjectId
85+
}
86+
],
87+
requiredTopics: [
88+
{
89+
topicId: ObjectId
90+
}
91+
]
92+
});
93+
94+
95+
var Tutorial = mongoose.model('Tutorial', tutorialSchema);
96+
97+
var pathSchema = new Schema( {
98+
name: String,
99+
description: String,
100+
content: [
101+
{
102+
step: Number,
103+
type: String,
104+
itemId: ObjectId,
105+
}
106+
]
107+
});
108+
109+
var Path = mongoose.model('Path', pathSchema);
110+
111+
fn();
112+
}
113+
114+
115+
ModelProvider.prototype.findAllTopic = function(callback) {
116+
this.Topic.find(function(err, topics){
117+
if (err)
118+
callback(err);
119+
else {
120+
callback(null, topics);
121+
}
122+
});
123+
}
124+
125+
ModelProvider.prototype.findTopicById = function(id, callback) {
126+
this.Topic.findById(id, function(err, tuts) {
127+
if (err)
128+
callback(err);
129+
else {
130+
callback(null, tuts);
131+
}
132+
});
133+
}
134+
135+
136+
ModelProvider.prototype.findTutorialById = function(id, callback) {
137+
this.Tutorial.findById(id, function(err, tuts) {
138+
if (err)
139+
callback(err);
140+
else {
141+
callback(null, tuts);
142+
}
143+
});
144+
}
145+
146+
ModelProvider.prototype.findTutorialByTopicId = function(topicId, number, callback) {
147+
var topic = findTopicById(topicId, function(err, tut) {
148+
var number = number || 10;
149+
if (err)
150+
callback(err);
151+
else {
152+
tutIds = topic.listRelatedTutorial;
153+
var listTuts = [];
154+
for (var id in tutIds) {
155+
findTutorialById(id, function(err, tut) {
156+
if (!err) {
157+
listTuts.push(tut);
158+
}
159+
});
160+
}
161+
callback(null, listTuts);
162+
}
163+
});
164+
}
165+
166+
ModelProvider.prototype.createTopic = function(top, callback) {
167+
var topic = this.Topic();
168+
topic.name = top.name;
169+
topic.description = top.description;
170+
topic.listRelatedTutorial = [];
171+
topic.save(function(err, topic) {
172+
if (err)
173+
callback(err);
174+
else {
175+
callback(null, topic.toObject()._id.toHexString());
176+
}
177+
});
178+
}
179+
180+
ModelProvider.prototype.createTutorial = function(tut, callback) {
181+
var tutorial = this.Tutorial();
182+
tutorial.name = tut.name;
183+
tutorial.description = tut.description;
184+
tutorial.author = tut.author || '';
185+
tutorial.content = tut.content;
186+
tutorial.type = tut.type;
187+
tutorial.imageUrl = tut.imageUrl;
188+
tutorial.level = tut.level;
189+
tutorial.enableTopics = [];
190+
191+
tutorial.requiredTopics = [];
192+
console.log(tutorial);
193+
tutorial.save(function(err, prod) {
194+
console.log(err);
195+
if (err)
196+
callback(err);
197+
else {
198+
callback(null, tutorial._id.toHexString());
199+
}
200+
});
201+
}
202+

models/topic.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
var Db = require('mongodb').Db;
2+
var Connection = require('mongodb').Connection;
3+
var Server = require('mongodb').Server;
4+
var BSON = require('mongodb').BSON;
5+
var ObjectID = require('mongodb').ObjectID;
6+
7+
8+
ArticleProvider = function(host, port){
9+
this.db= new Db('node-mongo-blog', new Server(host, port, {auto_reconnect: true}, {}));
10+
this.db.open(function(){});
11+
};
12+
13+
ArticleProvider.prototype.getCollection= function(callback) {
14+
this.db.collection('articles', function(error, article_collection) {
15+
if( error ) callback(error);
16+
else callback(null, article_collection);
17+
});
18+
};
19+
20+
ArticleProvider.prototype.findAll = function(callback) {
21+
this.getCollection(function(error, article_collection) {
22+
if( error ) callback(error)
23+
else {
24+
article_collection.find().toArray(function(error, results) {
25+
if( error ) callback(error)
26+
else callback(null, results)
27+
});
28+
}
29+
});
30+
};
31+
32+
ArticleProvider.prototype.findById = function(id, callback) {
33+
this.getCollection(function(error, article_collection) {
34+
if( error ) callback(error)
35+
else {
36+
article_collection.findOne({_id: article_collection.db.bson_serializer.ObjectID.createFromHexString(id)}, function(error, result) {
37+
if( error ) callback(error)
38+
else callback(null, result)
39+
});
40+
}
41+
});
42+
};
43+
44+
45+
ArticleProvider.prototype.save = function(articles, callback) {
46+
this.getCollection(function(error, article_collection) {
47+
if( error ) callback(error)
48+
else {
49+
if( typeof(articles.length)=="undefined")
50+
articles = [articles];
51+
52+
for( var i =0;i< articles.length;i++ ) {
53+
article = articles[i];
54+
article.created_at = new Date();
55+
if( article.comments === undefined ) article.comments = [];
56+
for(var j =0;j< article.comments.length; j++) {
57+
article.comments[j].created_at = new Date();
58+
}
59+
}
60+
61+
article_collection.insert(articles, function() {
62+
callback(null, articles);
63+
});
64+
}
65+
});
66+
};
67+
68+
69+
ArticleProvider.prototype.addCommentToArticle = function(articleId, comment, callback) {
70+
this.getCollection(function(error, article_collection) {
71+
if( error ) callback( error );
72+
else {
73+
article_collection.update(
74+
{_id: article_collection.db.bson_serializer.ObjectID.createFromHexString(articleId)},
75+
{"$push": {comments: comment}},
76+
function(error, article){
77+
if( error ) callback(error);
78+
else callback(null, article)
79+
});
80+
}
81+
});
82+
};
83+
84+
exports.ArticleProvider = ArticleProvider;

0 commit comments

Comments
 (0)