favorites.js
7.2 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
const express = require('express');
const router = express.Router();
router.get('/show', function(req, res, next) {
console.log(req.query);
user_id = req.query.id;
folders = [];
files = [];
let checkfolder = 'SELECT * FROM Folder_Favorites WHERE user_id = ?;';
let checkfiles = 'SELECT * FROM File_Favorites WHERE user_id = ?;';
connection.query(checkfolder, [user_id], function(err, rows, fields) {
if (err) {
console.log('select error');
res.status(404).send();
} else {
folders.push(rows);
}
});
connection.query(checkfiles, [user_id], function(err, rows, fields) {
if (err) {
console.log('select error');
res.status(404).send();
} else {
files.push(rows);
}
});
res.status(200).send({
folders: folders,
files: files
});
});
router.post('/addfolder', function(req, res, next) {
let user_id = req.body.id;
let folder_name = req.body.name;
console.log(req.body);
let getid = 'SELECT folder_id FROM folders WHERE user_id = ? AND folder_name = ?;';
connection.query(getid, [user_id, folder_name], function(err, folder, fields) {
if (err) {
console.log('getid error');
res.status(400).send({ error: err });
} else {
if (folder.length == 0) {
console.log('dont exist error');
res.status(400).send({ error: err });
} else {
let folder_id = folder[0].folder_id;
console.log(folder_id);
let checkfolder = 'SELECT * FROM Folder_Favorites WHERE user_id = ? AND folder_id = ?;';
connection.query(checkfolder, [user_id, folder_id], function(err, rows, fields) {
if (err) {
console.log('select error');
res.status(400).send({ error: err });
} else {
if (rows.length == 0) {
let sql = 'INSERT INTO Folder_Favorites (folder_id,user_id) values (?,?);';
connection.query(sql, [folder_id, user_id], function(err, result, fields) {
if (err) {
console.log('insert error');
res.status(400).send({ error: err });
} else {
res.status(200).send({
success: 'success',
error: ''
});
}
});
} else {
console.log('already exist error');
res.status(400).send({ error: err });
}
}
});
}
}
});
});
router.post('/addfile', function(req, res, next) {
let user_id = req.body.id;
let file_name = req.body.name;
let getid = 'SELECT file_id FROM files WHERE user_id = ? AND file_name = ?;';
connection.query(getid, [user_id, file_name], function(err, file, fields) {
if (err) {
console.log('getid error');
res.status(400).send({ error: err });
} else {
let file_id = file[0].file_id;
let checkfile = 'SELECT * FROM File_Favorites WHERE user_id = ? AND file_id = ?;';
connection.query(checkfile, [user_id, file_id], function(err, rows, fields) {
if (err) {
console.log('select error');
res.status(400).send({ error: err });
} else {
if (rows.length == 0) {
let sql = 'INSERT INTO File_Favorites (file_id,user_id) values (?,?);';
connection.query(sql, [file_id, user_id], function(err, result, fields) {
if (err) {
console.log('insert error');
res.status(400).send({ error: err });
} else {
res.status(200).send({
success: 'success',
error: ''
});
}
});
} else {
console.log('already exist error');
res.status(400).send({ error: err });
}
}
});
}
});
});
router.post('/delfolder', function(req, res, next) {
let user_id = req.body.id;
let folder_name = req.body.name;
let checkfolder = 'SELECT * FROM Folder_Favorites JOIN folders ON (Folder_Favorites.folder_id = folders.folder_id AND Folder_Favorites.user_id = ? AND folders.folder_name = ?);';
connection.query(checkfolder, [user_id, folder_name], function(err, rows, fields) {
if (err) {
console.log('select error');
res.status(400).send({ error: err });
} else {
if (rows.length != 0) {
let folder_id = rows[0].folder_id;
let sql = 'DELETE Folder_Favorites WHERE folder_id = ? AND user_id = ?;';
connection.query(sql, [folder_id, user_id], function(err, result, fields) {
if (err) {
console.log('delete error');
res.status(400).send({ error: err });
} else {
res.status(200).send({
success: 'success',
error: ''
});
}
})
} else {
console.log('dont exist error');
res.status(400).send({ error: err });
}
}
});
});
router.post('/delfile', function(req, res, next) {
let user_id = req.body.id;
let file_name = req.body.name;
let checkfile = 'SELECT * FROM File_Favorites JOIN files ON (File_Favorites.file_id = files.file_id AND File_Favorites.user_id = ? AND files.file_name = ?);';
connection.query(checkfile, [user_id, file_name], function(err, rows, fields) {
if (err) {
console.log('select error');
res.status(400).send({ error: err });
} else {
if (rows.length != 0) {
let file_id = rows[0].file_id;
let sql = 'DELETE File_Favorites WHERE file_id = ? AND user_id = ?;';
connection.query(sql, [file_id, user_id], function(err, result, fields) {
if (err) {
console.log('insert error');
res.status(400).send({ error: err });
} else {
res.status(200).send({
success: 'success',
error: ''
});
}
})
} else {
console.log('dont exist error');
res.status(400).send({ error: err });
}
}
});
});
module.exports = router;