diff --git a/projet/hyperdrive-rest.js b/projet/hyperdrive-rest.js index 78ed33c8201d4037229a1396c6824c99f797f0ca..e90e775a604eba5b46309ba6c97a109d8b402383 100644 --- a/projet/hyperdrive-rest.js +++ b/projet/hyperdrive-rest.js @@ -253,6 +253,8 @@ app.get('/download/:file_id', (req, res) => { * param */ app.get('/change-path*', (req, res) => { + + sql.changeDirectory('a', req.params['0']); res.send(`Request for a change path (${req.params['0']})`) }) diff --git a/projet/sql-request.js b/projet/sql-request.js index 43ad804421d8bbdc8983d3b3f6d9f074f3ce4ccf..635716ef39df77ed4eafa67d9aa7f9437278b7a7 100644 --- a/projet/sql-request.js +++ b/projet/sql-request.js @@ -14,21 +14,20 @@ var con = mysql.createConnection({ * @param {string} login This is the login of the user * @param {string} pass This is the password of the user */ -function userExist(login, pass){ +function userExist(login, pass, callback){ const q = `SELECT login, passwd FROM Users WHERE login='${login}' AND passwd='${pass}';`; - return new Promise (() => { - con.query(q, function (err, result) { - if (err) return false; - if (result.length > 0) { - console.log("user exists"); - return { - login: result[0]['login'], - passwd: result[0]['passwd'] - }; - } else { - console.log("user don't exists"); - } - }); + con.query(q, function (err, result) { + if (err) return false; + if (result.length > 0) { + console.log("user already exists"); + return callback({ + login: result[0]['login'], + passwd: result[0]['passwd'] + }); + } else { + console.log("user don't exists"); + return callback(false); + } }); } @@ -37,16 +36,17 @@ function userExist(login, pass){ * @param {string} login This is the login of the user * @param {string} pass This is the password of the user */ -async function insertUser(login, passwd) { +function insertUser(login, passwd, callback){ let q = `INSERT INTO Users VALUES ('${login}', '${passwd}');`; - return new Promise( () => { - userExist(login, passwd).then( + userExist(login, passwd, (userExist) => { + if (!userExist){ con.query(q, (err, res) => { - if (err) return false; - console.log("User", login, "inserted in the db") - return true; - }) - ) + if (err) return callback(false); + if (!res) {console.log(res);return callback(false);} + console.log("User", login, "inserted in the db"); + return callback(true); + }); + } }); } @@ -56,18 +56,16 @@ async function insertUser(login, passwd) { * @param {string} login User's path * @param {string} parent Parent of the new folder */ -function addPath(path, login, parent){ +function addPath(path, login, parent, callback){ let q = `INSERT INTO Paths VALUES ('${path}', '${login}', ${parent});`; - return new Promise(() => { - con.query(q, function(err, res) { - if (err) { - console.log("Error while adding a new path"); - console.log(err); - return false; - } - console.log("New path", path, "added succesfully !"); - return true; - }); + con.query(q, function(err, res) { + if (err) { + console.log("Error while adding a new path"); + console.log(err); + return callback(false); + } + console.log("New path", path, "added succesfully !"); + return callback(true); }); } @@ -77,13 +75,18 @@ function addPath(path, login, parent){ * @param {string} login This is the login of the user * @param {string} pass This is the password of the user */ -async function addUser(login, passwd){ - return new Promise(async () => { - await insertUser(login, passwd).then( - addPath('/'+login, login, null)); +function addUser(login, passwd) { + + insertUser(login, passwd, (insertOk) => { + console.log(insertOk); + if (insertOk) { + addPath('/'+login, login, null, (res)=>{}); + console.log("Add ok"); + } }); } + /** +----------------+-------+-----------+---------+-----------+ | paths | login | parent | file_id | file_name | @@ -93,20 +96,33 @@ async function addUser(login, passwd){ | /a/coucou/test | a | /a/coucou | NULL | NULL | +----------------+-------+-----------+---------+-----------+ */ -async function changePath(login, path){ +async function changeDirectory(login, path, callback){ q = ` SELECT Paths.paths, login, parent, Files.file_id, Files.file_name FROM Paths LEFT JOIN Files ON Files.paths = Paths.paths - WHERE Files.file_id IS NOT NULL - AND login='${login}' AND Paths.paths='${path}'`; + WHERE Paths.login='${login}' + AND Paths.paths='${path}' + OR Paths.parent='${path}';`; con.query(q, (err, res) => { if (err) { console.log("Error while loading the path"); - return false; + return callback(false, err); + } + + let content = []; + for (let i in res){ + content.push({ + paths: res[i].paths, + login: res[i].login, + parent: res[i].parent, + file_id: res[i].file_id, + file_name: res[i].file_name + }); } - console.log(res); + + console.log(content); }); return [ @@ -134,6 +150,6 @@ async function changePath(login, path){ ]; } -exports.userExist = userExist; exports.addUser = addUser; -exports.addPath = addPath; \ No newline at end of file +exports.addPath = addPath; +exports.changeDirectory = changeDirectory; \ No newline at end of file