-
Notifications
You must be signed in to change notification settings - Fork 0
/
prototypeRoutes.js
125 lines (111 loc) · 4.52 KB
/
prototypeRoutes.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
/* JSON Data */
/* required for image uploads */
var fs = require("fs");
module.exports = function(app, mongoExpressAuth, prototypeAPI){
/* returns the creator that is currently logged in's prototypes data */
app.get('/prototypes', function(req,res){
getAccountInfo(mongoExpressAuth, req, res, function(result){
prototypeAPI.getAll(result._id, makeSendResult(res));
});
});
/* publicly accessible prototype object */
app.get('/prototypes/:_id', function(req, res){
var _id = req.params._id;
var username = mongoExpressAuth.getUsername(req);
prototypeAPI.get(_id, makeSendResult(res));
});
/* creator deletes prototype object, note: creator must be
* logged in for this to occur */
app.delete('/prototypes/:_id', function(req, res){
getAccountInfo(mongoExpressAuth, req, res, function(accountInfo){
prototypeAPI.delete(""+accountInfo._id, req.params._id, makeSendResult(res))
});
});
/* CREATING A PROTOTYPE
* This section is split into many parts in line with the user flow */
/* Step 1
* Occurs once in the beginning
* creator submits {"name": "<appname>"}
* server returns {"_id": id, name: <appname>, "creator_id": <creator_id>, "screens":[empty array] } */
app.post('/prototypes/init', function(req, res){
getAccountInfo(mongoExpressAuth, req, res, function(accountInfo){
prototypeAPI.create(""+accountInfo._id, req.body.name, makeSendResult(res));
});
});
/* Step 2
* Occurs variable number of times for each image upload, first image is start screen for the application
* creator submits { "screen_name": <name>, "image": <stringdata> }
* server returns { "screen_id": <screen_id>, "image_path": <screen_path relative to host url> } */
app.put('/prototypes/:_id/addScreen', function(req, res){
console.log(req.body.screen_name);
getAccountInfo(mongoExpressAuth, req, res, function(accountInfo){
prototypeAPI.addScreen(""+accountInfo._id, req.params._id, req.body.screen_name, function(err, screen_id, image_path){
if (err) res.send({ 'err': 'unknown err' });
else {
writeFile(image_path, req.body.image);
res.send({screen_id: screen_id, image_path: image_path});
}
});
});
});
/* Step 3
* Occurs once for each screen in the application
* creator submits { "screen_id": <screen_id>, clickable_areas:[<array of clickable areas>] }
* clickableAreas are of the form {"x" : <x coord>, "y" : <y coord>, "width" : <width>, "height" : <height>, "destination_id" : <screen_id>} */
app.put('/prototypes/:_id/setClickableAreas', function(req,res){
getAccountInfo(mongoExpressAuth, req, res, function(accountInfo){
prototypeAPI.setClickableAreas(""+accountInfo._id, req.params._id, req.body.screen_id, req.body.clickable_areas, makeSendResult(res))
});
});
}
function makeSendResult(res){
return function(err, result){
if (typeof result === 'number')
result = String(result);
if (err)
res.send({ 'err': 'unknown err' });
else
res.send(result);
}
}
/* Based on the request, returns a logged in users object of the form
* {username": "sankalp","hashedPassword": "rKnHBdssI535e94315921228ad36a58416e7ad9a0e",
* "_id": "51686dd9de61933d2f000001"}
*/
function getAccountInfo(mongoExpressAuth, req, res, onSucess){
mongoExpressAuth.checkLogin(req, res, function(err)
{
if (err) res.send(err);
else
{
mongoExpressAuth.getAccount(req, function(err, result)
{
if (err) res.send(err);
else onSucess(result);
});
}
});
}
// Asynchronously read file contents, then call callbackFn
function readFile(filename, defaultData, callbackFn) {
fs.readFile(filename, function(err, data) {
if (err) {
console.log("Error reading file: ", filename);
data = defaultData;
} else {
console.log("Success reading file: ", filename);
}
if (callbackFn) callbackFn(err, data);
});
}
// Asynchronously write file contents, then call callbackFn
function writeFile(filename, data, callbackFn) {
fs.writeFile(filename, data, function(err) {
if (err) {
console.log("Error writing file: ", filename);
} else {
console.log("Success writing file: ", filename);
}
if (callbackFn) callbackFn(err);
});
}