-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
170 lines (138 loc) · 3.49 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
170
// general requires
const API = require("./API-auth");
const request = require("request");
const querystring = require("querystring");
// express setup
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
const port = process.env.PORT || 3000;
// stores current selection
let currentSelection = {};
app.set("view engine", "ejs");
app.use(express.static(__dirname + "/public"));
app.use(bodyParser.urlencoded({extended: true}));
// home route
app.get("/", (req, res) => {
res.render("index");
});
// search route
app.get("/search", (req, res) => {
res.render("search", {"error": false});
});
// show route
app.get("/show", (req, res) => {
res.render("show");
});
// about route
app.get("/about", (req, res) => {
res.render("about");
});
// view route
app.get("/view/:id", (req, res) => {
let recipe = {};
currentSelection.forEach(hit => {
// find recipe from list
if (hit.recipe.uri.split("#")[1] === req.params.id)
{
recipe = hit.recipe;
return true;
}
});
res.render("view", {recipe: recipe});
});
// show POST route
app.post("/show", (req, res) => {
let data = parseBody(req.body.fields);
data.q = data.q.split(",");
let url = `https://api.edamam.com/search?${querystring.stringify(data)}&from=0&to=12`;
request.get(url, (error, response, body) => {
if (error || response.statusCode !== 200)
{
return res.render("search", {"error": true});
}
else
{
let data = JSON.parse(response.body);
// store response for future use
currentSelection = data.hits;
res.render("show", {"count": data.count,"recipes": data.hits});
}
});
});
app.listen(port, () => console.log("Server started"));
function parseQueryText(queryText)
{
let query = "";
let ingredients = queryText.split(",");
ingredients.forEach(ingredient => {
query += "q=" + ingredient + "&";
});
return query;
}
function parseHealth(healthLabels)
{
let health = '';
healthLabels.forEach(label => {
health += "health=" + label + "&";
});
return health;
}
function parseBody(body)
{
// parse body
let queryText = body.ingredientInput;
let obj = {};
obj.q = queryText;
obj.app_id = API.appID;
obj.app_key = API.appKey;
if (body.diet !== "")
{
obj.diet = body.diet;
}
if (body.health !== undefined && body.health !== "")
{
obj.health = body.health;
}
let calories = "";
if (body.minCal === "" && body.maxCal === "")
{
calories = "";
}
else if (body.minCal !== "" && body.maxCal === "")
{
calories = body.minCal + "+";
obj.calories = calories;
}
else if (body.minCal === "" && body.maxCal !== "")
{
calories = body.maxCal;
obj.calories = calories;
}
else
{
calories = body.minCal + "-" + body.maxCal;
obj.calories = calories;
}
let time = "";
if (body.minTime === "" && body.maxTime === "")
{
time = "";
}
else if (body.minTime !== "" && body.maxTime === "")
{
time += body.minTime + "+";
obj.time = time;
}
else if (body.minTime === "" && body.maxTime !== "")
{
time += body.maxTime;
obj.time = time;
}
else
{
time = body.minTime + "-" + body.maxTime;
obj.time = time;
}
return obj;
}