-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
271 lines (240 loc) · 6.99 KB
/
Copy pathserver.js
File metadata and controls
271 lines (240 loc) · 6.99 KB
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
var express = require("express");
var path = require("path");
var bodyParser = require("body-parser");
var mongodb = require("mongodb");
var ObjectID = mongodb.ObjectID;
var PLAYLISTS_COLLECTION = "playlists";
var SONGS_COLLECTION = "songs";
var app = express();
app.use(express.static(__dirname + "/public"));
app.use(bodyParser.json());
// Create a database variable outside of the database connection callback to reuse the connection pool in your app.
var db;
// Connect to the database before starting the application server.
mongodb.MongoClient.connect(process.env.MONGODB_URI, function (err, database) {
if (err) {
console.log(err);
process.exit(1);
}
// Save database object from the callback for reuse.
db = database;
console.log("Database connection ready");
// Initialize the app.
var server = app.listen(process.env.PORT || 8080, function () {
var port = server.address().port;
console.log("App now running on port", port);
});
});
// Generic error handler used by all endpoints.
function handleError(res,reason,message,code) {
console.log("ERROR:" + reason);
res.status(code || 500).json({"error": message});
}
// PLAYLISTS API ROUTES BELOW
/*
* "/playlists"
* GET: finds all playlists
* POST: creates a new playlist
*/
app.get("/playlists",function(req,res) {
db.collection(PLAYLISTS_COLLECTION).find({}).toArray(function(err,docs) {
if (err) {
handleError(res, err.message, "Failed to get playlists");
} else {
res.status(200).json(docs);
}
});
});
app.post("/playlists",function(req,res) {
var newPlaylist = req.body;
newPlaylist.createDate = new Date();
if (req.body.name == null) {
handleError(res, "Invalid user input","Did not provide a name",400);
return;
}
if (req.body.playlistId == null) {
handleError(res, "Invalid user input", "Did not provide a playlist ID",400);
return;
}
if (req.body.userId == null) {
handleError(res, "Invalid user input", "Did not provide a host ID",400);
return;
}
if (req.body.hostIsLoggedInToSpotify == null) {
handleError(res, "Invalid user input", "Did not provide Spotify login status",400);
return;
}
if (req.body.hostIsLoggedInToSoundcloud == null) {
handleError(res, "Invalid user input", "Did not provide Soundcloud login status",400);
return;
}
if (req.body.hostIsLoggedInToAppleMusic == null) {
handleError(res, "Invalid user input", "Did not provide Apple Music login status",400);
return;
}
db.collection(PLAYLISTS_COLLECTION).insertOne(newPlaylist, function(err,doc) {
if (err) {
handleError(res,err.message,"Failed to create new playlist.");
} else {
res.status(201).json(doc.ops[0]);
}
});
});
/*
* "/playlists/:id"
* GET: find playlist by id
* PUT: update playlist by id
* DELETE: delete playlist by id
*/
app.get("/playlists/:id",function(req,res) {
db.collection(PLAYLISTS_COLLECTION).findOne({_id: new ObjectID(req.params.id)}, function(err,doc) {
if (err) {
handleError(res, err.message, "Failed to get playlist");
} else {
res.status(200).json(doc);
}
});
});
app.put("/playlists/:id",function(req,res) {
var updateDoc = req.body;
delete updateDoc._id;
db.collection(PLAYLISTS_COLLECTION).updateOne(
{_id: new ObjectID(req.params.id)},
{$set: updateDoc},
function(err,doc) {
if (err) {
handleError(res, err.message, "Failed to update playlist");
} else {
res.status(204).end();
}
});
});
app.delete("/playlists/:id",function(req,res) {
db.collection(PLAYLISTS_COLLECTION).deleteOne({_id: new ObjectID(req.params.id)}, function(err,result) {
if (err) {
handleError(res,err.message, "Failed to delete playlist");
} else {
res.status(204).end();
}
});
});
// FETCH PLAYLIST BY PLAYLISTID
app.get("/playlistid/:id",function(req,res) {
db.collection(PLAYLISTS_COLLECTION).findOne({playlistId: req.params.id}, function(err,doc) {
if (err) {
handleError(res, err.message, "Failed to get songs for playlist");
} else {
res.status(200).json(doc);
}
});
});
// FETCHING PLAYLIST SONGS API ROUTES BELOW
app.get("/playlistsongs/:id",function(req,res) {
db.collection(SONGS_COLLECTION).find({playlistId: req.params.id}).toArray(function(err,docs) {
if (err) {
handleError(res, err.message, "Failed to get songs for playlist");
} else {
res.status(200).json(docs);
}
});
});
// SONGS API ROUTES BELOW
/*
* "/songs"
* GET: finds all songs
* POST: creates a new song
*/
app.get("/songs",function(req,res) {
db.collection(SONGS_COLLECTION).find({}).toArray(function(err,docs) {
if (err) {
handleError(res, err.message, "Failed to get songs");
} else {
res.status(200).json(docs);
}
});
});
app.post("/songs",function(req,res) {
var newSong = req.body;
newSong.createDate = new Date();
if (!(req.body.playlistId && (req.body.title || req.body.description))) {
handleError(res, "Invalid user input","Must provide a playlist ID and song title or description",400);
}
db.collection(SONGS_COLLECTION).insertOne(newSong, function(err,doc) {
if (err) {
handleError(res,err.message,"Failed to create new song.");
} else {
res.status(201).json(doc.ops[0]);
}
});
});
/*
* "/songs/:id"
* GET: find song by id
* PUT: update song by id
* DELETE: delete song by id
*/
app.get("/songs/:id",function(req,res) {
db.collection(SONGS_COLLECTION).findOne({_id: new ObjectID(req.params.id)}, function(err,doc) {
if (err) {
handleError(res, err.message, "Failed to get song");
} else {
res.status(200).json(doc);
}
});
});
// This will update the fields specified.
// Typically use it for Active Status
app.put("/songs/:id",function(req,res) {
var updateDoc = req.body;
delete updateDoc._id;
db.collection(SONGS_COLLECTION).updateOne(
{_id: new ObjectID(req.params.id)},
{$set: updateDoc},
function(err,doc) {
if (err) {
handleError(res, err.message, "Failed to update song");
} else {
res.status(204).end();
}
});
});
app.delete("/songs/:id",function(req,res) {
db.collection(SONGS_COLLECTION).deleteOne({_id: new ObjectID(req.params.id)}, function(err,result) {
if (err) {
handleError(res,err.message, "Failed to delete song");
} else {
res.status(204).end();
}
});
});
/*
* "/upvote/:id"
*
*/
app.post("/upvote/",function(req,res) {
db.collection(SONGS_COLLECTION).updateOne(
{_id: new ObjectID(req.body.id)},
{$inc: {"score":1}},
function(err,doc) {
if (err) {
handleError(res, err.message, "Failed to upvote song");
} else {
res.status(204).end();
}
});
});
/*
* "/downvote/:id"
*/
app.post("/downvote/",function(req,res) {
db.collection(SONGS_COLLECTION).updateOne(
{_id: new ObjectID(req.body.id)},
{$inc: {"score":-1}},
function(err,doc) {
if (err) {
handleError(res, err.message, "Failed to downvote song");
} else {
res.status(204).end();
}
});
});