-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
296 lines (272 loc) · 7.96 KB
/
server.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
/*global path, err*/
/*jslint node:true */
/*jslint nomen: true */
var port = '8002';
var express = require('express');
var app = express();
var ejs = require('./download-pdf/ejs');
var pdf = require('./download-pdf/pdf');
var compression = require('compression');
// app.set('view engine', 'ejs');
app.use(compression());
app.use(express.static(__dirname));
app.set("view engine", "ejs");
//For parsing json files
app.use(express.json()); // for parsing application/json
app.use(express.urlencoded({
extended: true
})); // for parsing application/x-www-form-urlencoded
//Start Mongoose connection to db called pf_users
var mongoose = require('mongoose');
/*
Standard format: mongoose.connect('mongodb://username:password@host:port/database')
Other format: mongoose.connect('mongodb://host:port/database');
*/
// mongoose.connect("mongodb://localhost:27017/pf_users", {useNewUrlParser: true});
mongoose.connect("mongodb+srv://phoebe:[email protected]/pf_users", {
useNewUrlParser: true
});
// Create Route Schema for stored route data
var openingHoursSchema = new mongoose.Schema({
weekday_text: [String]
// open_now: {type: Boolean, default: true}
});
// Create Place Schema for stored route data
var locationSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
formatted_address: {
type: String,
required: true
},
lat: {
type: Number,
required: true
},
lng: {
type: Number,
required: true
},
place_id: {
type: String,
required: true
},
photos: {
type: [String],
required: true
},
rating: {
type: Number,
default: null
},
opening_hours: openingHoursSchema
});
// Create User Schema for stored user data
var userSchema = new mongoose.Schema({
name: String,
id: Number,
email: String
});
// Create Route Schema for stored route data
var routeSchema = new mongoose.Schema({
user_email: {
type: String,
required: true
},
route: {
type: [locationSchema],
required: true
},
created_at: {
type: Date,
required: true,
default: Date.now
}
});
// Create Feedback Schema
var feedbackSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
feedback: {
type: String,
required: true
}
});
// Create collections named users and routes
var User = mongoose.model("User", userSchema);
var Route = mongoose.model("Route", routeSchema);
var Feedback = mongoose.model("Feedback", feedbackSchema);
var user = "";
// Save user to mongodb, check if already registered first
app.post('/save-user', function (req) {
"use strict";
console.log("Logged in as " + req.body.email);
user = new User({
name: req.body.name,
id: req.body.id,
email: req.body.email
});
// Check if user has been registered before
User.find({
email: req.body.email
}, function (err, user_found) {
if (err) {
console.log(err);
} else if (user_found.length === 0) {
console.log("Registering new user " + req.body.name + "...");
user.save();
} else {
console.log("This user has already been registered. This is his/her record.");
console.log(user_found);
}
});
});
// Save path to mongodb, check for existing identical paths first
app.post('/save-path', function (req, res) {
"use strict";
// console.log(req.body);
var route = new Route({
user_email: user.email,
route: req.body.locations
});
Route.find({
user_email: user.email
}, function (err, routes_found) {
if (err) {
console.log(err);
// No routes saved before, save first ever route
} else if (routes_found.length === 0) {
console.log("Saving first ever route..." + route);
route.save();
// Check for identical existing paths
} else if (routes_found.length > 0) {
var savedBefore = false,
savedRecord;
routes_found.forEach(function (route) {
if (JSON.stringify(route.route, ["name"]) === JSON.stringify(req.body.locations, ["name"])) {
savedBefore = true;
savedRecord = route;
}
});
// Path has been saved before
if (savedBefore) {
console.log("This route has already been saved before. Record is as below");
console.log(savedRecord);
} else {
// Path has not been saved before
console.log("Not saved before");
// Check if locations are defined
if (req.body.locations !== undefined) {
console.log("Saving the route which contains the following locations... ");
req.body.locations.forEach(function (location) {
if (location) {
console.log(location.name);
}
});
console.log(route);
route.save();
} else {
console.log("No locations entered");
}
}
} else {
console.log("Error: Locations undefined. No locations have been entered");
}
});
res.redirect('/');
});
// Find path by its _id to delete it
app.post('/delete-path', function (req) {
"use strict";
Route.deleteOne({
_id: req.body._id
}, function (err) {
if (err) {
console.log(err);
} else {
console.log("Deleted route with id " + req.body._id);
}
});
});
// Find path by its _id to update it
app.post('/update-path', function (req) {
"use strict";
Route.updateOne({
_id: req.body._id
}, {
route: req.body.locations
}, function (err, resp) {
if (err) {
console.log(err);
} else {
console.log(resp);
}
});
});
// Find all paths saved by the user
app.get('/paths', function (resp, res) {
"use strict";
// console.log(resp);
if (user.email !== undefined) {
Route.find({
user_email: user.email
}, function (err, all_routes) {
if (err) {
console.log(err);
} else {
res.send(JSON.stringify(all_routes));
}
});
} else {
console.log("No email");
res.redirect("/");
}
});
app.post('/download-path', function (req, res) {
"use strict";
var locations = req.body.selected;
locations = JSON.parse(locations);
console.log(locations);
// var coords = shortestRoute(names);
// var data1 = getShortest(coords, names);
// console.log(data1);
res.render('pdf.ejs', {
obj: locations,
tagline: "Optimal path for the destinations is as follows"
});
ejs.toHTML('views/pdf.ejs', {
obj: locations,
tagline: "Optimal path for the destinations is as follows"
}).then(function (html) {
var options = {
format: 'A4',
margin: '2cm'
},
output = 'pdf_dryrun2.pdf';
// var output = 'pdf_' + moment().format('YYYYMMDDHHmmSS') + '.pdf'
pdf.toPDF(html, options, output).then(function (response) {
console.log("PDF file successfully written");
console.log(response);
}, function (error) {
console.error(error);
});
}, function (error) {
console.error(error);
});
});
app.post('/add-bug', function (req, res) {
console.log(req.body.name);
console.log(req.body.bug);
var feedback = new Feedback({
name: req.body.name,
feedback: req.body.bug
});
feedback.save();
res.redirect('/');
});
app.listen(process.env.PORT || 8002);
console.log('Working...');