Hong

즐겨찾기 수정

...@@ -34,21 +34,26 @@ router.get('/show', function(req, res, next) { ...@@ -34,21 +34,26 @@ 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 -
39 - connection.query(checkfolder, [user_id, folder_name], function(err, rows, fields) {
40 - if (err) {
41 - console.log('select error');
42 - res.status(400).send({ error: err });
43 - } else {
44 - if (!rows) {
45 let getid = 'SELECT folder_id FROM folders WHERE user_id = ? AND folder_name = ?;'; 38 let getid = 'SELECT folder_id FROM folders WHERE user_id = ? AND folder_name = ?;';
46 connection.query(getid, [user_id, folder_name], function(err, folder, fields) { 39 connection.query(getid, [user_id, folder_name], function(err, folder, fields) {
47 if (err) { 40 if (err) {
48 console.log('getid error'); 41 console.log('getid error');
49 res.status(400).send({ error: err }); 42 res.status(400).send({ error: err });
50 } else { 43 } else {
44 + if (folder.length == 0) {
45 + console.log('dont exist error');
46 + res.status(400).send({ error: err });
47 + } else {
51 let folder_id = folder[0].folder_id; 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) {
52 + if (err) {
53 + console.log('select error');
54 + res.status(400).send({ error: err });
55 + } else {
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) {
...@@ -60,34 +65,36 @@ router.post('/addfolder', function(req, res, next) { ...@@ -60,34 +65,36 @@ router.post('/addfolder', function(req, res, next) {
60 error: '' 65 error: ''
61 }); 66 });
62 } 67 }
63 - }) 68 + });
64 - }
65 - })
66 } else { 69 } else {
70 + console.log('already exist error');
67 res.status(400).send({ error: err }); 71 res.status(400).send({ error: err });
68 } 72 }
69 } 73 }
70 }); 74 });
75 + }
76 + }
77 + });
71 }); 78 });
72 79
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) {
79 - if (err) {
80 - console.log('select error');
81 - res.status(400).send({ error: err });
82 - } else {
83 - if (!rows) {
84 let getid = 'SELECT file_id FROM files WHERE user_id = ? AND file_name = ?;'; 84 let getid = 'SELECT file_id FROM files WHERE user_id = ? AND file_name = ?;';
85 - connection.query(getid, [user_id, folder_name], function(err, file, fields) { 85 + connection.query(getid, [user_id, file_name], function(err, file, fields) {
86 if (err) { 86 if (err) {
87 console.log('getid error'); 87 console.log('getid error');
88 res.status(400).send({ error: err }); 88 res.status(400).send({ error: err });
89 } else { 89 } else {
90 let file_id = file[0].file_id; 90 let file_id = file[0].file_id;
91 + let checkfile = 'SELECT * FROM File_Favorites WHERE user_id = ? AND file_id = ?;';
92 + connection.query(checkfile, [user_id, file_id], function(err, rows, fields) {
93 + if (err) {
94 + console.log('select error');
95 + res.status(400).send({ error: err });
96 + } else {
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,14 +106,15 @@ router.post('/addfile', function(req, res, next) { ...@@ -99,14 +106,15 @@ router.post('/addfile', function(req, res, next) {
99 error: '' 106 error: ''
100 }); 107 });
101 } 108 }
102 - }) 109 + });
103 - }
104 - })
105 } else { 110 } else {
111 + console.log('already exist error');
106 res.status(400).send({ error: err }); 112 res.status(400).send({ error: err });
107 } 113 }
108 } 114 }
109 }); 115 });
116 + }
117 + });
110 }); 118 });
111 119
112 router.post('/delfolder', function(req, res, next) { 120 router.post('/delfolder', function(req, res, next) {
...@@ -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 }
......