Skip to content

Commit 688a0cf

Browse files
committed
add tutorial.js agian
1 parent e8e15ee commit 688a0cf

File tree

2 files changed

+121
-4
lines changed

2 files changed

+121
-4
lines changed

app.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
var express = require('express')
66
, routes = require('./routes')
77
, user = require('./routes/user')
8+
, tutorial = require('./routes/tutorial')
89
, search = require('./routes/search')
910
, http = require('http')
1011
, path = require('path')
@@ -117,10 +118,10 @@ passport.use(new LocalStrategy(
117118

118119
app.get('/', routes.index);
119120

120-
app.get('/tutorial/new', routes.newTutorial);
121+
app.get('/tutorial/new', tutorial.newTutorial);
121122

122123
// TESTING DATABASE, ENABLE IT LATER
123-
app.post('/tutorial/new', routes.postNewTutorial);
124+
app.post('/tutorial/new', tutorial.postNewTutorial);
124125

125126
app.get('/learnpath/new', routes.newLearnpath);
126127
app.post('/learnpath/new', routes.postNewLearnpath);
@@ -129,13 +130,16 @@ app.post('/learnpath/new', routes.postNewLearnpath);
129130
app.get('/login', routes.login);
130131
app.get('/users/:uid/profile', routes.profile);
131132
app.get('/learnpath/:lid', routes.learnpath);
132-
app.get('/tutorial/:tid', routes.tutorial);
133+
app.get('/tutorial/:tid', tutorial.tutorial);
133134
app.get('/profile/:uid', user.profile);
134135
app.get('/search', search.list);
135136
app.get('/topic_hint', routes.topic_hint);
136137

137138

138-
app.post('/tutorial/:tid/comment',routes.postTutorialComment);
139+
app.post('/tutorial/:tid/comment',tutorial.postTutorialComment);
140+
app.post('/tutorial/:tid/like',tutorial.postTutorialLike);
141+
app.post('/tutorial/:tid/share',tutorial.postTutorialShare);
142+
139143
app.post('/learnpath/:lid/comment',routes.postLearnpathComment);
140144

141145
app.get('/auth',

routes/tutorial.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
var modelProvider = require('../models/model').instance;
2+
3+
4+
exports.tutorial = function(req, res) {
5+
var tutorialId = req.params.tid;
6+
7+
var tut = {
8+
id: tutorialId,
9+
title: 'NodeJS web basic',
10+
createdBy: 'Hung Doan',
11+
star: 5,
12+
desc: "In this tutorial I will run you through the process setting up an Express.js app and making it do what a basic website might do. You will learn the basics of routes, views, Jade templates, Stylus CSS engine, handling POST and GET requests.",
13+
url: 'http://www.hacksparrow.com/express-js-tutorial.html',
14+
stat: {
15+
likes: 103,
16+
shares: 50,
17+
views: 1042
18+
},
19+
requires: [
20+
'javascript-basic',
21+
'nodejs-basic',
22+
'javascript',
23+
'nodejs',
24+
],
25+
acquires: [
26+
'nodejs-intermediate',
27+
'expressjs-basic',
28+
'nodejs-basic',
29+
'javascript',
30+
],
31+
comments: [
32+
{poster:
33+
{name: 'Hung Doan',
34+
profile_url: 'https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash4/371019_697811725_247259128_q.jpg'},
35+
content: 'Hello world!'}
36+
]
37+
};
38+
if (!tutorialId) {
39+
res.send("Error");
40+
} else {
41+
// fetch tutorial based on id
42+
modelProvider.findTutorialById (tutorialId , function (err, tutorial){
43+
if (err != null)
44+
res.send(err)
45+
console.log(tutorial);
46+
tut.id = tutorial.id;
47+
tut.title = tutorial.name;
48+
tut.desc = tutorial.description;
49+
tut.url = tutorial.content;
50+
res.render('tutorial', {
51+
title: tut.title,
52+
tut: tut
53+
});
54+
55+
})
56+
}
57+
}
58+
59+
60+
61+
62+
63+
exports.newTutorial = function(req, res){
64+
console.log("new tut ");
65+
res.render('new_tut', {});
66+
};
67+
68+
exports.postNewTutorial = function(req, res){
69+
var tut = {};
70+
tut.name = req.body.title;
71+
tut.description = req.body.description;
72+
tut.author = req.body.author;
73+
tut.content = req.body.url;
74+
tut.imageUrl = req.body.imageUrl;
75+
tut.enableTopics = req.body.acquires;
76+
tut.requiredTopics = req.body.requires;
77+
modelProvider.createTutorial(tut, function(err, tut_id) {
78+
var title;
79+
if (err)
80+
title = err;
81+
else
82+
title = tut_id;
83+
res.redirect('/tutorial/'+tut_id);
84+
});
85+
86+
};
87+
88+
exports.postTutorialComment = function(req, res){
89+
90+
var comment = req.body.comment;
91+
var tutorialId = req.params.tid;
92+
console.log("comment " + comment + " tutorial id " +tutorialId);
93+
res.send("respond with a resource");
94+
};
95+
96+
97+
exports.postTutorialLike = function(req, res){
98+
99+
var comment = req.body.comment;
100+
var tutorialId = req.params.tid;
101+
console.log("comment " + comment + " tutorial id " +tutorialId);
102+
res.send("respond with a resource");
103+
};
104+
105+
106+
exports.postTutorialShare = function(req, res){
107+
108+
var comment = req.body.comment;
109+
var tutorialId = req.params.tid;
110+
console.log("comment " + comment + " tutorial id " +tutorialId);
111+
res.send("respond with a resource");
112+
};
113+

0 commit comments

Comments
 (0)