-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
303 lines (253 loc) · 9.04 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
/* ----------NPM PACKAGE DEPENDENCIES ---------*/
//require express package//
var http = require("http");
var express = require('express');
var db = require('./models');
var bodyParser = require("body-parser");
var exphbs = require("express-handlebars");
var session = require('express-session');
var path = require("path");
/* ----------INSTANTIATE MODULES ---------*/
//run express//
var app = express();
var PORT = process.env.PORT || 3000;
app.use(express.static(__dirname + '/public'));
//use the body parser//
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
//use handlebars//
//app.engine("handlebars", exphbs({ defaultLayout: "main", extname: '.handlebars' }));
//app.set('views', path.join(__dirname, 'views'));
app.set('views', path.join(__dirname, 'views'));
app.engine('handlebars', exphbs({
defaultLayout: 'main',
layoutsDir: path.join(__dirname, 'views', 'layouts')}
));
app.set("view engine", "handlebars");
//use session//
app.use(session({
secret: 'secret',
resave: true,
saveUninitialized: true
}));
/* ----------THESE SHOULD EVENTUALLY MIGRATE TO ROUTES FILES ---------*/
//catch all route//
app.get('/', (req, res) => {
if (req.session.loggedin) {
res.redirect('/index');
} else {
res.render('signIn', { title: "Welcome to Julia's Child!" });
}
});
//route to 404//
app.get('/404', (req, res) => {
res.render('404', { title: "ERROR 404" });
});
//route tocreate new user with signUp//
app.post('/signUp', function(req, res) {
// get user credentials from form
var userEmail = req.body.userEmail;
var userPassword = req.body.userPass;
//if both email and password are present, add an account to the database
if (userEmail && userPassword) {
db.Accounts
.findOrCreate({ where: { email: userEmail }, defaults: { password: userPassword } })
.then(([user, created]) => {
console.log(user.get({
plain: true
}))
if (created) {
req.session.loggedin = true;
req.session.username = userEmail;
res.redirect('/index');
} else {
res.render('signIn', { title: "Welcome to Julia's Child!", userExists:"true" });
}
})
} else {
res.send('Please enter Username and Password!');
res.end();
}
});
//change form to login//
app.get('/auth', (req, res) => {
res.render('signIn', { title: "Welcome to Julia's Child!", login:"true" });
});
//rout to log in user//
app.post('/logIn', function(req, res) {
// get user credentials from form
var userEmail = req.body.userEmail;
var userPassword = req.body.userPass;
//if both email and password are present, add an account to the database
if (userEmail && userPassword) {
db.Accounts
.findOne({where: {email: userEmail, password: userPassword}})
.then(user => {
if(user){
req.session.loggedin = true;
req.session.username = userEmail;
res.redirect('/index');
} else {
res.render('signIn', { title: "Welcome to Julia's Child!", login:"true", loginError:"true" });
}
})
} else {
res.send('Please enter Username and Password!');
res.end();
}
});
//route to index//
app.get('/index', (req, res) => {
if(!req.session.loggedin){
res.redirect('/');
} else {
var userEmail = req.session.username;
db.Accounts.findOne({ where: {email: userEmail} }).then(user => {
db.RecipeBox.findAll({where: {userID: user.userID}})
.then((recipeMatches, created)=>{
var matches=[];
recipeMatches.forEach(e=>{
matches.push(e.recipeID);
})
db.Recipe.findAll({where: {recipeID: matches}}).then(recipes=>{
recipes.map(recipe=>{
recipe.ingredientArray=recipe.ingredients.split(",");
recipe.ingredientArray.pop();
console.log(recipe.ingredients);
})
res.render('./index', {
title: "Your Recipe Box",
data: recipes
});
})
})
});
}
});
//route to addrecipe//
app.get('/addrecipe', (req, res) => {
if(!req.session.loggedin){
res.redirect('/');
} else {
res.render('addrecipe', { title: "Add A Recipe" });
}
});
//should submit-recipe to database//
app.post('/submit-recipe', (req, res) => {
console.log('storing a recipe...');
if(!req.session.loggedin){
res.redirect('/');
} else {
var recipeImage=("/images/fooddefault.jpeg");
if(req.body.recipeImage){
recipeImage = req.body.recipeImage;
}
var recipeTitle = req.body.recipeTitle;
var recipeDesc = req.body.recipeDesc;
var instructions = req.body.instructions;
var ingredientLines = req.body.recipeIngredients;
if (recipeTitle && recipeDesc && instructions) {
db.Recipe
.findOrCreate({
where:
{
recipeImage: recipeImage,
recipeTitle: recipeTitle,
recipeDesc: recipeDesc,
instructions: instructions,
ingredients: ingredientLines
}
})
.then(([recipe, created]) => {
if (!created) {
res.send('Something went wrong');
res.end();
}
var userEmail = req.session.username;
db.Accounts.findOne({ where: {email: userEmail} }).then(user => {
db.RecipeBox.create({ userID: user.userID, recipeID: recipe.recipeID })
.then(created=>{
res.redirect('/index', 302);
})
});
});
};
}
})
//route to search API//
require("./routes/apiRoutes")(app);
app.get('/addFavorite/', function(req, res) {
if(!req.session.loggedin){
res.redirect('/');
} else {
console.log("saving a recipe...");
db.Recipe
.findOrCreate({
where:
{
recipeImage: req.query.img,
recipeTitle: req.query.title,
recipeDesc: req.query.desc,
ingredients: req.query.ing
}
})
.then(([recipe, created]) => {
var userEmail = req.session.username;
db.Accounts.findOne({ where: {email: userEmail} }).then(user => {
db.RecipeBox.create({ userID: user.userID, recipeID: recipe.recipeID })
.then((created)=>{
console.log("ITWORKED!");
res.redirect(302, '/index');
})
});
});
}
})
app.get('/removeFavorite/:recipeIdx', function(req, res) {
if(!req.session.loggedin){
res.redirect('/');
} else {
var userEmail = req.session.username;
console.log(req.params.recipeIdx);
var recipeIdx = req.params.recipeIdx;
if (!recipeIdx){
console.log("aint no index here!", recipeIdx);
} else {
console.log("your recipe index is ", recipeIdx);
}
db.Accounts.findOne({ where: {email: userEmail} }).then(user => {
db.RecipeBox.destroy({ where: {userID: user.userID, recipeID: recipeIdx }})
.then((destroyed)=>{
console.log("entry destroyed!");
})
});
}
})
//route to log out//
app.get('/logOut', (req, res) => {
req.session.loggedin = false;
res.redirect('/');
});
//sync database with sequelize
db.sequelize.sync().then(function () {
//------------Uncomment for database starter recipe------//
// db.Recipe.create(thingToSave).then(function (stuffFromSQL) {
// console.log(stuffFromSQL);
// });
// var thingToSave = {
// title: "Salmon test 2",
// recipeImage: "https://www.inspiredtaste.net/wp-content/uploads/2018/09/Easy-Oven-Baked-Salmon-Recipe-2-1200.jpg",
// recipeTitle: "Salmon 3",
// recipeDesc: "$$$",
// calories: "200",
// nutrition: "Vegan, gluten free, yada",
// ingredientLines: '"1 chicken, about 3.5 to 4 pounds", "1 lemon", "1 blood orange", "1 tangerine or clementine", "Kosher salt", "1/2 cup chicken broth"',
// instructions: '"step1", "step2", "step3", "step4"'
// }
// db.Recipe.create(thingToSave).then(function (stuffFromSQL) {
// console.log(stuffFromSQL);
// });
app.listen(PORT, function () {
console.log("App listening on: http://localhost: " + PORT);
});
});