-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtodo_list.js
195 lines (180 loc) · 6.02 KB
/
todo_list.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
192
193
194
195
'use strict';
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var ITEMID = 0;
var PORT = 3001;
var HOST = 'localhost';
var BASE_PREFIX = 'http://' + HOST + ':' + PORT
var TODOS_URL = BASE_PREFIX + '/to-dos';
var TODOS = {
_self: TODOS_URL,
kind: 'TodoList',
items: BASE_PREFIX + '/items'
}
var ITEMS = {
_self: BASE_PREFIX + '/items',
kind: 'Collection',
items: [],
item_type: 'Item'
}
app.use(bodyParser.json()); // for parsing application/json
app.get('/to-dos', function(req, res) {
var accept_type = req.get('Accept');
if (typeof accept_type == 'undefined' || accept_type === '*/*'|| accept_type === 'application/json') {
res.set('Content-Type', 'application/json');
res.set('Content-Location', TODOS._self);
res.json(TODOS);
} else {
res.status(406).send('Unrecognized accept header media type: ' + accept_type)
}
});
function getItems(req, res) {
var accept_type = req.get('Accept');
if (typeof accept_type == 'undefined' || accept_type === '*/*'|| accept_type === 'application/json') {
res.set('Content-Type', 'application/json');
res.set('Content-Location', ITEMS._self);
res.json(ITEMS);
} else {
res.status(406).send('Unrecognized accept header media type: ' + accept_type)
}
}
app.get('/to-dos/items', getItems);
app.get('/items', getItems);
function getItem(req, res) {
var accept_type = req.get('Accept');
if (typeof accept_type == 'undefined' || accept_type === '*/*'|| accept_type === 'application/json') {
var itemid = req.params.itemid;
var items = ITEMS.items;
var item = null;
for (var i = 0; i < items.length; i++) {
if (items[i].id == itemid) {
item = items[i];
break;
}
}
if (item !== null) {
res.set('Content-Type', 'application/json');
res.set('Content-Location', item._self);
res.set('ETag', item._etag);
res.status(200);
res.json(item);
} else {
res.status(404).send('Not Found')
}
} else {
res.status(406).send('Unrecognized accept header media type: ' + accept_type)
}
}
app.get('/item/:itemid', getItem);
app.get('/to-dos/items;:itemid', getItem);
function patchItem(req, res) {
var accept_type = req.get('Accept');
if (typeof accept_type == 'undefined' || accept_type === '*/*'|| accept_type === 'application/json') {
var content_type = req.get('Content-Type');
if (typeof content_type == 'undefined' || content_type === 'application/json') {
var itemid = req.params.itemid;
var items = ITEMS.items;
var item = null;
for (var i = 0; i < items.length; i++) {
if (items[i].id == itemid) {
item = items[i];
break;
}
}
if (item !== null) {
var changes = req.body;
var error = null;
for (var property in changes) {
if (property.indexOf('_') == 0 || property == 'kind' || property == 'id') {
res.status(400);
res.set('Content-Type', 'application/json');
res.json({'text': 'Cannot modify property '+ property});
error = 400;
break;
}
}
if (error == null) {
for (var property in changes) {
item[property] = changes[property]
}
item._etag++
res.set('Content-Type', 'application/json');
res.set('Content-Location', item._self);
res.set('ETag', item._etag);
res.status(200);
res.json(item);
}
} else {
res.status(404).send('Not Found')
}
} else {
res.status(406).send({text: 'Unrecognized Content-Type header media type: ' + content_type})
}
} else {
res.status(406).send('Unrecognized Accept header media type: ' + accept_type)
}
}
app.patch('/item/:itemid', patchItem);
app.patch('/to-dos/items;:itemid', patchItem);
function deleteItem(req, res) {
var accept_type = req.get('Accept');
if (typeof accept_type == 'undefined' || accept_type === '*/*'|| accept_type === 'application/json') {
var content_type = req.get('Content-Type');
if (typeof content_type == 'undefined' || content_type === 'application/json') {
var itemid = req.params.itemid;
var items = ITEMS.items;
var item = null;
for (var i = 0; i < items.length; i++) {
if (items[i].id == itemid) {
item = items.splice(i,1)[0]
break;
}
}
if (item !== null) {
res.set('Content-Type', 'application/json');
res.set('Content-Location', item._self);
res.set('ETag', item._etag);
res.status(200);
res.json(item);
} else {
res.status(404).send('Not Found')
}
} else {
res.status(406).send({text: 'Unrecognized Content-Type header media type: ' + content_type})
}
} else {
res.status(406).send('Unrecognized Accept header media type: ' + accept_type)
}
}
app.delete('/item/:itemid', deleteItem);
app.delete('/to-dos/items;:itemid', deleteItem);
function postItem(req, res) {
var accept_type = req.get('Accept');
if (typeof accept_type == 'undefined' || accept_type === '*/*'|| accept_type === 'application/json') {
var content_type = req.get('Content-Type');
if (typeof content_type == 'undefined' || content_type === 'application/json') {
var item = req.body;
if ('kind' in item) {
var itemid = ITEMID++;
item._self = BASE_PREFIX + '/item/' + itemid.toString();
item.id = itemid.toString();
item._etag = 0;
ITEMS.items.push(item);
res.set('ETag', item._etag);
res.set('Location', item._self);
res.status(201).json(item)
} else {
res.status(400).send({text: 'No kind set for Item'})
}
} else {
res.status(406).send({text: 'Unrecognized Content-Type header media type: ' + content_type})
}
} else {
res.status(406).send('Unrecognized Accept header media type: ' + accept_type)
}
}
app.post('/to-dos/items', postItem);
app.post('/items', postItem);
console.log('Listening on %d', PORT);
app.listen(PORT);