forked from Dipesh-Budhiraja/music_player
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
111 lines (98 loc) · 3.4 KB
/
server.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
const express = require('express');
const app=express();
const sql = require('./sql.js');
const fileUpload=require('express-fileupload');
const bodyParser=require('body-parser');
const port=process.env.PORT||5000;
var uploadID=0;
const mongodb=require('./mongodb.js');
app.use('/',bodyParser.json());
app.use('/',bodyParser.urlencoded({extended:true}));
app.use('/',express.static('public_static'));
app.use(fileUpload());
// app.get('/',function(req,res){
// res.redirect(x.html);
// });
app.post('/songs/library',function(req,res){
var query='SELECT * FROM songs';
sql.sqlQuery(query,function (data){
res.send(data);
})
})
app.post('/songs/update/toggleFav',function(req,res){
var query2="SELECT fav FROM songs WHERE song_id="+req.body.song_id;
// console.log(query);
sql.sqlQuery(query2,function (data){
var fav=data[0].fav;
var query1="UPDATE songs SET FAV="+(parseInt(fav)>0?0:1)+" WHERE song_id="+req.body.song_id;
// console.log(query1);
sql.sqlQuery(query1,function(data){});
res.send(JSON.stringify(fav>0?0:1));
})
})
app.post('/songs/data',function(req,res){
var query="SELECT * FROM songs WHERE song_id="+req.body.song_id;
sql.sqlQuery(query,function (data) {
res.send(data[0]);
})
})
app.get('/songs/search',function (req,res) {
var query1="SELECT * FROM songs WHERE name='"+convert_case(req.query.q)+"'";
sql.sqlQuery(query1,function (data) {
var query2="SELECT * FROM songs WHERE artist='"+convert_case(req.query.q)+"'";
sql.sqlQuery(query2,function (data2) {
// console.log(data.concat(data2));
res.send(data.concat(data2));
})
})
})
function convert_case(str) {
var lower = str.toLowerCase();
return lower.replace(/(^| )(\w)/g, function(x) {
return x.toUpperCase();
});
}
app.post('/upload', function(req, res) {
// console.log("blhblaqkdksxz");
if (!req.files)
return res.status(400).send('No files were uploaded.');
let textfile = req.files.file;
// console.log(textfile);
// the uploaded file object
if(textfile.name.split('.')[1]=='mp3'&&uploadID!=0){
textfile.mv('./public_static/music/'+textfile.name);
res.send("File Uploaded");
sql.sqlQuery("UPDATE songs SET location='music/"+textfile.name+"' WHERE song_id="+uploadID,function (data) {
console.log(data);
});
}
else{
res.status(400).send('please upload mp3 file');
}
});
app.post('/upload/form',function (req,res) {
var query='INSERT INTO songs (name,artist,genre,img_src,fav) values ("'+convert_case(req.body.name)+'","'+convert_case(req.body.artist)+'","'+convert_case(req.body.genre)+'","'+req.body.img_src+'",0)';
sql.sqlQuery(query,function(data){
res.send(req.body.name);
// console.log(data);
uploadID=parseInt(data.insertId);
console.log(uploadID);
})
})
app.post('/user/playlist',function (req,res) {
mongodb.getList({'user':req.body.user},function (data) {
// console.log(data);
res.send(data);
})
})
app.post('/user/update',function (req,res) {
mongodb.updateItem({'user':req.body.data.user},{$set:{playlists:req.body.data.playlists}},function (data) {
// console.log(data);
res.send(data);
})
})
mongodb.connectToMongo(function () {
app.listen(port,function(){
console.log("listening on"+port);
});
})