diff --git a/projet/db/hyperdrive.sql b/projet/db/hyperdrive.sql index a7c6d2438d1d27198e5a9e9f870cbba00c151cb6..f1fbc9e2ac7221308b784ba09f5f8b06e7c562a0 100644 --- a/projet/db/hyperdrive.sql +++ b/projet/db/hyperdrive.sql @@ -39,8 +39,8 @@ CREATE TABLE IF NOT EXISTS Shares ( FOREIGN KEY (file_id) REFERENCES Files(file_id) ); --- Inserting datas +-- Inserting datas INSERT INTO Users VALUES ("a", "test"), diff --git a/projet/hyperdrive-rest.js b/projet/hyperdrive-rest.js index 0869843731f722e42ca9670b8a037b76b96aee04..78ed33c8201d4037229a1396c6824c99f797f0ca 100644 --- a/projet/hyperdrive-rest.js +++ b/projet/hyperdrive-rest.js @@ -95,8 +95,6 @@ app.get('/', (req, res) => { // resCode : [ 0: User now logged in, 1: False password, 2: Invalid username, 3: Empty user or pass ] app.get('/login', (req, res) => { - sql.connect(); - const user = req.query['user']; const pass = req.query['pass']; userObject = sql.userExist(user, pass); diff --git a/projet/sql-request.js b/projet/sql-request.js index 37e54d0f14d3cad14126c7143a1a735284c6322b..43ad804421d8bbdc8983d3b3f6d9f074f3ce4ccf 100644 --- a/projet/sql-request.js +++ b/projet/sql-request.js @@ -14,21 +14,21 @@ var con = mysql.createConnection({ * @param {string} login This is the login of the user * @param {string} pass This is the password of the user */ -async function userExist(login, pass){ +function userExist(login, pass){ const q = `SELECT login, passwd FROM Users WHERE login='${login}' AND passwd='${pass}';`; - con.query(q, function (err, result) { - console.log(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"); - return false; - } + 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"); + } + }); }); } @@ -38,28 +38,36 @@ async function userExist(login, pass){ * @param {string} pass This is the password of the user */ async function insertUser(login, passwd) { - let q = `INSERT INTO Users VALUES (${login}, ${passwd});`; - re = await userExist(login, passwd); - if (!re){ - console.log("querry"); - con.query(q, (err, res) => { - if (err) return false; - console.log("User", login, "inserted in the db") - return true; - }); - } + let q = `INSERT INTO Users VALUES ('${login}', '${passwd}');`; + return new Promise( () => { + userExist(login, passwd).then( + con.query(q, (err, res) => { + if (err) return false; + console.log("User", login, "inserted in the db") + return true; + }) + ) + }); } -function addPath(login, path, parent){ - let q = `INSERT INTO Paths VALUES ('/${login}', ${login}, ${parent});`; - 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; +/** + * This function add a new directory to a user + * @param {string} path Path to add + * @param {string} login User's path + * @param {string} parent Parent of the new folder + */ +function addPath(path, login, parent){ + 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; + }); }); } @@ -70,23 +78,11 @@ function addPath(login, path, parent){ * @param {string} pass This is the password of the user */ async function addUser(login, passwd){ - return new Promise(resolve => { - a == true; + return new Promise(async () => { + await insertUser(login, passwd).then( + addPath('/'+login, login, null)); }); } -// user_inserted = await insertUser(login, passwd); -// if (user_inserted) { -// path_added = await addPath(login, `/${login}`, 'NULL'); -// if (path_added) { -// console.log("File added succesfully"); -// return true -// } -// console.log("Error while adding a path"); -// return false; -// } -// return false; -// } - /** +----------------+-------+-----------+---------+-----------+ @@ -97,8 +93,22 @@ async function addUser(login, passwd){ | /a/coucou/test | a | /a/coucou | NULL | NULL | +----------------+-------+-----------+---------+-----------+ */ +async function changePath(login, path){ + 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}'`; + + con.query(q, (err, res) => { + if (err) { + console.log("Error while loading the path"); + return false; + } + console.log(res); + }); -function changePath(login, path){ return [ { path: '/a/coucou',