-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
166 lines (147 loc) · 4.5 KB
/
server.js
File metadata and controls
166 lines (147 loc) · 4.5 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
const express = require('express');
const app = express();
const cors = require('cors')
const database = require('./db/knex');
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors())
app.get('/', (request, response) => {
response.send('Hello 8track!');
})
if (!module.parent) {
app.listen(process.env.PORT || 8080, () => {
console.log('8track server is live on port 8080! (http://localhost:8080)');
});
}
app.get('/api/v1/artists', (request, response) => {
database('artists').select()
.then((artists) => {
response.status(200).json(artists);
})
.catch((error) => {
console.error('something is wrong with the DB');
});
})
app.get('/api/v1/songs', (request, response) => {
database('songs').select()
.then((songs) => {
response.status(200).json(songs);
})
.catch((error) => {
console.error('something is wrong with the database');
});
})
app.get('/api/v1/artists/:id', (request, response) => {
database('songs').where('artist_id', request.params.id).select()
.then((songs) => {
response.status(200).json(songs);
})
.catch((error) => {
console.error('something is wrong with the redirect')
});
})
app.get('/api/v1/artists-songs', (request, response) => {
database('artists').join('songs', 'artists.id', '=', 'songs.artist_id')
.select('artists.name', 'songs.title')
.then((data) => {
response.status(200).json(data);
})
.catch((error) => {
console.error('something is wrong with the request');
});
})
app.post('/api/v1/artists', (request, response) => {
const name = request.body.name;
const artist = { name };
database('artists').insert(artist)
.then(() => {
database('artists').select()
.then((artists) => {
response.status(201).json(artists);
})
.catch((error) => {
console.error('new artist was not created, try again.')
});
})
})
app.post('/api/v1/songs', (request, response) => {
const title = request.body.title;
const artist_id = request.body.artist_id;
const song = { title, artist_id };
database('songs').insert(song)
.then(() => {
database('songs').select()
.then((songs) => {
response.status(201).json(songs);
})
.catch((error) => {
console.error('new song was not created, try again.')
});
})
})
app.put('/api/v1/artists/:id', (request, response) => {
const id = request.params.id;
const name = request.body.name;
const updated_at = new Date();
database('artists').where('id', id)
.update('name', name)
.update('updated_at', updated_at)
.then(() => {
database('artists').select()
.then((artists) => {
response.status(200).json(artists)
})
.catch((error) => {
console.error('artist update was unsuccessful, try again')
});
})
})
app.put('/api/v1/songs/:id', (request, response) => {
const id = request.params.id;
const title = request.body.title;
const artist_id = request.body.artist_id;
const updated_at = new Date();
database('songs').where('id', id)
.update('title', title)
.update('artist_id', artist_id)
.update('updated_at', updated_at)
.then(() => {
database('songs').select()
.then((songs) => {
response.status(200).json(songs)
})
.catch(() => {
console.error('song update was unsuccessful, try again')
});
})
})
app.delete('/api/v1/artists/:id', (request, response) => {
const id = request.params.id;
database('artists').where('id', id)
.del()
.then(() => {
database('artists').select()
.then((artists) => {
response.status(204).json(artists)
})
.catch(() => {
console.error('song deletion unsuccessful, try again')
});
})
})
app.delete('/api/v1/songs/:id', (request, response) => {
const id = request.params.id;
database('songs').where('id', id)
.del()
.then(() => {
database('songs').select()
.then((songs) => {
response.status(204).json(songs)
})
.catch(() => {
console.error('song deletion unsuccessful, try again')
});
})
})
module.exports = app;