forked from mgargard/researchers-final
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
265 lines (203 loc) · 9.73 KB
/
Copy pathapp.js
File metadata and controls
265 lines (203 loc) · 9.73 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// This is a sample application which you can use as a starting point for your
// project. The only parts you should *need* to change are indicated with `TODO`
// below. However, you are welcome to change more if you wish.
////////////////////////////////////////////////////////////////////////////////
var USER_OR_GROUP_NAME = ''; // TODO: Insert GitHub username or group name.
////////////////////////////////////////////////////////////////////////////////
if (! USER_OR_GROUP_NAME) {
throw new Error(
'You must set your GitHub username or group name in the app.js file');
}
// Import some utility functions.
var utils = require('./utils');
// Create a new web application.
var app = utils.initializeWebApp();
// Connect to your database.
var db = utils.connectToDatabase(USER_OR_GROUP_NAME);
// TODO: Start defining your resource handlers. You may just need to modify the
// examples below, or you may need to add additional handlers by copying,
// pasting, and modifying these examples.
////////////////////////////////////////////////////////////////////////////////
// Example of handling PUT to create or update a resource. /////////////////////
// Here we create or update an item using the ID specified in the URI. /////////
////////////////////////////////////////////////////////////////////////////////
app.put('/parties/:id', // TODO: change to suit your URI design.
function(req, res) {
// Get the item ID from the URI.
var item_id = req.params.id;
// Get the item info that was PUT from the input form.
// See the form in `views/list-parties.ejs`.
var item = req.body.item;
item.type = 'party'; // TODO: change to the type of item you want
// Save the new item to the database, specifying the ID.
db.save(item_id, item, function(err) {
// If there was a database error, return an error status.
if (err) { res.send(err, 500); }
// Otherwise, redirect back to the URI from which the form was submitted.
else { res.redirect('back' ); }
});
}
);
////////////////////////////////////////////////////////////////////////////////
// Example of handling GET of a "collection" resource. /////////////////////////
// Here we list all items of type `party`. /////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
app.get('/parties/', // TODO: change to suit your URI design.
function(req, res) {
var item_type = 'party'; // TODO: change to the type of item you want.
// Get all items of the specified type from the database.
db.getAll(item_type, function(err, items) {
// If there was a database error, return an error status.
if (err) { res.send(err, 500); }
// Otherwise, use the returned data to render an HTML page.
else {
res.render(
'list-parties', // TODO: change to the name of your HTML template.
{ items: items }
);
}
});
}
);
////////////////////////////////////////////////////////////////////////////////
// Example of handling POST to create a resource. //////////////////////////////
// Here we create an item and allow the ID to be created automatically. ////////
////////////////////////////////////////////////////////////////////////////////
app.post('/candidates/', // TODO: change to suit your URI design.
function(req, res) {
// Get the item info that was POSTed from the input form.
// See the form in `views/one-party.ejs`.
var item = req.body.item;
item.type = 'candidate'; // TODO: change to the type of item you want
// Save the new item to the database. (No ID specified, it will be created.)
db.save(item, function(err) {
// If there was a database error, return an error status.
if (err) { res.send(err, 500); }
// Otherwise, redirect back to the URI from which the form was submitted.
else { res.redirect('back' ); }
});
}
);
////////////////////////////////////////////////////////////////////////////////
// Another example of handling PUT to update a resource. ///////////////////////
// Here we update an item using the ID specified in the URI. ///////////////////
////////////////////////////////////////////////////////////////////////////////
app.put('/candidates/:id', // TODO: change to suit your URI design.
function(req, res) {
// Get the item ID from the URI.
var item_id = req.params.id;
// Get the item info that was PUT from the input form.
// See the form in `views/one-candidate.ejs`.
var item = req.body.item;
item.type = 'candidate'; // TODO: change to the type of item you want
// Save the new item to the database, specifying the ID.
db.save(item_id, item, function(err) {
// If there was a database error, return an error status.
if (err) { res.send(err, 500); }
// Otherwise, redirect back to the URI from which the form was submitted.
else { res.redirect('back' ); }
});
}
);
////////////////////////////////////////////////////////////////////////////////
// Another example of handling GET of a "collection" resource. /////////////////
// This time we support filtering the list by some criteria (i.e. searching). //
////////////////////////////////////////////////////////////////////////////////
app.get('/candidates/', // TODO: change to suit your URI design.
function(req, res) {
var item_type = 'candidate'; // TODO: change to the type of item you want.
// Get items of the specified type that match the query.
db.getSome(item_type, req.query, function(err, items) {
// If there was a database error, return an error status.
if (err) { res.send(err, 500); }
// Otherwise, use the returned data to render an HTML page.
else {
res.render(
'list-candidates', // TODO: change to the name of your HTML template.
{ items: items }
);
}
});
}
);
////////////////////////////////////////////////////////////////////////////////
// An example of handling GET of a "single" resource. //////////////////////////
// This handler is more complicated, because we want to show not only the //////
// item requested, but also links to a set of related items. ///////////////////
////////////////////////////////////////////////////////////////////////////////
app.get('/parties/:id', // TODO: change to suit your URI design.
function(req, res) {
var item_type = 'party'; // TODO: change to the type of item you want.
// Get the item ID from the URI.
var item_id = req.params.id;
// Get one item of the specified type, identified by the item ID.
db.getOne(item_type, item_id, function(err, item) {
// If there was a database error, return an error status.
if (err) {
if (err.error == 'not_found') { res.send(404); }
else { res.send(err, 500); }
}
// Otherwise, get the related items associated with this item.
else {
var related_type = 'candidate'; // TODO: change to type of related item.
// Set our query to find the items related to the requested item.
req.query.party = item_id; // TODO: change `party` to reflect the
// relation between the item fetched above
// and the related items to be fetched below.
// Get items of the specified type that match the query.
db.getSome(related_type, req.query, function(err, items) {
// If there was a database error, return an error status.
if (err) { res.send(err, 500); }
// Otherwise, use the returned data to render an HTML page.
else {
res.render(
'one-party', // TODO: change to the name of your HTML template.
{ item: item, related_items: items }
);
}
});
}
});
}
);
////////////////////////////////////////////////////////////////////////////////
// An example of handling GET of a "single" resource. //////////////////////////
// This handler is also complicated, because we want to show not only the //////
// item requested, but also a list of potential related items, so that users ///
// can select from a list when updating the item. //////////////////////////////
////////////////////////////////////////////////////////////////////////////////
app.get('/candidates/:id', // TODO: change to suit your URI design.
function(req, res) {
var item_type = 'candidate'; // TODO: change to the type of item you want.
// Get the item ID from the URI.
var item_id = req.params.id;
// Get one item of the specified type, identified by the item ID.
db.getOne(item_type, item_id, function(err, item) {
// If there was a database error, return an error status.
if (err) {
if (err.error == 'not_found') { res.send(404); }
else { res.send(err, 500); }
}
// Otherwise, get the items potentially related to this item.
else {
var related_type = 'party'; // TODO: change to type of related item.
// Get all items of the specified related type.
db.getAll(related_type, function(err, items) {
// If there was a database error, return an error status.
if (err) { res.send(err, 500); }
// Otherwise, use the returned data to render an HTML page.
else {
res.render(
'one-candidate', // TODO: change to name of your HTML template.
{ item: item, related_items: items }
);
}
});
}
});
}
);
// Handle GET of the "index" resource.
app.get('/', function(req, res) { res.render('index'); });
// Start listening for incoming HTTP connections.
app.listen(process.env.PORT);