-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest.js
301 lines (281 loc) · 12 KB
/
rest.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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
var mongoose = require('mongoose'),
bug = require('./models/bug.js'),
issue = require('./models/issue.js');
function REST_ROUTER(router, md5) {
var self = this;
self.handleRoutes(router, md5, self);
}
REST_ROUTER.prototype.handleRoutes= function(router, md5, self) {
router.get("/",function(req, res){
res.jsonp({
"Message" : "Hello World below is my api :)",
"GET requests": {
"api/bug": "Returns all bugs",
"api/bug/summary": "Returns all bugs with less data",
"api/bug/id": "Returns a bug with a given id",
"api/bug/id/summary": "Returns a bug with a given id and with less data",
"api/issue": "Returns all issues",
"api/issue/summary": "Returns all issues with less data",
"api/issue/id": "Returns a issue with a given id",
"api/issue/id/summary": "Returns a issue with a given id and with less data"
},
"POST requests": {
"api/submitbug": {
"description": "Submits a bug to the web service and expects the following fields",
"fields": {
"exception": {
"description": "An exception thrown by the application, can be null",
"type": "String"
},
"dateTime": {
"description": "The date and time of submission",
"type": "Date"
},
"userDescription": {
"description": "A description the user reporting the bug can fill out",
"type": "String"
},
"progDescription": {
"description": "A description outputted by the program",
"type": "String"
},
"osName": {
"description": "The name of the operating system that the application was run on",
"type": "String"
},
"osVersion": {
"description": "The version of the operating system that the application was run on",
"type": "String"
},
"args": {
"description": "The arguments that the program was running with",
"type": "String"
},
"javaVersion": {
"description": "The version of the java that the applicaion was being run on",
"type": "String"
},
"histUndoPossible": {
"description": "If undo was possible",
"type": "Boolean"
},
"histRedoPossible": {
"description": "If redo was possible",
"type": "Boolean"
},
"navForwardPossible": {
"description": "If navigation forward was possible,",
"type": "String"
},
"navBackwardPossible": {
"description": "If navigation backwards was possible",
"type": "Boolean"
},
"screenshot": {
"description": "A list of images encoded in base64",
"type": "Array"
},
"misc": {
"description": "Other data outputted from the application",
"type": "String"
}
}
}
}
});
});
router.post("/submitbug", function(req, res) {
var issueData = req.body;
var bugSubmission = submitBug(issueData, md5, function (submission) {
if (!submission.result) {
//fixme: Note that passing the exact db error is bad in the real world as this can expose
//internal implementation to hackers, for the purpose of debugging it has been included
res.status(500).jsonp({"error" : true, "message" : "Error submitting bug", "dbError": submission.err});
console.log("Something went wrong: " + submission.err);
}
else {
res.jsonp({"error" : false, "message" : "Bug submitted!"});
}
}.bind(this));
}.bind(self));
router.get("/bug", function(req, res) {
var query = bug.find();
query.select('-__v -issues.__v');
query.exec(function (err, result) {
if (err) {
res.status(500).jsonp({"error" : true, "message" : "error getting bugs", "dbError": err});
}
else {
res.jsonp({"error" : false, "message" : "All the bugs", "data": result});
}
}.bind(this));
}.bind(self));
router.get("/bug/summary", function(req, res) {
var query = bug.find();
query.select('hash ' +
'_id ' +
'issues._id issues.dateTime ' +
'issues.progDescription ' +
'issues.userDescription ' +
'issues.histRedoPossible ' +
'issues.histUndoPossible ' +
'issues.navBackwardPossible ' +
'issues.navForwardPossible');
query.exec(function (err, result) {
if (err) {
res.status(500).jsonp({"error" : true, "message" : "error getting bugs", "dbError": err});
}
else {
res.jsonp({"error" : false, "message" : "All the bugs", "data": result});
}
}.bind(this));
}.bind(self));
router.get("/bug/:id", function (req, res) {
var id = req.params.id;
if (id.length === 24) {
var query = bug.findById(id, function (err, result) {
query.select('-__v -issues.__v');
if (err) {
res.status(500).jsonp({"error" : true, "message" : "error getting bug with id " + req.params.id, "dbError": err});
}
else if (result === null) {
res.status(404).jsonp({"error" : true, "message" : "Bug with id " + id + " was not found"});
}
else {
res.jsonp({"error" : false, "message" : "Found bug with id " + id, "data": result});
}
}.bind(this));
}
else {
res.status(404).jsonp({"error" : true, "message" : "Invalid id"});
}
}.bind(self));
router.get("/bug/:id/summary", function (req, res) {
var id = req.params.id;
if (id.length === 24) {
var query = bug.findById(id,
'hash ' +
'_id ' +
'issues._id issues.dateTime ' +
'issues.progDescription ' +
'issues.userDescription ' +
'issues.histRedoPossible ' +
'issues.histUndoPossible ' +
'issues.navBackwardPossible ' +
'issues.navForwardPossible', function (err, result) {
if (err) {
res.status(500).jsonp({"error" : true, "message" : "error getting bug with id " + req.params.id, "dbError": err});
}
else if (result === null) {
res.status(404).jsonp({"error" : true, "message" : "Bug with id " + id + " was not found"});
}
else {
res.jsonp({"error" : false, "message" : "Found bug with id " + id, "data": result});
}
}.bind(this));
}
else {
res.status(404).jsonp({"error" : true, "message" : "Invalid id"});
}
}.bind(self));
router.get("/issue", function (req, res) {
var query = issue.find();
query.select('-__v');
query.exec(function (err, result) {
if (err) {
res.status(500).jsonp({"error" : true, "message" : "error getting issues", "dbError": err});
}
else {
res.jsonp({"error" : false, "message" : "All issues", "data": result});
}
}.bind(this));
}.bind(self));
router.get("/issue/summary", function (req, res) {
var query = issue.find();
query.select('_id ' +
'dateTime ' +
'progDescription ' +
'userDescription ' +
'histRedoPossible ' +
'histUndoPossible ' +
'navBackwardPossible ' +
'navForwardPossible');
query.exec(function (err, result) {
if (err) {
res.status(500).jsonp({"error" : true, "message" : "error getting issues", "dbError": err});
}
else {
res.jsonp({"error" : false, "message" : "All issues", "data": result});
}
}.bind(this));
}.bind(self));
router.get("/issue/:id", function (req, res) {
var id = req.params.id;
if (id.length === 24) {
var query = issue.findById(id, '-__v', function (err, result) {
if (err) {
res.status(500).jsonp({"error" : true, "message" : "error getting issue with id " + req.params.id, "dbError": err});
}
else if (result === null) {
res.status(404).jsonp({"error" : true, "message" : "Issue with id " + id + " was not found"});
}
else {
res.jsonp({"error" : false, "message" : "Found issue with id " + id, "data": result});
}
}.bind(this));
}
else {
res.status(404).jsonp({"error" : true, "message" : "Invalid id"});
}
}.bind(self));
router.get("/issue/:id/summary", function (req, res) {
var id = req.params.id;
if (id.length === 24) {
var query = issue.findById(id, '_id ' +
'dateTime ' +
'progDescription ' +
'userDescription ' +
'histRedoPossible ' +
'histUndoPossible ' +
'navBackwardPossible ' +
'navForwardPossible', function (err, result) {
if (err) {
res.status(500).jsonp({"error" : true, "message" : "error getting issue with id " + req.params.id, "dbError": err});
}
else if (result === null) {
res.status(404).jsonp({"error" : true, "message" : "Issue with id " + id + " was not found"});
}
else {
res.jsonp({"error" : false, "message" : "Found issue with id " + id, "data": result});
}
}.bind(this));
}
else {
res.status(404).jsonp({"error" : true, "message" : "Invalid id"});
}
}.bind(self));
}
function submitBug(issueData, md5, callback) {
var exceptionHash;
if (issueData.exception == null || issueData.exception == "null") {
exceptionHash = md5(issueData.userDescription);
}
else {
exceptionHash = md5(issueData.exception);
}
var newIssue = new issue(issueData);
newIssue.save(function (err) {
if (err) callback(handleError(err));
}.bind(this));
// check to see if the issue is unique, if it is submit a new bug along with the issue
// if not then update the bug record with the same hash with the new key.
bug.findOneAndUpdate({hash: exceptionHash}, {$push: {issues: newIssue}}, {new: true, upsert: true}, function (err) {
if (err) callback(handleError(err));
}.bind(this));
callback({result: true, err: ""});
}
function handleError(err) {
console.log("Something went wrong: " + err);
return {result: false, err: "something went wrong"};
}
module.exports = REST_ROUTER;