forked from Anonymous-Pizza/jukebox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
246 lines (203 loc) · 5.99 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
// *** Express ***
const express = require('express');
const app = express();
// *** Webpack ***
const env = require('./env/credentials.js');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpack = require('webpack');
const webpackConfig = require(`./webpack.config${env.prod ? '.prod' : ''}.js`);
const compiler = webpack(webpackConfig);
if (!env.prod) {
app.use(webpackDevMiddleware(compiler, {
hot: true,
filename: 'bundle.js',
publicPath: '/',
stats: {
colors: true,
},
historyApiFallback: true,
}));
}
// *** Static Assets ***
app.use(express.static(__dirname + '/public'));
// *** Database ***
const Db = require('./db/config').mongoose;
const User = require('./db/config').user;
const Song = require('./db/config').song;
const Party = require('./db/config').party;
// *** Parser ***
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
const cookieParser = require('cookie-parser');
app.use(cookieParser());
const querystring = require('querystring');
// *** Helpers ***
const spotifyHelpers = require('./helpers/spotifyHelpers.js');
// *** Server ***
const server = app.listen(process.env.PORT || 3000, () => {
console.log('Listening at http://localhost:3000');
});
/* * * * * * * * * * * * * * * * * * * * * * * * * * *
ROUTES to ACCESS SPOTIFY API
* * * * * * * * * * * * * * * * * * * * * * * * * * */
app.get('/hostInfo', (req, res) => {
spotifyHelpers.getHostInfo(req, res);
});
// fetch song research results and send to client
app.get('/songs/search', (req, res) => {
spotifyHelpers.getTrackSearchResults(req, res, req.query.query)
});
app.get('/hostPlaylists', (req, res) => {
spotifyHelpers.getHostPlaylists(req, res);
});
app.get('/currentlyPlaying', (req, res) => {
spotifyHelpers.currentlyPlaying(req, res);
});
app.get('/playlistSongs', (req, res) => {
spotifyHelpers.getPlaylistSongs(req, res);
});
// Host Authentication
app.get('/hostLogin', (req, res) => {
spotifyHelpers.handleHostLogin(req, res);
});
app.get('/callback', (req, res) => {
spotifyHelpers.redirectAfterLogin(req, res);
});
/* * * * * * * * * * * * * * * * * * * * * * * * * * *
ROUTES to ACCESS DATABASE SONG COLLECTION
* * * * * * * * * * * * * * * * * * * * * * * * * * */
// fetch top 50 songs by netVoteCount from songs collection and send to client
app.get('/songs', (req, res) => {
Song.find({partyCode: req.query.partyCode}).sort({netVoteCount: 'descending'}).limit(50)
.then((songs) => {
res.send(songs);
});
});
// add songs to both user collection and songs collection
app.post('/songs', (req, res) => {
var songsToAdd = req.body.songs;
var partyCode = req.body.partyCode;
var userName = req.body.userName;
if (!Array.isArray(songsToAdd)){
var song = songsToAdd;
Song.find({name: song.name, partyCode: partyCode})
.then((response)=> {
if (response.length>0) {
console.log('song already in list');
} else {
new Song({
name: song.name,
artist: song.artists[0].name,
image: song.album.images[1].url,
link: song.external_urls.spotify,
upVoteCount: 1,
downVoteCount: 0,
netVoteCount: 1,
duration_ms: song.duration_ms,
userName: userName,
partyCode: partyCode
}).save();
}
res.sendStatus(201);
})
}
else {
for (var i = 0 ; i < songsToAdd.length ; i++) {
var song = songsToAdd[i].track;
new Song({
name: song.name,
artist: song.artists[0].name,
image: song.album.images[1].url,
link: song.external_urls.spotify,
upVoteCount: 1,
downVoteCount: 0,
netVoteCount: 1,
duration_ms: song.duration_ms,
userName: userName,
partyCode: partyCode
}).save();
}
res.sendStatus(201);
}
});
// update vote on songs collection
app.put('/song', (req, res) => {
Song.findOne({name: req.body.name, partyCode: req.body.partyCode})
.then(function(song) {
if (song) {
if(req.body.vote > 0) {
song.upVoteCount++;
} else {
song.downVoteCount++;
}
song.netVoteCount = song.upVoteCount - song.downVoteCount;
song.save();
res.sendStatus(201);
}
});
});
// delete song from songs collection
app.delete('/song', (req, res) => {
const songId = req.query.id;
Song.remove({'_id': songId}, (err) => {
if (err) { console.log(err); }
});
res.sendStatus(201);
});
// delete all songs from one party
app.delete('/songs', (req, res) => {
Song.remove({partyCode: req.query.partyCode}, (err) => {
if (err) { console.log(err); }
});
res.sendStatus(201);
});
/* * * * * * * * * * * * * * * * * * * * * * * * * * *
ROUTES to ACCESS DATABASE PARTY COLLECTION
* * * * * * * * * * * * * * * * * * * * * * * * * * */
//Look up party via party code
app.get('/party', (req,res) => {
Party.findOne({partyCode: req.query.partyCode})
.then((party) => {
res.send(party);
})
});
//Create new party
app.post('/party', (req,res) => {
//first check if user already has a party create
//if user already has a party, delete that one & delete songs with that party code
var newParty = new Party({
partyCode: req.body.partyCode,
partyHost: req.body.partyHost,
token: req.body.token
});
Party.findOne({partyCode: req.body.partyCode})
.then((party) => {
if(!party) {
newParty.save()
.then(() => {
res.sendStatus(201);
});
} else {
res.send("Party already exists!");
}
})
});
app.delete('/party', (req, res)=>{
Party.remove({partyCode: req.query.partyCode}, (err)=> {
if (err) {
console.log(err);
}
res.sendStatus(201);
})
});
/* * * * * * * * * * * * * * * * * * * * * * * * * * *
ALL Other Routes
* * * * * * * * * * * * * * * * * * * * * * * * * * */
app.get('/tokens', (req, res) => {
res.send(tokens);
});
// send 404 to client
app.get('/*', (req, res) => {
res.status(404).send('Not Found');
});