-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
192 lines (149 loc) · 4.23 KB
/
app.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
* author: Masud
* Module dependencies.
*/
var express = require('express');
var cradle = require('cradle');
var app = module.exports = express.createServer();
var conn = new(cradle.Connection)();
var db = conn.database('test');
var mysql = require("mysql");
// First you need to create a connection to the db
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "root",
database: "nodejs_todos"
});
// Configuration
app.configure(function() {
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({ secret: "asdfghjkl" }));
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.dynamicHelpers({ messages: require('express-messages') });
app.configure('development', function() {
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function() {
app.use(express.errorHandler());
});
// Routes
app.get('/', function(req, res) {
var flash = req.flash();
res.render('index', {
title: 'TODO APP',
flashes: flash.error ? flash.error : null
});
});
app.get('/items', function(req, res) {
// con.connect();
con.query('SELECT * FROM items', function(err, rows, fields) {
if(err) throw err;
console.log('Data received from Db:\n');
console.log(rows);
res.render('items', {
title: 'List of TODO item.',
items: rows
});
});
// con.end();
});
app.get('/items/new', function(req, res) {
var item = {name: '', description: ''};
res.render('new-form', {
title: 'Add todo item',
item: item
});
});
app.post('/items/new', function(req, res) {
var item = req.body;
console.log(req);
con.query('INSERT INTO items SET ?', item, function(err, res) {
if (err) {
console.log(err);
req.flash('err', 'Unable to create new item', err.reason);
res.redirect('/items/new');
return;
}
console.log('Last insert ID:');
req.flash('info', 'Successfully added!');
res.redirect('/items');
});
});
app.get('/edit/:id', function(req, res) {
var id = parseInt(req.params.id);
console.log(id);
// con.connect();
con.query('SELECT * FROM items Where ID = ?', [id], function (err, result) {
if (err) {
console.log(err);
req.flash('err', 'Item %s does not exist', req.params.id);
res.redirect('/items');
return;
}
console.log(result)
res.render('edit-form', {
title: 'Edit todo item #' + req.params.id,
item: result[0]
});
});
// con.end();
});
app.post('/edit/:id', function(req, res) {
var id = parseInt(req.params.id);
var item = req.body;
console.log(id, item);
con.query('SELECT * FROM items Where ID = ?', [id], function (err, result) {
if (err) {
console.log(err);
req.flash('err', 'Item %s does not exist', req.params.id);
res.redirect('/items');
return;
}
console.log(result)
con.query('UPDATE items SET name= ?, description = ? Where ID = ?', [item.name, item.description, id], function (err, result) {
if (err) {
console.log(err);
req.flash('error', 'Database error: %s', err.reason);
res.redirect('/edit/' + req.params.id);
return;
}
console.log('Changed ' + result.changedRows + ' rows');
req.flash('info', 'Successfully saved');
res.redirect('/items');
});
});
});
app.get('/items/:id', function(req, res, next) {
var id = parseInt(req.params.id);
console.log(id);
con.query('SELECT * FROM items Where ID = ?', [id], function (err, result) {
if (err) {
console.log(err);
req.flash('err', 'Item %s does not exist', req.params.id);
res.redirect('/items');
return;
}
console.log(result)
con.query('DELETE FROM items WHERE id = ?', [id], function (err, result) {
if (err) {
req.flash('error', 'Database error: %s', err.reason);
res.redirect('/items');
return;
}
console.log('Deleted ' + result.affectedRows + ' rows');
});
});
req.flash('info', 'Successfully Deleted!');
res.redirect('/items');
// next(new Error('not implemented'));
});
app.listen(4000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
// vim:ts=4