-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
303 lines (261 loc) · 9.11 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
"use strict";
/**
* Main Server File. Sets up API endpoints
**/
//Imports
const _ = require('underscore');
const UserUtils = require("./user_utils.js");
const Friends = require("./friends.js");
const Auth = require("./auth.js");
const User = require("./db.js");
const Yelp = require("./yelp.js");
const Restfiy = require('restify');
//Create the Restify Server
const server = Restfiy.createServer({
name: 'FoodMob-Node',
version: '1.0.0'
});
server.use(Restfiy.acceptParser(server.acceptable));
server.use(Restfiy.queryParser());
server.use(Restfiy.bodyParser());
//Output Exceptions thrown in Restify
server.on('uncaughtException', function (req, res, route, err) {
console.error('uncaught error', err);
console.error(err.stack);
return res.send(500, 'Unknown Error\n' + err.stack);
});
/**
* Setup API EndPoints
*
* Full API Definition at https://docs.google.com/document/d/1c_W9Swb8wVKoVLiBv0ZFhFMh5jhU5u0iR2UylNOX-5M/edit?usp=sharing
**/
//Login
server.post('/login', function (req, res, next) {
console.log(req.params);
const params = req.params;
const email = params.email;
const password = params.password;
Auth.loginUser(email, password)
.spread(function (token, user) {
console.log(email + " login successful");
res.send({"email": email, success: true, auth_token: token, profile: user.profile});
next();
}).catch(function (err) {
console.log(err);
console.log(email + " Login Failed");
res.send({"email": email, success: false});
next();
});
});
//Register
server.post('/users', function (req, res, next) {
const params = req.params;
const email = params.email;
const first_name = params.first_name;
const last_name = params.last_name;
const password = params.password;
Auth.registerUser(email, password, first_name, last_name)
.then(function (newUser) {
console.log('User created!');
console.log(email + " register successfully");
return Auth.loginUser(email, password);
}).spread(function (authToken, user) {
console.log("Registration successful");
res.send({"email": email, "token": authToken, success: true});
return next();
}).catch(function (err) {
console.log("Registration Failed");
console.log(err);
console.log(err.stack);
res.send({"email": email, success: false});
return next();
});
});
//Logout
server.post('/logout', function (req, res, next) {
const params = req.params;
console.log(req.params);
const email = params.email;
const authToken = params.auth_token;
console.log("logout Request");
Auth.logoutUser(email, authToken)
.then(function (user) {
console.log("Logout Successful");
res.send({"email": email, success: true});
return next();
}).catch(function (err) {
console.log("Failed!");
console.log(err);
res.send({"email": email, success: false});
return next();
});
});
//Get Friends
server.get('/users/:userEmail/friends', function (req, res, next) {
//console.log(req.params);
const params = req.params;
const userEmail = params.userEmail;
const authToken = params.auth_token;
Friends.getFriends(userEmail, authToken)
.then(function (friends) {
//let friends = user.friends;
console.log("friends " + friends);
res.send({"friends": friends, success: true});
next();
}).catch(function (error) {
console.log("Failed!", error);
res.send({success: false});
next();
});
});
//Add Friends
server.put('/users/:userEmail/friends', function (req, res, next) {
//console.log(req.params);
const params = req.params;
const userEmail = params.userEmail;
const friendEmail = params.friend_email;
const authToken = params.auth_token;
Friends.addFriend(userEmail, authToken, friendEmail)
.then(function (user) {
console.log("Friend Added Successfully " + user);
res.send({"email": friendEmail, success: true});
next();
}).catch(function (error) {
if (error == "Friend already added") {
console.log("Duplicated Friend tried to be added " + friendEmail);
res.send({"email": friendEmail, success: false, error: "duplicate"});
next();
} else {
console.log("Failed!", error);
res.send({"email": friendEmail, success: false});
next();
}
});
});
//Get Food Profile
server.get('/users/:email/food_profile', function (req, res, next) {
console.log(req.params);
const params = req.params;
const email = params.email;
const authToken = params.auth_token;
UserUtils.getUserFoodProfile(email, authToken)
.then(function (foodProfile) {
console.log("Food Profile Found " + foodProfile);
res.send({"email": email, success: true, food_profile: foodProfile});
next();
}).catch(function (error) {
console.log("Failed!", error);
console.log(error.stack);
res.send({"email": email, success: false});
next();
});
});
//Update Food Profile
server.put('/users/:email/food_profile', function (req, res, next) {
console.log(req.params);
const params = req.params;
const email = params.email;
const authToken = params.auth_token;
const foodProfile = params.food_profile;
UserUtils.updateUserFoodProfile(email, authToken, foodProfile)
.then(function (user) {
console.log("User Update successful " + foodProfile);
res.send({"email": email, success: true});
next();
}).catch(function (error) {
console.log("Failed!", error);
res.send({"email": email, success: false});
next();
});
});
//Search for Restaurants
server.post('/search', function (req, res, next) {
console.log("Search");
console.log(req.params);
const params = req.params;
const email = params.email;
const authToken = params.auth_token;
const users = params.friends;
const location = params.location;
const minRating = params.min_rating;
let options;
if (params.options) {
options = params.options;
} else {
options = {};
}
let ll = params.ll;
if (ll) {
ll = ll.join()
}
let cll = params.c11;
if (cll) {
cll = cll.join();
}
const goodCategories = [];
goodCategories.push(params.good_categories);
const badCategories = [];
badCategories.push(params.bad_categories);
const allergyCategories = [];
allergyCategories.push(params.allergy_categories);
UserUtils.getUserFoodProfile(email, authToken)
.then(function (foodProfile) {
goodCategories.push(foodProfile.likes);
badCategories.push(foodProfile.dislikes);
allergyCategories.push(foodProfile.allergies);
let ourOptions = {};
ourOptions.limit = 20;
if (minRating) {
ourOptions.sort = 2;//Best Rating
}
_.extend(ourOptions, options);
return Yelp.searchYelp("restaurants", location, cll, ll, ourOptions)
}).then(function (data) {
let businesses = data.businesses;
let location = data.region;
businesses = businesses.filter(function (business) {
console.log(minRating, business.rating);
if (minRating) {
if (!business.rating || minRating > business.rating) {
return false;
}
}
return true;
});
businesses.forEach(function (business) {
let categories = business.categories.map(p => p[1])
let score = 0
const goodCatCount = goodCategories.map(array => _.intersection(array, categories).length);
const badCatCount = badCategories.map(array => _.intersection(array, categories).length);
const allergyCatCount = allergyCategories.map(array => _.intersection(array, categories).length);
let goodCount = goodCatCount.reduce(function (a, b) {
return a + b
});
let badCount = badCatCount.reduce(function (a, b) {
return a + b
});
let allergyCount = allergyCatCount.reduce(function (a, b) {
return a + b
});
score += goodCount * 5;
score -= badCount * 5;
score -= allergyCount * 25;
business.score = score
console.log(business.name)
console.log(categories)
console.log("score")
console.log(business.score)
//console.log("name: " +businesses.name)
});
businesses.sort(function (a, b) {
return b.score - a.score
});
res.send({"businesses": businesses, success: true});
next();
}).catch(function (err) {
console.log("Failed!", err);
res.send({"email": email, success: false});
next();
});
});
module.exports = server;