-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
344 lines (315 loc) · 9.97 KB
/
app.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
require('dotenv').config()
const express = require('express');
const mysql = require('mysql');
const bodyParser = require('body-parser');
const jwt = require('jsonwebtoken')
const bcrypt = require('bcryptjs')
const cors = require('cors')
// Create connection
const db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: process.env.password,
database: 'sunnona'
});
// Connect
db.connect((err) => {
if (err) {
throw err;
}
console.log('MySql Connected...');
});
const app = express();
app.use(express.json({}))
app.use(cors());
app.post('/register', (req, res) => {
const { email, password } = req.body;
// console.log(email, password)
bcrypt.genSalt(5, (error, salt) => {
bcrypt.hash(password, salt, (error, hash) => {
if (error) throw error;
else {
let checksql = `select * from user where name=` + mysql.escape(email);
let query = db.query(checksql, (err, result) => {
if (err) throw new Error(err);
if (result[0]) {
console.log(result)
res.send('user already exist with same email');
} else {
let register = { name: email, password: hash };
let sql = 'INSERT INTO user SET ?';
let query = db.query(sql, register, (err, result) => {
if (err) throw err;
const payload = {
user: {
email: email
}
}
jwt.sign(
payload,
"5646546",
{ expiresIn: 10000 },
(error, token) => {
if (error) throw error;
console.log(token)
res.json({ user: { token: token, name: email } });
}
)
});
}
})
}
})
})
})
app.post('/login', (req, res) => {
const { email, password } = req.body;
let sqlSearch = "select password from user where name=" + mysql.escape(email);
let query = db.query(sqlSearch, (err, result) => {
if (err) throw new Error(err);
// const resultJson = JSON.parse(result)
// console.log(result[0].password)
if (result[0]) {
bcrypt.compare(password, result[0].password, (err, isMatch) => {
if (err) throw new Error(err);
if (!isMatch) {
console.log(result)
res.send('Hello user')
} else {
const payload = {
user: {
email: email
}
}
jwt.sign(
payload,
"5646546",
{ expiresIn: 10000 },
(error, token) => {
if (error) throw error;
console.log(token)
res.json({ user: { token: token, name: email } });
}
)
}
})
} else {
res.send('User not found');
}
})
})
app.get('/albums', (req, res) => {
// let sql = "select a.name as albumName, a.id, a.image as albumImage, t.name as trackName, t.audioLink as audio from album a, track t where a.id=t.albumId";
let sql = "select a.name as artistName, A.image as albumImage, A.id, t.id as trackId, t.name as trackName, A.name as albumName, t.audioLink as audio from artist a, track t, album A, sings s where a.name=s.artistName and s.albumId=t.albumId and s.trackId=t.id and t.albumId=A.id;"
db.query(sql, (error, results) => {
if (error) throw new Error(error);
// res.send(results)
const albumDetails = []
if (results) {
const albumResult = []
for (result of results) {
const album = {
albumId: result.id,
albumName: result.albumName,
albumArtwork: result.albumImage,
songs: []
}
for (result1 of results) {
if (result.id === result1.id) {
const song = {
id: result1.trackId,
name: result1.trackName,
src: result1.audio,
artistName: result1.artistName
}
album.songs.push(song);
}
}
if (albumResult.length !== 0) {
var count = 0;
for (result of albumResult) {
if (result.albumName === album.albumName) {
count++;
break;
}
}
if (count === 0) {
albumResult.push(album)
}
} else {
albumResult.push(album)
}
}
res.send(albumResult);
}
})
})
app.get('/artists', (req, res) => {
let sql = "select a.name as artistName, a.image as artistImage, t.albumId as albumId, t.id as trackId, t.name as trackName, t.audioLink as audio from artist a, track t, sings s where a.name=s.artistName and s.trackid=t.id and s.albumId=t.albumId";
db.query(sql, (error, results) => {
if (error) throw new Error(error);
// res.send(result);
const artistsResult = [];
for (result of results) {
const artist = {
albumId: result.albumId,
albumName: result.artistName,
albumArtwork: result.artistImage,
songs: []
}
for (artist1 of results) {
if (artist1.artistName === artist.albumName) {
const song = {
id: artist1.trackId,
name: artist1.trackName,
src: artist1.audio
}
artist.songs.push(song);
}
}
if (artistsResult.length !== 0) {
var count = 0;
for (result of artistsResult) {
if (artist.albumName === result.albumName) {
count++;
break;
}
}
if (!count) {
artistsResult.push(artist)
}
} else {
artistsResult.push(artist);
}
}
res.send(artistsResult)
})
})
app.get('/genres', (req, res) => {
let sql = "select t.name as trackName, t.audioLink as audioLink, t.id as trackId, g.name as genreName, g.image as genreImage, t.albumId as albumId from track t, genre g where t.genreId=g.id";
db.query(sql, (err, results) => {
if (err) throw new Error(err);
// res.send(results)
var genreResult = [];
for (result of results) {
var genre = {
albumId: result.albumId,
albumName: result.genreName,
albumArtwork: result.genreImage,
songs: []
}
for (result1 of results) {
if (result1.genreName === result.genreName) {
let song = {
id: result1.trackId,
name: result1.trackName,
src: result1.audioLink
}
genre.songs.push(song);
}
}
// console.log(genreResult.length)
if (genreResult.length !== 0) {
var count = 0;
for (result of genreResult) {
if (genre.albumName === result.albumName) {
// console.log(result.genreName, genre.albumName)
count++;
break;
}
}
if (count === 0) {
genreResult.push(genre)
}
} else {
genreResult.push(genre);
}
}
res.send(genreResult)
})
})
app.post('/add/favourite', (req, res) => {
let data = { name: req.body.userId, trackId: req.body.trackId, albumId: req.body.albumId };
let sql = `insert into favourites set ?`;
db.query(sql, data, (error, result) => {
if (error) {
let sql1 = "delete from favourites where name=" + mysql.escape(data.name) + " and trackId=" + mysql.escape(data.trackId) + " and albumId=" + mysql.escape(data.albumId);
db.query(sql1, (error, result1) => {
if (error) throw new Error(error);
// return (res.send(result1))
})
};
res.send(result);
})
})
app.post('/get/favourites', (req, res) => {
console.log(req.body)
let sql = "select a.name as artistName, t.name as trackName, t.audioLink as audioLink from favourites f, track t, artist a, sings s where f.trackId=t.id and f.albumId=t.albumId and s.trackId=t.id and s.albumId=t.albumId and a.name=s.artistName and f.name=" + mysql.escape(req.body.loggedInUser);
db.query(sql, (error, results) => {
if (error) throw new Error(error)
let songs = [];
for (result1 of results) {
const details = {
name: result1.trackName,
src: result1.audioLink,
artistName: result1.artistName
}
songs.push(details)
}
res.send(songs);
})
})
app.post('/incCount', (req, res) => {
// console.log(req.body);
const data = { trackId: req.body.trackId, albumId: req.body.albumId };
let sql = "update track set count=count+1 where id=" + mysql.escape(data.trackId) + " and albumId=" + mysql.escape(data.albumId);
db.query(sql, (err, result) => {
if (err) throw new Error(err);
res.send(result);
})
})
app.get('/songs/sorted', (req, res) => {
let sql = "select t.name as trackName, t.audioLink as audioLink, a.name as artistName from track t, artist a, sings s where t.id=s.trackId and t.albumId=s.albumId and a.name=s.artistName order by t.count desc"
db.query(sql, (error, results) => {
if (error) throw new Error(error);
// res.send(result);
let songs = [];
for (result1 of results) {
const details = {
name: result1.trackName,
src: result1.audioLink,
artistName: result1.artistName
}
songs.push(details)
}
res.send(songs);
})
})
app.get('/lang/:lang', (req, res) => {
let sql = "select t.name as trackName, t.audioLink as audioLink, a.name as artistName from track t, artist a, sings s where t.id=s.trackId and t.albumId=s.albumId and a.name=s.artistName and lang=" + mysql.escape(req.params.lang);
db.query(sql, (err, results) => {
if (err) throw new Error(err);
let songs = [];
for (result1 of results) {
const details = {
name: result1.trackName,
src: result1.audioLink,
artistName: result1.artistName
}
songs.push(details)
}
res.send(songs);
})
})
app.get('/logout', (req, res) => {
if (req.user) {
req.logout();
console.log('logged out');
res.send({ msg: 'logging out' })
} else {
res.send({ msg: 'no user to log out' })
}
})
app.listen('4000', () => {
console.log('server is running at 4000');
})