-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
169 lines (146 loc) · 3.87 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
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
var express = require('express');
var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
// config mongoose
var dbConfig = require('./config/db');
mongoose.connect(dbConfig.url);
// data schemas
var AppModel = require('./models/appModel').AppModel;
var ImageModel = require('./models/appModel').ImageModel;
var app = express();
// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// routes
app.get('/sawyerssecretroute', function(req, res) {
res.send("This is a secret. Go back.");
});
app.get('/scavengerhunt', function(req, res) {
var object = {
"path": [
{
"id" : 1,
"latitude" : 42.29386,
"longitude" : -71.26483,
"s3id" : "MVI_3146.3gp"
},
{
"id" : 2,
"latitude" : 42.292987,
"longitude" : -71.264039,
"s3id" : "MVI_3145.3gp"
},
{
"id" : 3,
"latitude" : 42.292733,
"longitude" : -71.263977,
"s3id" : "MVI_3144.3gp"
},
{
"id" : 4,
"latitude" : 42.293445,
"longitude" : -71.263481,
"s3id" : "MVI_3147.3gp"
},
{
"id" : 5,
"latitude" : 42.293108,
"longitude" : -71.262802,
"s3id" : "MVI_3141.3gp"
},
{
"id" : 6,
"latitude" : 42.292701,
"longitude" : -71.262054,
"s3id" : "MVI_3140.3gp"
}
]
};
res.json(object);
});
app.post('/userdata/:appId', function(req, res) {
var appId = req.params.appId;
var imageKey = req.body.imageKey;
var imageLocation = req.body.imageLocation
if (imageKey == null || imageLocation == null) {
res.json({"error":"Set the imageKey and imageLocation in the body"});
return;
}
console.log(appId);
// here we shall get the user's data and save it to mongo
AppModel.findOne({ 'appId' : appId }, function(err, a) {
if (err) {
res.sendStatus(500);
return;
}
if (!a) {
var imageModel = new ImageModel();
imageModel.imageKey = imageKey;
imageModel.imageLocation = imageLocation;
var newApp = new AppModel();
newApp.appId = appId;
newApp.userImages = [imageModel]; // TODO: Add image from imagePath, imageLocation
newApp.save(function(err) {
if (err) {
res.sendStatus(500);
return;
}
res.sendStatus(200);
return;
});
} else {
console.log("APP");
var imageModel = new ImageModel();
imageModel.imageKey = imageKey;
imageModel.imageLocation = imageLocation;
a.userImages.push(imageModel);
a.save(function(err) {
if (err) {
res.sendStatus(500);
return;
}
res.sendStatus(200);
return;
});
}
});
});
app.get('/userdata/:appId', function(req, res) {
var appId = req.params.appId;
console.log(appId);
// here we shall get the users data from mongo and return this to the app.
AppModel.findOne({ 'appId' : appId }, function(err, app) {
if (err) {
res.sendStatus(500);
return;
}
if (!app) {
console.log("NO app");
var newApp = new AppModel();
newApp.appId = appId;
newApp.userImages = [];
newApp.save(function(err) {
if (err) {
res.sendStatus(500);
return;
}
res.json({'data':newApp.userImages});
return;
});
} else {
var images = app.userImages;
res.json({'data':images});
return;
}
});
});
var url = "0.0.0.0";
var port = process.env.PORT || 8080;
app.listen(port, url);
console.log('Express started on port ' + port + " at " + url);