-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
60 lines (52 loc) · 1.79 KB
/
app.js
File metadata and controls
60 lines (52 loc) · 1.79 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
const express = require('express');
const app = express();
const cors = require('cors');
const queries = require('./queries');
const port = process.env.PORT || 8000;
app.use(express.static('public'));
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({
extended: true
}))
app.get('/', (req, res) => {
queries.listAll().then(playlists => res.send(playlists));
});
app.get('/playlist/:playlistName', (req, res) => {
const { playlistName } = req.params;
queries.getPlaylistByName(playlistName).then(playlist => res.send(playlist));
});
app.post('/playlist', (req, res) => {
const { playlistName } = req.body;
// send response if playlist doesn't exist
if (playlistName) {
queries.getPlaylistByName(playlistName).then(playlist => res.send(playlist));
} else {
res.send('there was a problem with your request');
}
});
app.post('/playlist/new', (req, res) => {
const newPlaylist = req.body;
queries.getPlaylistByName(newPlaylist.playlistName).then(playlistStatus => {
isNameADuplicate = false;
if (playlistStatus.length >= 1) {
isNameADuplicate = true;
return res.send('That playlist name already exists, choose another');
}
if (newPlaylist && !isNameADuplicate) {
return queries.createPlaylist(newPlaylist).then(newPlaylistStatus => res.sendStatus(204));
} else {
return res.send('There was an issue with your request');
}
});
});
app.delete('/playlists/:playlistName/delete', (req, res) => {
const { playlistName } = req.params;
queries.deletePlaylist(playlistName).then(res.sendStatus(204));
});
// get playlist by name
// playlist should have name, id
// status live?
app.listen(port, () => {
console.log(`listening on ${port}`);
});