-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
425 lines (397 loc) · 12.7 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
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
var express = require("express"),
app = express(),
bodyParser = require("body-parser"),
methodOverride = require("method-override"),
validate = require('express-validator')
cookieParser = require("cookie-parser"),
session = require("express-session"),
passport = require("passport"),
LocalStrategy = require("passport-local").Strategy,
axios = require('axios');
// importing models
var db = require("./models"),
User = db.User,
Coin = db.Coin;
// configure bodyParser to read form data
app.use(bodyParser.urlencoded({ extended: true, }));
// serve static files from public folder
app.use(express.static(__dirname + "/public"));
// set view engine to ejs
app.set("view engine", "ejs");
app.use(methodOverride("_method"));
// ensure user remains logged in throughout session
app.use(cookieParser());
app.use(session({
secret: "thisisasecret",
resave: false,
saveUninitialized: false,
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(validate());
// Login system wont work without these
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
// load home page with axios call for data
app.get('/', function(req, res) {
axios.get('https://api.coinmarketcap.com/v1/ticker/?start=0&limit=350')
.then(function(response) {
console.log('Home Page Refreshed');
res.render('index', {user: req.user, coins: response.data})
})
.catch(function(err) {
console.log(`Error: ${err}`);
})
})
// add to favorites
app.post('/addToFavorites', function(req, res) {
var userId = req.user._id
// find user to add the coin to their favorites
User.findById(userId, function(err, foundUser) {
if (err) {
console.log(`Error: ${err}`);
} else {
console.log(`req.body: ${req.body}`);
// the submitted form has a hidden input field with the coin's symbol
Coin.findOne({symbol: req.body.symbol}, function(err, succ) {
if (err) {
console.log(`Error: ${err}`);
} else {
console.log(`Found Coin: ${succ}`);
// if the coin we're trying to find doesn't exist, create a new one with the coin's symbol from the form
if (succ === null) {
var newCoin = new Coin({
symbol: req.body.symbol
});
newCoin.save();
console.log(`newCoin._id: ${newCoin._id}`);
// push newCoin's id into user's favorites array
foundUser.favorites.push(newCoin._id);
foundUser.save()
res.redirect('/')
} else {
foundUser.favorites.push(succ._id);
foundUser.save()
res.redirect('/')
}
}
})
}
})
});
// load favorites view
app.get("/favorites/:id", function (req, res) {
var userId = req.params.id;
User.findById(userId, function(err, user) {
if (err) {
console.log(`Error: ${err}`);
} else {
// find the user and populate the 'symbol' attribute from the 'favorites' id
// we pass the success from populate function to our front end
// this allows us to retrieve the data from just the stored id reference
User.findById(userId)
.populate('favorites', 'symbol')
.exec(function(err, returnedFavs) {
console.log(`.exec fn returned: ${returnedFavs}`);
axios.get('https://api.coinmarketcap.com/v1/ticker/?limit=0')
.then(function(response) {
// passing User, API data & Favorites symbols to the ejs template
res.render('favorites', {user: user, coinIds: response.data, favs: returnedFavs.favorites})
})
})
}
})
});
// add coin to portfolio (mirrors add coin to favorites)
app.post('/addToPortfolio', function(req, res) {
var userId = req.user._id
User.findById(userId, function(err, foundUser) {
if (err) {
console.log(`Error: ${err}`);
} else {
// find coin by symbol from the hidden input field on the form body on ejs template view
Coin.findOne({symbol: req.body.symbol}, function(err, succ) {
if (err) {
console.log(`Error: ${err}`);
} else {
console.log(`Found Coin: ${succ}`);
// if the coin doesn't exist in my db, create it
if (succ === null) {
console.log(req.body.qty);
var newCoin = new Coin({
symbol: req.body.symbol,
qty: req.body.qty
});
newCoin.save();
console.log(`newCoin._id: ${newCoin._id}`);
// push the coin's _id value into the users portfolio array
foundUser.portfolio.push(newCoin._id);
foundUser.save()
res.redirect('/')
} else {
foundUser.portfolio.push(succ._id);
foundUser.save()
res.redirect('/')
}
}
})
}
})
})
// Update coin's quantity in user's portfolio
app.put('/portfolio/:id', function(req, res) {
var userId = req.params.id;
User.findById(userId, function(err, foundUser) {
if (err) {
console.log(`Error: ${err}`);
} else {
Coin.findOne({symbol: req.body.symbol}, function(err, succ) {
if (err) {
console.log(`Error: ${err}`);
} else {
// updating the found coin's quantity to the quantity submitted on the form
succ.qty = req.body.qty;
succ.save(function(err, updateSaved) {
if (err) {
console.log(`Error: ${err}`);
} else {
User.findById(userId)
.populate('portfolio', 'symbol qty')
.exec(function(err, returnedPort) {
axios.get('https://api.coinmarketcap.com/v1/ticker/?limit=0')
.then(function(response) {
console.log(returnedPort.portfolio);
console.log('Portfolio Saved: ' + updateSaved);
res.render('portfolio', {user: foundUser, coinIds: response.data, portfolio: returnedPort.portfolio})
})
})
}
})
}})
}})
})
// Remove a coin from portfolio
app.put('/deleteFromPortfolio', function(req, res) {
var userId = req.user._id
User.findById(userId, function(err, foundUser) {
if (err) {
console.log("Error: " + err);
} else {
Coin.findById(req.body._id, function(err, succ) {
if (err) {
console.log(`Error: ${err}`);
} else {
console.log(`Coin id: ${succ._id}`);
var idToRemove = succ._id
// here we have the user & ID of the coin we want to remove
// use .find() to iterate through our user's portfolio array
// if there's a match, we use .splice() to remove that ID
foundUser.portfolio.find(function(value, index) {
if (value.toString() === idToRemove.toString()) {
return foundUser.portfolio.splice(index, 1);
}
});
// need to save user or else deletion will not work
foundUser.save(function(err, success) {
if (err) {
console.log(`Error: ${err}`);
} else {
User.findById(userId)
.populate('portfolio', 'symbol qty')
.exec(function(err, returnedPort) {
axios.get('https://api.coinmarketcap.com/v1/ticker/?limit=0')
.then(function(response) {
res.render('portfolio', {user: foundUser, coinIds: response.data, portfolio: returnedPort.portfolio})
})
})
}
})
}
})
}
})
})
// Delete from favorites (same as delete from portfolio)
app.put('/deleteFromFavorites', function(req, res) {
var userId = req.user._id
User.findById(userId, function(err, foundUser) {
if (err) {
console.log(`Error: ${err}`);
} else {
Coin.findById(req.body._id, function(err, succ) {
if (err) {
console.log(`Error: ${err}`);
} else {
var idToRemove = succ._id
foundUser.favorites.find(function(value, index) {
if (value.toString() === idToRemove.toString()) {
return foundUser.favorites.splice(index, 1);
}
});
foundUser.save(function(err, success) {
if (err) {
console.log(`Error: ${err}`);
} else {
User.findById(userId)
.populate('favorites', 'symbol')
.exec(function(err, returnedFavs) {
axios.get('https://api.coinmarketcap.com/v1/ticker/?limit=0')
.then(function(response) {
res.render('favorites', {user: foundUser, coinIds: response.data, favs: returnedFavs.favorites})
})
})
}
})
}
})
}
})
})
// view user's portfolio
app.get("/portfolio/:id", function (req, res) {
var userId = req.params.id;
console.log(userId);
User.findById(userId, function(err, user) {
if (err) {
console.log(`Error: ${err}`);
} else {
User.findById(userId)
.populate('portfolio', 'symbol qty')
.exec(function(err, returnedPort) {
axios.get('https://api.coinmarketcap.com/v1/ticker/?limit=0')
.then(function(response) {
console.log(returnedPort.portfolio);
res.render('portfolio', {user: user, coinIds: response.data, portfolio: returnedPort.portfolio})
})
})
}
})
});
// a user's view if they are not signed in
app.get('/portfolio', function(req, res) {
res.render('portfolio', {user: req.user})
})
// a user's view if they are not signed in
app.get('/favorites', function(req, res) {
res.render('favorites', {user: req.user})
})
// Get news
app.get("/news", function (req, res) {
res.render("news", {user: req.user});
});
// signup page
app.get("/signup", function (req, res) {
res.render("signup");
});
// login page
app.get("/login", function (req, res) {
res.render("login");
});
//view all data in db
app.get('/all', function(req, res) {
User.find(function(err, allUsers) {
if (err) {
console.log("Error getting all Users: " + err);
} else {
Coin.find(function(err, allCoins) {
if (err) {
console.log(`Error: ${err}`);
} else {
res.json({users: allUsers, coins: allCoins})
}
})
}
})
})
// Register new user and redirect to profile
app.post("/signup", function (req, res) {
console.log(req.body);
User.register(new User({ username: req.body.username}), req.body.password,
function () {
passport.authenticate("local")(req, res, function() {
req.user.save();
res.redirect('/');
})
}
)
});
// user's profile page
app.get('/user/:id', function(req, res) {
var userId = req.params.id;
User.findById(userId, function(err, succ) {
if (err) {
console.log("Error: " + err);
} else {
console.log(userId + " " + succ._id);
res.render('profile', {user: succ, req: userId})
}
})
})
// page for user to update profile
app.get('/user/:id/update', function(req, res) {
var Id = req.user._id;
var userId = req.params.id;
User.findById(userId, function(err, succ) {
if (err) {
console.log("Error: " + err);
} else {
res.render('update_profile', {user: succ, req: userId, id: Id})
}
})
})
// save changes to profile update
app.put('/user/:id', function(req, res) {
var Id = req.user._id;
var userId = req.params.id;
User.findById(userId, function(err, foundUser) {
if (err) {
console.log("Error: " + err);
} else {
console.log("found user " + foundUser);
foundUser.username = req.body.username;
foundUser.save(function(err, updatedUserSaved) {
if (err) {
console.log(`Error: ${err}`);
} else {
console.log('User is Saved: ' + updatedUserSaved);
res.render('profile', {user: updatedUserSaved, req: userId, id: Id})
}
})
}
})
})
// delete user
app.delete('/user/:id', function(req, res) {
User.findByIdAndRemove(req.params.id, function(err, deletedUser) {
if (err) {
console.log(`Error: ${err}`);
} else {
console.log("User Deleted Sucessfully");
res.redirect('/');
}
})
})
// login
app.post("/login", passport.authenticate("local"), function (req, res) {
User.findOne({email: req.body.email}, function(err, succ){
if (err) {
console.log(`Error: ${err}`);
} else {
if (req.body.password) {
res.redirect('/');
} else {
res.sendStatus(404);
}
}
})
});
// logout (broken)
app.get('/logout', function(req, res) {
req.logout();
res.redirect('/');
});
// listen on port 3000
app.listen(process.env.PORT || 3000, function() {
console.log("server started");
});