-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
387 lines (325 loc) · 10.3 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
//initialize the express object
var express = require('express');
var app = express();
//hashcode design for userid
String.prototype.hashCode = function(){
var hash = 0, len = this.length;
if ( len === 0 ) return hash;
for( var i = 0; i < len; ++i ) {
char = this.charCodeAt(i);
hash = ((hash<<5)-hash)+char;
hash = hash & hash; // Convert to 32bit integer
}
return hash;
}
//use body parser
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static('web'));
//delete from database
app.delete('/region/*', function (req, res) {
var postBody = req.body;
var email = req.params[0];
var userid = email.hashCode();
//database part
var fs = require('fs');
var sql = require('sql.js');
var filebuffer = fs.readFileSync('foodminer.db');
var db = new sql.Database(filebuffer);
//select user previous info
var stmt = db.prepare("SELECT * FROM Preference WHERE ID=:userid");
var result = stmt.getAsObject({':userid' : userid});
var arrayR = result['Region'].split(",");
var foodregion = "";
for(var i = 0; i < arrayR.length -1 ; i++){
console.log(arrayR[i]);
if(postBody.foodregion != arrayR[i])
foodregion = foodregion + arrayR[i] + ",";
}
if(foodregion == ",")
foodregion = "None";
else if(foodregion =="")
foodregion ="None";
console.log(foodregion);
db.run('UPDATE Preference SET Region='
+ '\'' + String(foodregion) +'\''
+ 'WHERE ID = ' + '\'' + String(userid) +'\'');
var data = db.export();
var buffer = new Buffer(data);
fs.writeFileSync('foodminer.db', buffer);
db.close();
res.send(foodregion);
});
//update pereference
app.post('/region/*', function (req, res) {
var postBody = req.body;
var email = req.params[0];
var userid = email.hashCode();
//database part
var fs = require('fs');
var sql = require('sql.js');
var filebuffer = fs.readFileSync('foodminer.db');
var db = new sql.Database(filebuffer);
//select user previous info
var stmt = db.prepare("SELECT * FROM Preference WHERE ID=:userid");
var result = stmt.getAsObject({':userid' : userid});
if(result['Region'] == "None")
var foodregion = postBody.foodregion;
else
var foodregion = result['Region'] + postBody.foodregion;
//update
db.run('UPDATE Preference SET Region='
+ '\'' + String(foodregion) +',\''
+ 'WHERE ID = ' + '\'' + String(userid) +'\'');
var data = db.export();
var buffer = new Buffer(data);
fs.writeFileSync('foodminer.db', buffer);
db.close();
res.send(foodregion);
});
//update preference for types of food
//update pereference
app.post('/type/*', function (req, res) {
var postBody = req.body;
var email = req.params[0];
var userid = email.hashCode();
//database part
var fs = require('fs');
var sql = require('sql.js');
var filebuffer = fs.readFileSync('foodminer.db');
var db = new sql.Database(filebuffer);
//select user previous info
var stmt = db.prepare("SELECT * FROM Preference WHERE ID=:userid");
var result = stmt.getAsObject({':userid' : userid});
if(result['Type'] == "None")
var foodtype = postBody.foodtype;
else
var foodtype = result['Type'] + postBody.foodtype;
//update
db.run('UPDATE Preference SET Type='
+ '\'' + String(foodtype) +',\''
+ 'WHERE ID = ' + '\'' + String(userid) +'\'');
var data = db.export();
var buffer = new Buffer(data);
fs.writeFileSync('foodminer.db', buffer);
db.close();
res.send(foodtype);
});
//delete from database
app.delete('/type/*', function (req, res) {
var postBody = req.body;
var email = req.params[0];
var userid = email.hashCode();
//database part
var fs = require('fs');
var sql = require('sql.js');
var filebuffer = fs.readFileSync('foodminer.db');
var db = new sql.Database(filebuffer);
//select user previous info
var stmt = db.prepare("SELECT * FROM Preference WHERE ID=:userid");
var result = stmt.getAsObject({':userid' : userid});
var arrayR = result['Type'].split(",");
var foodtype = "";
for(var i = 0; i < arrayR.length -1 ; i++){
console.log(arrayR[i]);
if(postBody.foodtype != arrayR[i])
foodtype = foodtype + arrayR[i] + ",";
}
if(foodtype == ",")
foodtype = "None";
else if(foodtype =="")
foodtype ="None";
console.log(foodtype);
db.run('UPDATE Preference SET Type='
+ '\'' + String(foodtype) +'\''
+ 'WHERE ID = ' + '\'' + String(userid) +'\'');
var data = db.export();
var buffer = new Buffer(data);
fs.writeFileSync('foodminer.db', buffer);
db.close();
res.send(foodtype);
});
//get comments
app.get('/fcomment/*', function (req, res) {
var postBody = req.body;
var rest = req.params[0];
var fs = require('fs');
var sql = require('sql.js');
var filebuffer = fs.readFileSync('foodminer.db');
var db = new sql.Database(filebuffer);
var result = db.exec("SELECT * FROM Comments WHERE Name=" + "\'"+rest + "\';");
db.close();
console.log(result);
res.send(result);
return;
});
//get user info
app.post('/rec/*', function (req, res){
var email = req.params[0];
var userid = email.hashCode();
var postBody = req.body;
var resultlist = [];
console.log(postBody);
var regions = postBody.region.split(',');
var types = postBody.type.split(',');
var index = 0;
//database part
var fs = require('fs');
var sql = require('sql.js');
var filebuffer = fs.readFileSync('foodminer.db');
var db = new sql.Database(filebuffer);
var results = db.exec("SELECT * FROM Restaurant;");
console.log(results);
results = results[0]['values'];
for(var i = 0; i < results.length; i++){
var tags = results[i][0];
for(var j = 0; j < regions.length;j++){
if(regions[j] != ""&&tags.indexOf(regions[j]) > -1){
console.log(tags.indexOf(regions[j]));
resultlist[index] = results[i];
index++;
tags = "-1";
}
}
console.log(types);
for(var k = 0; k < types.length; k++){
if(types[k] != "" && tags.indexOf(types[k]) > -1){
console.log(tags.indexOf(types[k]));
resultlist[index] = results[i];
index++;
tags = "-1";
}
}
}
res.send(resultlist);
});
//write user comments
app.post('/comment/*', function (req, res) {
var postBody = req.body;
var rest = req.params[0];
//database part
var fs = require('fs');
var sql = require('sql.js');
var filebuffer = fs.readFileSync('foodminer.db');
var db = new sql.Database(filebuffer);
console.log('INSERT INTO Comments VALUES (\'' +
String(rest) + '\',\'' + String(postBody.userid) + '\',\'' +
String(postBody.email) + '\',\'' + String(postBody.comment) + '\')');
db.run('INSERT INTO Comments VALUES ("' +
String(rest) + '","' + String(postBody.userid) + '","' +
String(postBody.email) + '","' + String(postBody.comment) + '")');
var data = db.export();
var buffer = new Buffer(data);
fs.writeFileSync('foodminer.db', buffer);
res.send('OK');
});
app.post('/users/', function (req, res) {
var postBody = req.body;
console.log(postBody);
var nickname = postBody.nickname;
var password = postBody.password;
var age = postBody.age;
var gender = postBody.gender;
var email = postBody.email;
var userid = email.hashCode();
//database part
var fs = require('fs');
var sql = require('sql.js');
var filebuffer = fs.readFileSync('foodminer.db');
var db = new sql.Database(filebuffer);
var stmt = db.prepare("SELECT * FROM Users WHERE email=:email");
var result = stmt.getAsObject({':email' : email});
if (JSON.stringify(result) != '{}'){
res.send("This email has already been registered!");
}
else{
db.run('INSERT INTO Users VALUES (\'' +
String(userid) + '\',\'' + String(email) + '\',\'' +
String(nickname) + '\',\'' + String(password) + '\',\'' +
String(age) + '\',\'' + String(gender) + '\')');
db.run('INSERT INTO Preference VALUES (\'' +
String(userid) + '\',\'' + "None" + '\',\'' + "None" +'\')');
//var stmt = db.prepare("SELECT * FROM Users WHERE Email=:email-adress");
//var result = stmt.getAsObject({':email-adress' : email});
//console.log(result);
var data = db.export();
var buffer = new Buffer(data);
fs.writeFileSync('foodminer.db', buffer);
res.send('OK');
}
db.close();
return;
});
app.get('/login/*',function (req,res){
var email = req.params[0];
var userid = email.hashCode();
//database part
var fs = require('fs');
var sql = require('sql.js');
var filebuffer = fs.readFileSync('foodminer.db');
var db = new sql.Database(filebuffer);
var stmt = db.prepare("SELECT * FROM Users WHERE email=:email");
var result = stmt.getAsObject({':email' : email});
var stmt2 = db.prepare("SELECT * FROM Preference WHERE ID=:userid");
var result2 = stmt2.getAsObject({':userid' : userid});
console.log(result);
console.log(result2); //Will print the user colomn
result['Region'] = result2.Region;
result['Type'] = result2.Type;
db.close();
res.send(result);
return;
});
app.put('/users/*', function (req, res){
var postBody = req.body;
console.log(postBody);
var userid = postBody.userid;
var nickname = postBody.nickname;
var age = postBody.age;
var gender = postBody.gender;
//try to look up in database
var fs = require('fs');
var sql = require('sql.js');
var filebuffer = fs.readFileSync('foodminer.db');
var db = new sql.Database(filebuffer);
var stmt = db.prepare("SELECT * FROM Users WHERE ID=:userid");
var result = stmt.getAsObject({':userid' : userid});
if (JSON.stringify(result) == '{}'){
res.send("There is no such account!");
} else {
db.run("UPDATE Users SET Nickname=\'" + String(nickname) + "\', Age=\'" + String(age) + "\', Gender=\'" + String(gender) + "\' WHERE ID=\'" + String(userid) + "\'");
res.send('OK');
}
// console.log(result2);
var data = db.export();
var buffer = new Buffer(data);
fs.writeFileSync('foodminer.db', buffer);
db.close();
return;
});
//send home page
app.get('/', function (req, res) {
res.sendFile(__dirname + '/web/home.html');
});
//send services page
app.get('/services' , function (req,res) {
res.sendFile(__dirname + '/web/services.html');
});
//send about page
app.get('/about' , function (req,res) {
res.sendFile(__dirname + '/web/about.html');
});
//send discovery page
app.get('/discover', function (req, res) {
res.sendFile(__dirname + '/web/discover.html');
});
//send contact page
app.get('/contact', function (req, res) {
res.sendFile(__dirname + '/web/contact.html');
});
// start the server on http://localhost:3000/
var server = app.listen(3000, function () {
var port = server.address().port;
console.log('Server started at http://localhost:%s/', port);
});