Showing
1 changed file
with
53 additions
and
43 deletions
... | @@ -34,37 +34,44 @@ router.get('/show', function(req, res, next) { | ... | @@ -34,37 +34,44 @@ router.get('/show', function(req, res, next) { |
34 | router.post('/addfolder', function(req, res, next) { | 34 | router.post('/addfolder', function(req, res, next) { |
35 | let user_id = req.body.id; | 35 | let user_id = req.body.id; |
36 | let folder_name = req.body.name; | 36 | let folder_name = req.body.name; |
37 | - 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 = ?);'; | 37 | + console.log(req.body); |
38 | - | 38 | + let getid = 'SELECT folder_id FROM folders WHERE user_id = ? AND folder_name = ?;'; |
39 | - connection.query(checkfolder, [user_id, folder_name], function(err, rows, fields) { | 39 | + connection.query(getid, [user_id, folder_name], function(err, folder, fields) { |
40 | if (err) { | 40 | if (err) { |
41 | - console.log('select error'); | 41 | + console.log('getid error'); |
42 | res.status(400).send({ error: err }); | 42 | res.status(400).send({ error: err }); |
43 | } else { | 43 | } else { |
44 | - if (!rows) { | 44 | + if (folder.length == 0) { |
45 | - let getid = 'SELECT folder_id FROM folders WHERE user_id = ? AND folder_name = ?;'; | 45 | + console.log('dont exist error'); |
46 | - connection.query(getid, [user_id, folder_name], function(err, folder, fields) { | 46 | + res.status(400).send({ error: err }); |
47 | + } else { | ||
48 | + let folder_id = folder[0].folder_id; | ||
49 | + console.log(folder_id); | ||
50 | + let checkfolder = 'SELECT * FROM Folder_Favorites WHERE user_id = ? AND folder_id = ?;'; | ||
51 | + connection.query(checkfolder, [user_id, folder_id], function(err, rows, fields) { | ||
47 | if (err) { | 52 | if (err) { |
48 | - console.log('getid error'); | 53 | + console.log('select error'); |
49 | res.status(400).send({ error: err }); | 54 | res.status(400).send({ error: err }); |
50 | } else { | 55 | } else { |
51 | - let folder_id = folder[0].folder_id; | 56 | + if (rows.length == 0) { |
52 | - let sql = 'INSERT INTO Folder_Favorites (folder_id,user_id) values (?,?);'; | 57 | + let sql = 'INSERT INTO Folder_Favorites (folder_id,user_id) values (?,?);'; |
53 | - connection.query(sql, [folder_id, user_id], function(err, result, fields) { | 58 | + connection.query(sql, [folder_id, user_id], function(err, result, fields) { |
54 | - if (err) { | 59 | + if (err) { |
55 | - console.log('insert error'); | 60 | + console.log('insert error'); |
56 | - res.status(400).send({ error: err }); | 61 | + res.status(400).send({ error: err }); |
57 | - } else { | 62 | + } else { |
58 | - res.status(200).send({ | 63 | + res.status(200).send({ |
59 | - success: 'success', | 64 | + success: 'success', |
60 | - error: '' | 65 | + error: '' |
61 | - }); | 66 | + }); |
62 | - } | 67 | + } |
63 | - }) | 68 | + }); |
69 | + } else { | ||
70 | + console.log('already exist error'); | ||
71 | + res.status(400).send({ error: err }); | ||
72 | + } | ||
64 | } | 73 | } |
65 | - }) | 74 | + }); |
66 | - } else { | ||
67 | - res.status(400).send({ error: err }); | ||
68 | } | 75 | } |
69 | } | 76 | } |
70 | }); | 77 | }); |
... | @@ -73,21 +80,21 @@ router.post('/addfolder', function(req, res, next) { | ... | @@ -73,21 +80,21 @@ router.post('/addfolder', function(req, res, next) { |
73 | router.post('/addfile', function(req, res, next) { | 80 | router.post('/addfile', function(req, res, next) { |
74 | let user_id = req.body.id; | 81 | let user_id = req.body.id; |
75 | let file_name = req.body.name; | 82 | let file_name = req.body.name; |
76 | - 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 = ?);'; | ||
77 | 83 | ||
78 | - connection.query(checkfile, [user_id, file_name], function(err, rows, fields) { | 84 | + let getid = 'SELECT file_id FROM files WHERE user_id = ? AND file_name = ?;'; |
85 | + connection.query(getid, [user_id, file_name], function(err, file, fields) { | ||
79 | if (err) { | 86 | if (err) { |
80 | - console.log('select error'); | 87 | + console.log('getid error'); |
81 | res.status(400).send({ error: err }); | 88 | res.status(400).send({ error: err }); |
82 | } else { | 89 | } else { |
83 | - if (!rows) { | 90 | + let file_id = file[0].file_id; |
84 | - let getid = 'SELECT file_id FROM files WHERE user_id = ? AND file_name = ?;'; | 91 | + let checkfile = 'SELECT * FROM File_Favorites WHERE user_id = ? AND file_id = ?;'; |
85 | - connection.query(getid, [user_id, folder_name], function(err, file, fields) { | 92 | + connection.query(checkfile, [user_id, file_id], function(err, rows, fields) { |
86 | - if (err) { | 93 | + if (err) { |
87 | - console.log('getid error'); | 94 | + console.log('select error'); |
88 | - res.status(400).send({ error: err }); | 95 | + res.status(400).send({ error: err }); |
89 | - } else { | 96 | + } else { |
90 | - let file_id = file[0].file_id; | 97 | + if (rows.length == 0) { |
91 | let sql = 'INSERT INTO File_Favorites (file_id,user_id) values (?,?);'; | 98 | let sql = 'INSERT INTO File_Favorites (file_id,user_id) values (?,?);'; |
92 | connection.query(sql, [file_id, user_id], function(err, result, fields) { | 99 | connection.query(sql, [file_id, user_id], function(err, result, fields) { |
93 | if (err) { | 100 | if (err) { |
... | @@ -99,12 +106,13 @@ router.post('/addfile', function(req, res, next) { | ... | @@ -99,12 +106,13 @@ router.post('/addfile', function(req, res, next) { |
99 | error: '' | 106 | error: '' |
100 | }); | 107 | }); |
101 | } | 108 | } |
102 | - }) | 109 | + }); |
110 | + } else { | ||
111 | + console.log('already exist error'); | ||
112 | + res.status(400).send({ error: err }); | ||
103 | } | 113 | } |
104 | - }) | 114 | + } |
105 | - } else { | 115 | + }); |
106 | - res.status(400).send({ error: err }); | ||
107 | - } | ||
108 | } | 116 | } |
109 | }); | 117 | }); |
110 | }); | 118 | }); |
... | @@ -119,7 +127,7 @@ router.post('/delfolder', function(req, res, next) { | ... | @@ -119,7 +127,7 @@ router.post('/delfolder', function(req, res, next) { |
119 | console.log('select error'); | 127 | console.log('select error'); |
120 | res.status(400).send({ error: err }); | 128 | res.status(400).send({ error: err }); |
121 | } else { | 129 | } else { |
122 | - if (rows) { | 130 | + if (rows.length != 0) { |
123 | let folder_id = rows[0].folder_id; | 131 | let folder_id = rows[0].folder_id; |
124 | let sql = 'DELETE Folder_Favorites WHERE folder_id = ? AND user_id = ?;'; | 132 | let sql = 'DELETE Folder_Favorites WHERE folder_id = ? AND user_id = ?;'; |
125 | connection.query(sql, [folder_id, user_id], function(err, result, fields) { | 133 | connection.query(sql, [folder_id, user_id], function(err, result, fields) { |
... | @@ -134,13 +142,14 @@ router.post('/delfolder', function(req, res, next) { | ... | @@ -134,13 +142,14 @@ router.post('/delfolder', function(req, res, next) { |
134 | } | 142 | } |
135 | }) | 143 | }) |
136 | } else { | 144 | } else { |
145 | + console.log('dont exist error'); | ||
137 | res.status(400).send({ error: err }); | 146 | res.status(400).send({ error: err }); |
138 | } | 147 | } |
139 | } | 148 | } |
140 | }); | 149 | }); |
141 | }); | 150 | }); |
142 | 151 | ||
143 | -router.post('/addfile', function(req, res, next) { | 152 | +router.post('/delfile', function(req, res, next) { |
144 | let user_id = req.body.id; | 153 | let user_id = req.body.id; |
145 | let file_name = req.body.name; | 154 | let file_name = req.body.name; |
146 | 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 = ?);'; | 155 | 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 = ?);'; |
... | @@ -150,7 +159,7 @@ router.post('/addfile', function(req, res, next) { | ... | @@ -150,7 +159,7 @@ router.post('/addfile', function(req, res, next) { |
150 | console.log('select error'); | 159 | console.log('select error'); |
151 | res.status(400).send({ error: err }); | 160 | res.status(400).send({ error: err }); |
152 | } else { | 161 | } else { |
153 | - if (!rows) { | 162 | + if (rows.length != 0) { |
154 | let file_id = rows[0].file_id; | 163 | let file_id = rows[0].file_id; |
155 | let sql = 'DELETE File_Favorites WHERE file_id = ? AND user_id = ?;'; | 164 | let sql = 'DELETE File_Favorites WHERE file_id = ? AND user_id = ?;'; |
156 | connection.query(sql, [file_id, user_id], function(err, result, fields) { | 165 | connection.query(sql, [file_id, user_id], function(err, result, fields) { |
... | @@ -165,6 +174,7 @@ router.post('/addfile', function(req, res, next) { | ... | @@ -165,6 +174,7 @@ router.post('/addfile', function(req, res, next) { |
165 | } | 174 | } |
166 | }) | 175 | }) |
167 | } else { | 176 | } else { |
177 | + console.log('dont exist error'); | ||
168 | res.status(400).send({ error: err }); | 178 | res.status(400).send({ error: err }); |
169 | } | 179 | } |
170 | } | 180 | } | ... | ... |
-
Please register or login to post a comment