Skip to content

Commit fe67a60

Browse files
committed
Importing todo-single app for Lab 6.5
1 parent e347c8f commit fe67a60

20 files changed

+1160
-0
lines changed

todo-single/app.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
var restify = require('restify');
2+
3+
var controller = require('./controllers/items');
4+
5+
var db = require('./models/db');
6+
var model = require('./models/items');
7+
8+
model.connect(db.params, function(err) {
9+
if (err) throw err;
10+
});
11+
12+
var server = restify.createServer()
13+
.use(restify.fullResponse())
14+
.use(restify.queryParser())
15+
.use(restify.bodyParser());
16+
17+
controller.context(server, '/todo/api', model);
18+
19+
server.get(/\/todo\/?.*/, restify.serveStatic({
20+
'directory': __dirname,
21+
'default': 'index.html'
22+
}));
23+
24+
var port = process.env.PORT || 8080;
25+
server.listen(port, function (err) {
26+
if (err)
27+
console.error(err);
28+
else
29+
console.log('App is ready at : ' + port);
30+
});
31+
32+
33+
/*
34+
process.on('uncaughtException', function (err) {
35+
console.error(JSON.parse(JSON.stringify(err, ['stack', 'message', 'inner'], 2)))
36+
});
37+
*/
38+

todo-single/controllers/items.js

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
2+
var model = undefined;
3+
4+
exports.context = function(server, path, itemsModel) {
5+
if (!server)
6+
done('has to provide a restify server object');
7+
8+
var context = "/items";
9+
if (path)
10+
context = path + context;
11+
12+
server.get(context + '/', this.list);
13+
server.get(context + '/:id', this.read);
14+
server.get(context + '-count', this.count);
15+
server.post(context + '/', this.save);
16+
server.del(context + '/:id', this.destroy);
17+
18+
model = itemsModel;
19+
};
20+
21+
exports.list = function(req, res, next) {
22+
var page_no = req.query.page || 1;
23+
var sortField = req.query.sortFields || "id";
24+
var sortDirection = req.query.sortDirections || "asc";
25+
26+
model.listAll(page_no, sortField, sortDirection, function(err, items) {
27+
if (err) {
28+
res.send(err);
29+
}
30+
else {
31+
if (items) {
32+
model.countAll(function(err, n) {
33+
if (err) {
34+
res.send(err);
35+
}
36+
else {
37+
if (n) {
38+
var page = {
39+
"currentPage" : page_no,
40+
"list" : items,
41+
"pageSize" : 10,
42+
"sortDirections" : sortDirection,
43+
"sortFields" : sortField,
44+
"totalResults" : n
45+
};
46+
res.json(page);
47+
next();
48+
}
49+
}
50+
});
51+
}
52+
else {
53+
res.send(err);
54+
}
55+
}
56+
})
57+
};
58+
59+
exports.read = function(req, res, next) {
60+
var key = req.params.id;
61+
model.read(key, function(err, item) {
62+
if (err) {
63+
res.send(err);
64+
}
65+
else {
66+
if (item) {
67+
res.json(item);
68+
next();
69+
}
70+
else {
71+
res.send(err);
72+
}
73+
}
74+
})
75+
};
76+
77+
78+
exports.count = function(req, res, next) {
79+
model.countAll(function(err, n) {
80+
if (err) {
81+
res.send(err);
82+
}
83+
else {
84+
var page = {
85+
count: n
86+
};
87+
res.json(page)
88+
next();
89+
}
90+
})
91+
};
92+
93+
94+
exports.save = function(req, res, next) {
95+
if (req.params.id) {
96+
model.update(req.params.id, req.params.description, req.params.done, function(err, item) {
97+
if (err) {
98+
res.send(err);
99+
}
100+
else {
101+
res.json(item);
102+
next();
103+
}
104+
});
105+
}
106+
else {
107+
model.create(req.params.description, req.params.done, function(err, item) {
108+
if (err) {
109+
res.send(err);
110+
}
111+
else {
112+
res.json(item);
113+
next();
114+
}
115+
});
116+
}
117+
};
118+
119+
120+
exports.destroy = function(req, res, next) {
121+
if (req.params.id) {
122+
model.destroy(req.params.id, function(err, item) {
123+
if (err) {
124+
res.send(err);
125+
}
126+
else {
127+
res.json(item);
128+
}
129+
});
130+
}
131+
}

