-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
128 lines (98 loc) · 3.53 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
const connection = require('./config')
const express = require('express')
const server = express()
const bodyParser = require('body-parser')
server.use(bodyParser.json());
server.use(bodyParser.urlencoded({extended: true}))
// be able to create a new playlist --> done
server.post('/api/playlist', (req, res) => {
connection.query('INSERT INTO playlist SET ?', req.body, (err, result) => {
if (err) {
console.log(err);
res.status(500).send('Sorry, there was an error creating your playlist');
} else {
res.json('Perfect, you create a new playlist');
}
});
});
// be able to see a playlist by entering its id in the url --> done
server.get('api/playlist/:id', (req, res) => {
connection.query('SELECT * FROM playlist WHERE id = ?', req.params.id, (err, results) => {
if (err) {
console.log(err);
res.sendStatus(404);
} else res.json(results[0]);
});
});
// create and assign a song to a playlist --> done
server.post('/api/song', (req, res)=>{
connection.query('INSERT INTO songs SET ?', req.body, (err, result)=>{
if (err) {
console.log(err);
res.sendStatus(404);
} else {
console.log(result);
res.json('Congrats! You created your song and added to a playlist')};
});
})
// list all the songs from a playlist --> done
server.get('/api/playlist/:id/songs', (req, res) => {
connection.query('SELECT * FROM songs WHERE playlist_id = ?', req.params.id, (err, result) => {
console.log('conectando a la db')
if (err) {
console.log(err);
res.status(404).send('Sorry, your playlist does not exist')
} else {
res.json(result)
}
})
});
// be able to delete a playlist --> done
server.delete('/api/playlist/:id', (req, res)=>{
connection.query('DELETE FROM playlist WHERE id = ?', req.params.id, (err, result)=>{
if (err) {
console.log(err);
res.status(404).send('Sorry, it cannot be possible delete your playlist')
} else {
console.log(result)
res.json('You deleted your playlist')
}
});
});
// be able to modify a playlist --> done
server.patch('/api/playlist/:id', (req, res)=> {
connection.query('UPDATE playlist SET ? WHERE id=?', [req.body, req.params.id], (err, results)=> {
if (err) {
console.log(err);
res.status(404).send('Sorry, it cannot be possible modify your playlist')
} else {
res.json('You modified your playlist')
}
});
});
// delete a song from a playlist --> done
server.delete('/api/song/:id', (req, res) => {
console.log(req.params.id);
connection.query('DELETE FROM songs where id = ?', req.params.id, (err, result)=>{
if (err) {
console.log(err);
res.status(404).send('Sorry, it cannot be possible delete your song from the playlist')
} else {
res.json('You deleted the song')
}
});
});
// edit a song from a playlist --> done
server.patch('/api/song/:id', (req, res) => {
connection.query('UPDATE songs SET ? where id = ?', [req.body, req.params.id], (err, result) => {
if (err) {
console.log(err);
res.status(404).send('Sorry, your playlist does not exist')
} else {
res.json('Congrats! You modified the song')
}
});
});
server.listen(3004, function(){
console.log('listening on port 3004')
});