-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
125 lines (108 loc) · 3.11 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
'use strict';
// Backend for our Trello clone.
// NPM dependencies
var express = require('express');
var handlebars = require('express-handlebars');
var bodyParser = require('body-parser');
var _ = require('underscore');
// Local dependencies
var storage = require('./storage');
var app = express();
// Locate layouts and partials
app.engine('handlebars', handlebars({
defaultLayout: 'main',
layoutsDir: 'views/layouts/',
partialsDir: 'views/partials/'
}));
// Locate the views
app.set('views', __dirname + '/views');
// Locate the assets
app.use(express.static(__dirname + '/static'));
// Set Handlebars
app.set('view engine', 'handlebars');
// Add POST request parsing for message bodies
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json())
// Index Page
app.get('/', function(request, response, next) {
response.render('index');
});
// Entity functions
function isValid(entity, obj) {
if (! _.isUndefined(obj.id) && ! _.isFinite(obj.id)) {
console.log('Id validation failed', obj.id);
return false;
}
return _.all(_.pairs(entity).map(function(item) {
var name = item[0], validate = item[1];
var ret = validate(obj[name]);
if (! ret) {
console.log('Validation failed for field %s, value: %s', name, obj[name]);
}
return ret;
}));
}
function getFields(entity, obj) {
return _.pick(obj, function(v, k) {
return _.has(entity, k);
});
}
// REST Endpoint: /api/list
// CRUD actions for list
var listApiRouter = express.Router();
app.use('/api/lists', listApiRouter);
// LIST_FIELDS: names of valid entity fields mapped to functions for validating
// contents of fields.
var LIST_FIELDS = {
name: function(name) {
return name && _.isString(name) && name.length;
},
pos: _.isNumber,
cards: function(cards) {
return ! cards || (_.isArray(cards) && _.all(cards, _.isString));
}
};
// GET /api/lists Get all lists
listApiRouter.get('/', function(req, resp, next) {
var result = storage.getAll('list');
if (result) {
resp.json({ rows: result });
} else {
resp.status(404).end();
}
});
// GET /api/lists/:id Get one list
listApiRouter.get('/:id', function(req, resp, next) {
var result = storage.getOne('list', parseInt(req.params.id));
if (result) {
resp.json(result);
} else {
resp.status(404).end();
}
});
// POST /api/lists create new list
listApiRouter.post('/', function(req, resp, next) {
var fields = getFields(LIST_FIELDS, req.body);
fields.pos = parseInt(fields.pos);
if (! isValid(LIST_FIELDS, fields)) {
resp.status(400).end();
} else {
console.log('Create list', fields);
resp.json(storage.upsert('list', fields));
}
});
// POST /api/lists/:id Update existing list
listApiRouter.post('/:id', function(req, resp, next) {
var fields = getFields(LIST_FIELDS, req.body);
fields.pos = parseInt(fields.pos);
if (! isValid(LIST_FIELDS, fields)) {
resp.status(400).end();
} else {
console.log('Update list', fields);
resp.json(storage.upsert('list', fields));
}
});
// Start
var port = process.env.PORT || 3000;
app.listen(port);
console.log('Express started on port ' + port);