todo-single/models/db.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
module.exports.params = {
3+
dbname: process.env.DATABASE_NAME,
4+
username: process.env.DATABASE_USER,
5+
password: process.env.DATABASE_PASSWORD,
6+
params: {
7+
host: process.env.DATABASE_SVC,
8+
dialect: 'mysql',
9+
}
10+
};

todo-single/models/items.js

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
var Sequelize = require("sequelize");
2+
3+
var Item = undefined;
4+
5+
module.exports.connect = function(params, callback) {
6+
var sequlz = new Sequelize(
7+
params.dbname, params.username, params.password,
8+
params.params);
9+
Item = sequlz.define('Item', {
10+
id: { type: Sequelize.BIGINT,
11+
primaryKey: true, unique: true, allowNull: false,
12+
autoIncrement: true },
13+
description: { type: Sequelize.STRING,
14+
allowNull: true },
15+
done: { type: Sequelize.BOOLEAN,
16+
allowNull: true }
17+
}, {
18+
timestamps: false,
19+
freezeTableName: true
20+
});
21+
22+
if (process.env.DATABASE_INIT == 'true') {
23+
Item.sync({ force: true }).then(function() {
24+
callback();
25+
}).catch(function(err) {
26+
callback(err);
27+
});
28+
}
29+
}
30+
31+
exports.disconnect = function(callback) {
32+
//XXX shouln'd to something to close or release the db connection?
33+
callback();
34+
}
35+
36+
exports.create = function(description, done, callback) {
37+
Item.create({
38+
//id: id,
39+
description: description,
40+
done: (done) ? true : false
41+
}).then(function(item) {
42+
callback(null, item);
43+
}).catch(function(err) {
44+
callback(err);
45+
});
46+
}
47+
48+
exports.update = function(key, description, done, callback) {
49+
Item.find({ where:{ id: key } }).then(function(item) {
50+
if (!item) {
51+
callback(new Error("Nothing found for key " + key));
52+
}
53+
else {
54+
item.updateAttributes({
55+
description: description,
56+
done: (done) ? true : false
57+
}).then(function() {
58+
callback(null, item);
59+
}).error(function(err) {
60+
callback(err);
61+
});
62+
}
63+
}).catch(function(err) {
64+
callback(err);
65+
});
66+
}
67+
68+
69+
exports.read = function(key, callback) {
70+
Item.find({ where:{ id: key } }).then(function(item) {
71+
if (!item) {
72+
callback(new Error("Nothing found for key " + key));
73+
}
74+
else {
75+
//XXX why recreating the item object?
76+
callback(null, {
77+
id: item.id,
78+
description: item.description,
79+
done: item.done
80+
});
81+
}
82+
}).catch(function(err) {
83+
callback(err);
84+
});
85+
}
86+
87+
exports.destroy = function(key, callback) {
88+
Item.find({ where:{ id: key } }).then(function(item) {
89+
if (!item) {
90+
callback(new Error("Nothing found for " + key));
91+
}
92+
else {
93+
item.destroy().then(function() {
94+
callback(null, item);
95+
}).error(function(err) {
96+
callback(err);
97+
});
98+
}
99+
}).catch(function(err) {
100+
callback(err);
101+
});
102+
}
103+
104+
exports.countAll = function(callback) {
105+
Item.findAll({
106+
attributes: [[Sequelize.fn('COUNT', Sequelize.col('id')), 'no_items']]
107+
}).then(function(n) {
108+
callback(null, n[0].get('no_items'));
109+
}).catch(function(err) {
110+
callback(err);
111+
});
112+
}
113+
114+
exports.listAll = function(page, sortField, sortDirection, callback) {
115+
Item.findAll({ offset: 10 * (page - 1), limit: 10, order: [[sortField, sortDirection]] }).then(function(items) {
116+
var theitems = [];
117+
items.forEach(function(item) {
118+
//XXX why recreating the item objects for theitems?
119+
theitems.push({
120+
id: item.id, description: item.description, done: item.done });
121+
});
122+
callback(null, theitems);
123+
}).catch(function(err) {
124+
callback(err);
125+
});
126+
}
127+

todo-single/package.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "todo-single",
3+
"description": "single container version of the todoapp",
4+
"version": "0.0.2",
5+
"private": true,
6+
"author": "Red Hat Training",
7+
"license": "ASL",
8+
"scripts": {
9+
"start": "node app.js"
10+
},
11+
"dependencies": {
12+
"restify": "4.0.3",
13+
"sequelize": "3.14.2",
14+
"mysql": "2.9.0"
15+
}
16+
}

0 commit comments

Comments
 (0)