-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
540 lines (513 loc) · 20.7 KB
/
Copy pathapp.js
File metadata and controls
540 lines (513 loc) · 20.7 KB
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
var express = require("express");
var bodyParser = require("body-parser");
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
const create = require("./database/createRecord");
const fetch = require("./database/matchRecord");
const update = require("./database/updateRecord");
const unlink = require("./database/unlinkRecord");
const search = require("./algorithm/search");
const automate = require("./algorithm/automate");
const spoonacular = require("./api/spoonacular");
/**
* Endpoint for getting ingredient details
*/
app.get("/ingredient", function (req, res) {
var parameters = req.query;
//Fetch the ingredients for the user
fetch.fetchIngredients(parameters.user)
.then(function (response) {
var data = response.data.results[0].data;
if (data) {
var responseData = [];
for (var i = 0; i < data.length; i++) {
//Create new array for the output combining the responses
responseData.push({
'name': data[i]['row'][0]['name'],
'amount': data[i]['row'][1]['amount'],
'measurement': data[i]['row'][1]['measurement'],
'location': data[i]['row'][1]['location'],
});
}
return res.send(responseData);
} else {
return res.send("ERROR when fetching ingredient node " + createResponse.data.errors[0].code + " " + createResponse.data.errors[0].message);
}
})
.catch(function (error) {
if (error) {
res.send("Error when fetching ingredient " + error)
}
});
});
/**
* Endpoint for creating ingredients
*/
app.post("/ingredient", function (req, res) {
var parameters = req.body;
//Create ingredient and relationship nodes
create.createIngredientNodes(parameters)
.then(function (response) {
var userResponse = response.data.results[0].data;
var ingredientResponse = response.data.results[1].data;
if (userResponse && ingredientResponse) {
//If there are valid responses the front end will output saved
return res.send(
"SUCCESS New ingredient node created for " +
userResponse[0].row[0]['name'] +
" which is " +
ingredientResponse[0].row[0]['name']
);
} else {
return res.send("" +
"ERROR creating ingredient node " +
response.data.errors[0].code +
" " +
response.data.errors[0].message);
}
})
.catch(function (error) {
//Return error to front end which then gets displayed
if (error) {
res.send("Error when creating ingredient " + error)
}
});
});
/**
* Endpoint for deleting ingredients
*/
app.delete("/ingredient", function (req, res) {
var parameters = req.query;
//Unlink the ingredient from the user - don't delete so it is still available in the search
unlink.deleteIngredient(parameters)
//Get the response from matchParameters
.then(function (response) {
var userResponse = response.data.results[0].data[0].row[0];
if (response.data.results[0].data || response.data.results[1].data) {
return res.send("SUCCESS Ingredient deleted for " + userResponse['name']);
} else {
res.send("Error when deleting ingredient" + response.data.errors[0].code + " " + response.data.errors[0].message);
}
})
.catch(function (error) {
if (error) {
res.send("Error when deleting ingredient " + error)
}
});
});
/**
* Endpoint for updating ingredients
*/
app.patch("/ingredient", function (req, res) {
var parameters = req.body;
//Update the ingredient
update.updateIngredient(parameters)
//Get the response from matchParameters
.then(function (response) {
var userResponse = response.data.results[0].data[0].row[0];
if (response.data.results[0].data || response.data.results[1].data) {
return res.send("SUCCESS ingredient updated for " + userResponse['name']);
} else {
res.send("Error when updating ingredient " + response.data.errors[0].code + " " + response.data.errors[0].message);
}
})
.catch(function (error) {
if (error) {
res.send("Error when updating ingredient " + error)
}
});
});
/**
* Endpoint for updating ingredient amounts
*/
app.patch("/ingredient/amount", function (req, res) {
var parameters = req.body;
//Update only the amount field for the ingredient
update.updateIngredientAmounts(parameters)
//Get the response from matchParameters
.then(function (response) {
var userResponse = response.data.results[0].data[0].row[0];
if (response.data.results[0].data || response.data.results[1].data) {
return res.send("SUCCESS ingredient amounts updated for " + userResponse['name']);
} else {
res.send("Error when updating ingredient amounts" + response.data.errors[0].code + " " + response.data.errors[0].message);
}
})
.catch(function (error) {
if (error) {
res.send("Error when updating ingredient amounts " + error)
}
});
});
/**
* Endpoint for getting recipe details
*/
app.get("/recipe", function (req, res) {
//Fetch the recipes for the user
var responseData = [];
var parameters = req.query;
fetch.fetchRecipes(parameters.user)
.then(function (response) {
var data = response.data.results[0].data;
if (data) {
for (var i = 0; i < data.length; i++) {
//Create new array for the output combining the recipe responses
var recipeResponse = data[i]['row'][0];
responseData.push({
'name': recipeResponse['name'],
'tag': recipeResponse['tag'],
'servings': recipeResponse['servings'],
'prepTime': recipeResponse['prepTime'],
'cookTime': recipeResponse['cookTime'],
'method': JSON.parse(recipeResponse['method']),
'ingredients': [],
});
var ingredients = data[i]['row'][1];
for (var j = 0; j < ingredients.length; j++) {
var index = responseData.findIndex(x => x.name === data[i]['row'][0]['name'])
//Push ingredients to the output array
responseData[index]['ingredients'].push({
'name': ingredients[j][0]['name'],
'amount': ingredients[j][1]['amount'],
'measurement': ingredients[j][1]['measurement'],
"weight": ingredients[j][1]['weight'],
"calories": ingredients[j][1]['calories'],
"energy": ingredients[j][1]['energy'],
"fat": ingredients[j][1]['fat'],
"carbs": ingredients[j][1]['carbs'],
"protein": ingredients[j][1]['protein'],
})
}
}
return res.send(responseData);
} else {
return res.send("ERROR when fetching recipe node " + response.data.errors[0].code + " " + response.data.errors[0].message);
}
})
.catch(function (error) {
if (error) {
res.send("Error when fetching recipe " + error)
}
});
});
/**
* Endpoint for creating recipes
*/
app.post("/recipe", function (req, res) {
var parameters = req.body;
//Create the nodes and relationships for the recipe if they do not already exist
create.createRecipeNodes(parameters)
//Get the response from createRecipeNodes
.then(function (response) {
var userResponse = response.data.results[0].data;
var recipeResponse = response.data.results[1].data;
if (userResponse && recipeResponse) {
return res.send("SUCCESS New recipe node created for " +
userResponse[0].row[0]['name'] +
" which is " +
recipeResponse[0].row[0]['name']);
} else {
return res.send("ERROR creating recipe node " +
response.data.errors[0].code +
" " +
response.data.errors[0].message);
}
})
.catch(function (error) {
if (error) {
res.send("Error when creating recipe " + error)
}
});
});
/**
* Endpoint for deleting recipes
*/
app.delete("/recipe", function (req, res) {
var parameters = req.query;
//Unlink the recipe from the user - don't delete so it is still available in the search
unlink.deleteRecipe(parameters)
//Get the response from matchParameters
.then(function (response) {
var userResponse = response.data.results[0].data[0].row[0];
if (response.data.results[0].data || response.data.results[1].data) {
return res.send("SUCCESS Recipe deleted for " + userResponse['name']);
} else {
res.send("Error when deleting recipe" + response.data.errors[0].code + " " + response.data.errors[0].message);
}
})
.catch(function (error) {
if (error) {
res.send("Error when deleting recipe " + error)
}
});
});
/**
* Endpoint for updating recipe summary
*/
app.patch("/recipe/summary", function (req, res) {
var parameters = req.body;
//Unlink the recipe from the user - don't delete so it is still available in the search
update.updateRecipeSummary(parameters)
//Get the response from matchParameters
.then(function (response) {
var userResponse = response.data.results[0].data[0].row[0];
if (response.data.results[0].data || response.data.results[1].data) {
return res.send("SUCCESS recipe summary updated for " + userResponse['name']);
} else {
res.send("Error when updating recipe summary" + response.data.errors[0].code + " " + response.data.errors[0].message);
}
})
.catch(function (error) {
if (error) {
res.send("Error when updating recipe summary " + error)
}
});
});
/**
* Endpoint for updating recipe method
*/
app.patch("/recipe/method", function (req, res) {
var parameters = req.body;
//Unlink the recipe from the user - don't delete so it is still available in the search
update.updateRecipeMethod(parameters)
//Get the response from matchParameters
.then(function (response) {
var userResponse = response.data.results[0].data[0].row[0];
if (response.data.results[0].data || response.data.results[1].data) {
return res.send("SUCCESS recipe method updated for " + userResponse['name']);
} else {
res.send("Error when updating recipe method" + response.data.errors[0].code + " " + response.data.errors[0].message);
}
})
.catch(function (error) {
if (error) {
res.send("Error when updating recipe method " + error)
}
});
});
/**
* Endpoint for updating recipe ingredients
*/
app.patch("/recipe/ingredients", function (req, res) {
var parameters = req.body;
//Unlink the recipe from the user - don't delete so it is still available in the search
update.updateRecipeIngredients(parameters)
//Get the response from matchParameters
.then(function (response) {
var userResponse = response.data.results[0].data[0].row[0];
if (response.data.results[0].data || response.data.results[1].data) {
return res.send("SUCCESS recipe ingredients updated for " + userResponse['name']);
} else {
res.send("Error when updating recipe ingredients" + response.data.errors[0].code + " " + response.data.errors[0].message);
}
})
.catch(function (error) {
if (error) {
res.send("Error when updating recipe ingredients " + error)
}
});
});
/**
* Endpoint for getting details of next recipe in meal calendar
*/
app.get("/recipe/next", function (req, res) {
var parameters = req.query;
fetch.fetchRecipe(parameters.recipe)
.then(function (response) {
var recipeResponse = response.data.results[0].data[0]['row'][0];
var responseData = {
'name': recipeResponse['name'],
'tag': recipeResponse['tag'],
'servings': recipeResponse['servings'],
'prepTime': recipeResponse['prepTime'],
'cookTime': recipeResponse['cookTime'],
'method': JSON.parse(recipeResponse['method']),
'ingredients': [],
};
for (var i = 0; i < response.data.results[0].data.length; i++) {
var ingredientResponse = response.data.results[0].data[i]['row'];
responseData.ingredients.push(
{
'name': ingredientResponse[2]['name'],
'amount': ingredientResponse[1]['amount'],
'measurement': ingredientResponse[1]['measurement'],
'weight': ingredientResponse[1]['weight'],
'calories': ingredientResponse[1]['calories'],
'energy': ingredientResponse[1]['energy'],
'fat': ingredientResponse[1]['fat'],
'carbs': ingredientResponse[1]['carbs'],
'protein': ingredientResponse[1]['protein']
}
);
}
res.send(responseData);
})
.catch(function (error) {
if (error) {
res.send("Error when fetching recipe " + error)
}
});
});
/**
* Endpoint for getting search results for recipes
*/
app.get("/recipe/search", function (req, res) {
var parameters = req.query;
var recipes = [];
//Return results for the user query
fetch.fetchSearchRecipes(parameters.user)
.then(function (fetchResponse) {
recipes = fetchResponse.data.results;
return fetch.fetchIngredients(parameters.user);
}).then(function (ingredientResponse) {
return search.searchRecipe(ingredientResponse.data.results[0], recipes, JSON.parse(parameters.search), JSON.parse(parameters.diets), JSON.parse(parameters.allergies))
}).then(function (searchResponse) {
if (searchResponse != null) {
return res.send(searchResponse);
} else {
res.send("Error when searching recipe");
}
})
.catch(function (error) {
if (error) {
res.send("Error when searching recipe " + error)
}
});
});
/**
* Endpoint for creating a relationship between a recipe and a user
*/
app.post("/recipe/link", function (req, res) {
var parameters = req.body;
//Link recipe to the user
fetch.fetchIngredients(parameters.user)
.then(function (fetchResponse) {
return create.createRecipeUserLink(parameters, fetchResponse.data.results.data);
})
//Get the response from creating the link
.then(function (response) {
var linkResponse = response.data.results[0].data;
if (linkResponse) {
return res.send("SUCCESS New link created for " + linkResponse[0].row[0]['name'] + " to " + linkResponse[1].row[0]['name']);
} else {
return res.send("ERROR creating link " + response.data.errors[0].code + " " + response.data.errors[0].message);
}
})
.catch(function (error) {
if (error) {
res.send("Error when creating link " + error)
}
});
});
/**
* Endpoint for getting a shopping list details
*/
app.get("/list", function (req, res) {
var parameters = req.query;
//Fetch the shopping list for the user
fetch.fetchShoppingList(parameters)
.then(function (response) {
var data = response.data.results[0].data;
if (data) {
var responseData = [];
for (var i = 0; i < data.length; i++) {
//Take the amount you have away from the needed amount
var amount = data[i]['row'][1] - data[i]['row'][2];
if (amount > 0) {
responseData.push({
'name': data[i]['row'][0]['name'],
'amount': amount,
'measurement': data[i]['row'][3],
'price': (data[i]['row'][4] - data[i]['row'][5]).toFixed(2)
});
}
}
return res.send(responseData);
} else {
return res.send("ERROR when fetching list node " + createResponse.data.errors[0].code + " " + createResponse.data.errors[0].message);
}
})
.catch(function (error) {
if (error) {
res.send("Error when fetching shopping list " + error)
}
});
});
/**
* Endpoint for updating a shopping list
*/
app.patch("/list", function (req, res) {
var parameters = req.body;
update.updateShoppingList(parameters)
//Update the shopping list with bought amounts
.then(function (response) {
if (response.data.results[0].data || response.data.results[1].data) {
return res.send("SUCCESS Shopping list updated for " + response.data.results[0].data[0].row[0]['name']);
} else {
res.send("Error when updating shopping list" + response.data.errors[0].code + " " + response.data.errors[0].message);
}
})
.catch(function (error) {
if (error) {
res.send("Error when updating shopping list " + error)
}
});
});
/**
* Endpoint for getting automated calendar results
*/
app.get("/automate", function (req, res) {
var parameters = req.query;
console.log(parameters);
//Return results for automating the calendar
fetch.fetchRecipes(parameters.user)
.then(function (response) {
return automate.automateCalendar(parameters, response.data.results[0].data);
})
.then(function (automateResponse) {
res.send(automateResponse);
})
.catch(function (error) {
if (error) {
res.send("Error when searching recipe " + error)
}
});
});
/**
* Endpoint for pulling recipes into the application
*/
app.get("/pull", function (req, res) {
var parameters = req.query;
var formattedRecipes = [];
//Check if the user and recipe already exists in the app
spoonacular.pullRecipes(parameters.number)
//Get the response from matchParameters
.then(function (response) {
return spoonacular.formatRecipes(parameters.number, response.data.recipes);
})
.then(function (formatResponse) {
if (formatResponse !== []) {
formattedRecipes = formatResponse;
return create.createRecipeNodesBulk(formattedRecipes)
//Get the response from createNodesandRelationships
.then(function (response) {
if (response.data.results[0].data) {
return res.send("SUCCESS New recipe node created for admin");
} else {
return res.send("ERROR creating recipe node " + response.data.errors[0].code + " " + createResponse.data.errors[0].message);
}
})
} else {
res.send("Error fetching recipes");
}
})
.catch(function (error) {
if (error) {
res.send("Error when fetching recipes " + error)
}
});
});
var server = app.listen(3000, function () {
console.log("Listening on port %s...", server.address().port);
});