-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
477 lines (420 loc) · 13.3 KB
/
index.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
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
require('dotenv').config();
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const db = require('./models/db');
const axios = require('axios');
const API_KEY = process.env.EVENTFUL_API_KEY;
const lastfm_key = process.env.LASTFM_API_KEY;
app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Session modules
const session = require('express-session')
const pgSession = require('connect-pg-simple')(session);
app.use(session({
store: new pgSession({
pgPromise: db
}),
secret: 'abc123kasfsdbukbfrkqwuehnfioaebgfskdfhgcniw3y4fto7scdghlusdhbv',
saveUninitialized: false
}));
// =========================
// Protecting User Account
// =========================
function protectRoute(req, res, next) {
let isLoggedIn = req.session.user ? true : false;
if (isLoggedIn) {
console.log(`${req.session.user.name} is logged in`);
next();
} else {
console.log('not logged in');
res.redirect('/login')
}
}
app.use((req, res, next) => {
let isLoggedIn = req.session.user ? true : false;
console.log(`on ${req.path}, is a user logged in? ${isLoggedIn}`);
next();
});
app.get('/API/user/isValid', (req, res) => {
console.log(req.session);
console.log(req.session.user);
let user = req.session.user;
let isLoggedIn = user ? true : false;
res.send({
isLoggedIn,
user
})
})
//============
//USER METHODS
//============
const User = require('./models/User');
// =====================
// User registration
// =====================
app.post('/API/register', (req, res) => {
const newUsername = req.body.username;
User.getByUsername(newUsername)
.then((doesExist) => {
console.log(doesExist)
if (doesExist) {
console.log('this user already exists')
res.json({ status: "taken" })
}
})
.catch((err) => {
console.log('theres been an error')
console.log('new user added')
const newPassword = req.body.password;
const newEmail = req.body.emailAddress;
const newHome = req.body.home;
const newName = req.body.name;
const newLikes = req.body.likes;
const newDislikes = req.body.dislikes;
const newPal = req.body.pal;
User.add(newName, newUsername, newPassword, newEmail, newHome, newLikes, newDislikes, newPal)
.then((newUser) => {
req.session.user = newUser;
req.session.save(() => {
res.json({ status: "good to go" })
})
})
});
});
// =====================
// User Login
// =====================
app.post('/API/login', (req, res) => {
const theUsername = req.body.username;
const thePassword = req.body.password;
User.getByUsername(theUsername)
.then((theUser) => {
console.log(theUser)
if (theUser.passwordDoesMatch(thePassword)) {
req.session.user = theUser
console.log(`you're in`)
console.log(`${req.session.user.username}`)
req.session.save(() => {
res.json({ status: 'good to go' })
})
} else {
console.log('username or password incorrect')
res.json({ status: 'incorrect' })
}
})
})
// =====================
// User Profile
// =====================
app.get('/API/profile', protectRoute, (req, res) => {
User.getUserById(req.session.user.id)
.then(user => {
res.send(user);
console.log(`sending ${user.likes} info like a mutha`)
})
})
// ==================
// Other User Methods
// ==================
//GET USER BY ID
app.get('/myInfo', (req, res) => {
User.getUserById(res.session.user.id)
.catch(user => {
res.send(user);
console.log('sending user info like nobody\'s business')
})
})
//GET USERS BY SHOW
app.post('/showUsers', (req, res) => {
console.log(req.body.eventID)
let eventID = req.body.eventID;
User.getUsersGoingToShow(eventID)
.then(shows => {
res.send(shows);
console.log('shows. BAM');
})
})
//ADD USER GOING TO SHOW
app.post('/addUserGoingToShow', (req, res) => {
console.log(`${req.session.user.username} wants to see ${req.body.eventID} like a boss`)
let eventID = req.body.eventID;
User.addUserGoingToShow(req.session.user.id, eventID)
.then(shows => {
res.send(shows);
console.log('confirm boss status');
})
})
//GET FRIENDS BY USER
app.get('/myFriends', (req, res) => {
User.getFriendsOfUser(req.session.user.id)
.then(friends => {
res.send(friends);
console.log('friends are the glue that sticks concerts together');
});
});
//GET USER AS PAL
app.post('/palProfile', (req, res) => {
console.log('HELLO')
User.getUserById(req.body.userID)
.then(pal => {
res.send(pal);
console.log(`here's the dirt on your friend ${pal.username}`)
})
})
//GET OTHER USER FRIENDS
app.post('/palFriends', (req, res) => {
User.getFriendsOfUser(req.body.userID)
.then(friends => {
res.send(friends);
console.log('keep your friends\' friends close...')
})
})
//UPDATE USER INFO
app.post('/updateUser', (req, res) => {
User.updateUserInfo(req.session.user.id, req.body.name, req.body.username, req.session.user.pwhash, req.body.email, req.body.home, req.body.likes, req.body.dislikes, req.body.pal)
.then(newUser => {
res.redirect('/profile');
console.log('user updated?')
})
});
//ADD USER FRIEND
app.post('/addToUserFriends', (req, res) => {
console.log(req.body.friend_id)
User.addFriend(req.session.user.id, req.body.friend_id)
.then(newFriend => {
res.send(newFriend);
console.log('you made a friend!')
}).catch(error => {
console.error(error)
})
})
//DELETE A FRIEND-- TURNS OUT THEY WERE A DOUCHE AFTER ALL
app.post('/deleteFriend', (req, res) => {
User.deleteFriend(req.session.user.id, req.body.friend_id)
.then(console.log('so much for friends'));
});
//DELETE USER
app.get('/deleteAll', (req, res) => {
console.log('my name is delete')
User.deleteUser(req.session.user.id)
.then(console.log('you have been deleted and'))
.then(res.send('your account has been deleted'))
.catch(error => {
console.error(error)
})
});
//==============
//ARTIST METHODS
//==============
const Artist = require('./models/Artist');
// GET USER'S FAVORITE ARTISTS
app.get('/myArtists', (req, res) => {
Artist.getArtistsByUser(req.session.user.id)
.then(artists => {
res.send(artists);
console.log('got me some artists');
});
});
//GET OTHER's FAVE ARTISTS
app.post('/palArtists', (req, res) => {
Artist.getArtistsByUser(req.body.userID)
.then(artists => {
res.send(artists);
console.log('keep their artists closer');
})
.catch(error => {
console.error(error)
})
});
//DELETE ARTIST FROM USER_ARTISTS
app.post('/deleteArtistFromUser', (req, res) => {
console.log('wake up! i\'m deleting artists!')
Artist.deleteArtistFromUser(req.body.artist_id, req.session.user.id)
.then(artists => {
res.send(artists);
console.log(`${req.session.user.name} don't need no stinkin artists`);
})
.catch(error => {
console.error(error)
})
})
//===================
// Artists from API
//====================
app.post('/APIartistList', (req, res) => {
console.log(req.body.artistSearch)
console.log(req.body.searchArtist)
let artistSearch = req.body.searchArtist;
const APIArtists = async () => {
try {
return await axios.get(`http://ws.audioscrobbler.com/2.0/?`, {
params: {
method: `artist.search`,
artist: `${artistSearch}`,
api_key: `${lastfm_key}`,
format: `json`,
limit: `10`
}
})
}
catch (error) {
console.error(error)
}
}
const artists = (APIArtists()
.then(data => {
const artistResults = data.data.results.artistmatches.artist
// console.log(data.data.results)
artistArray = artistResults.map(artistObj => {
let a = new Artist(
artistObj.id,
artistObj.name
)
console.log(a)
return a;
})
// res.send(artistResults)
console.log('api artists received')
return artistArray
// return artistArray
}))
.then(artistArray => {
res.send(artistArray)
})
.catch(error => {
console.error(error)
})
});
// ==================
// ADD ARTIST
// ==================
app.post('/addArtistToUser', (req, res) => {
Artist.addArtistToUser(req.body.artist, req.session.user.id)
});
//=============
//EVENT METHODS
//=============
const Event = require('./models/Event');
//GET ALL EVENTS
app.get('/eventList', (req, res) => {
Event.getFilteredShows("")
.then(results => {
res.send(results);
console.log('you get ALL the events')
})
});
//GET EVENTS LIST FOR USER LOGGED IN
app.get('/upcomingShows', (req, res) => {
// let id = req.body.userID
Event.getShowsForUser(req.session.user.id)
.then(shows => {
res.send(shows);
console.log('got your user shows right here');
});
});
//GET EVENTS LIST FOR OTHER USER
app.post('/otherShows', (req, res) => {
console.log(`${req.body.userID} emmer-effer`)
Event.getShowsForUser(req.body.userID)
.then(shows => {
res.send(shows);
console.log('sending other shows');
});
});
//DELETE EVENT FROM USER
app.post('/deleteEventfromUser', (req, res) => {
Event.deleteEventfromUserShows(req.body.eventID, req.session.user.id)
});
//=============
//EVENTS FROM API
//=============
const APIEvent = require('./models/APIEvent');
app.post('/APIeventList', (req, res) => {
console.log(req.body.searchLocation);
let location = req.body.searchLocation;
let keyword = req.body.searchKeyword;
console.log(req.body.searchKeyword);
let artist = req.body.searchArtist;
console.log(req.body.searchArtist);
const APIEvents = async () => {
try {
return await axios.get(`http://api.eventful.com/json/events/search?`, {
// app_key=${API_KEY}&keywords=concert+music+${keyword}&location=Atlanta+GA&date=This+Weekend&page_size=25
params: {
app_key: `${API_KEY}`,
keywords: `concert music ${artist} ${keyword}`,
location: `${location}`,
date: `future`,
page_size: 25,
sort_order: `date`
}
})
}
catch (error) {
console.error(error)
}
}
const events = (APIEvents()
.then(data => {
console.log(data.data.total_items);
console.log('^^ data ends');
let eventArray;
if (data.data.total_items === '0') {
console.log('this is the if');
eventArray = [{ artist: null }];
// return eventArray;
}
else {
console.log('this is the else');
eventArray = data.data.events.event.map(eventObj => {
let a = new APIEvent(
eventObj.id,
eventObj.title,
eventObj.venue_name,
eventObj.city_name,
eventObj.region_abbr,
eventObj.start_time)
console.log(a);
return a;
})
}
return eventArray
// res.send(eventArray)
// }
})
.then(eventArray => {
res.send(eventArray);
})
.catch(error => {
console.error(error)
}))
console.log('api call nailed!');
});
app.post('/addShowToDb', (req, res) => {
console.log(req.body.artist);
console.log(req.session.user.username)
Event.addEvent(req.body.eventID, req.body.artist, req.body.venue, req.body.city, req.body.state, req.body.date)
.then(() => {
User.addUserGoingToShow(req.session.user.id, req.body.eventID)
.then(() => {
res.send('you did it!')
})
})
});
// ===============
// Logout
// ===============
app.post('/logout', (req, res) => {
req.session.destroy(() => {
// console.log('you have logged out')
res.redirect('/login')
})
});
// =============
// Connexion
// =============
app.listen(5000, () => {
console.log('Ain\'t we got fun?');
});