Select Git revision
sql-request.js
Forked from
Développement Web Avancé / 2019_TP2
Source project has a limited visibility.
sql-request.js 4.58 KiB
const mysql = require('mysql');
var con = mysql.createConnection({
host: "127.0.0.1",
port: 3306,
user: "hyperdrive",
password: "hyper",
database: 'hyperdrive',
insecureAuth: true
});
/**
* This function check if a user exist in the DB
* @param {string} login This is the login of the user
* @param {string} pass This is the password of the user
*/
function userExist(login, pass, callback){
const q = `SELECT login, passwd FROM Users WHERE login='${login}' AND passwd='${pass}';`;
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);
}
});
}
/**
* This function insert a new user in the DB
* @param {string} login This is the login of the user
* @param {string} pass This is the password of the user
*/
function insertUser(login, passwd, callback){
let q = `INSERT INTO Users VALUES ('${login}', '${passwd}');`;
userExist(login, passwd, (userExist) => {
if (!userExist){
con.query(q, (err, res) => {
if (err) return callback(false);
if (!res) {console.log(res);return callback(false);}
console.log("User", login, "inserted in the db");
return callback(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, callback){
let q = `INSERT INTO Paths VALUES ('${path}', '${login}', ${parent});`;
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);
});
}
/**
* This function add a user in the db it it doesn't already exists
* It also add the default path of the user after inserting the new user.
* @param {string} login This is the login of the user
* @param {string} pass This is the password of the user
*/
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 |
+----------------+-------+-----------+---------+-----------+
| /a/coucou | a | /a | abnnnnn | deux |
| /a/coucou | a | /a | accb | trois |
| /a/coucou/test | a | /a/coucou | NULL | NULL |
+----------------+-------+-----------+---------+-----------+
*/
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 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 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(content);
return callback(content, "Change dir success");
});
// return [
// {
// path: '/a/coucou',
// login: 'a',
// parent: '/a',
// file_id: 'abnnnnn',
// file_name: 'deux'
// },
// {
// path: '/a/coucou',
// login: 'a',
// parent: '/a', // file_id: 'accb',
// file_name: 'trois'
// },
// {
// path: '/a/coucou/test',
// login: 'a',
// parent: '/a/coucou',
// file_id: 'NULL',
// file_name: 'NULL'
// }
// ];
}
exports.addUser = addUser;
exports.addPath = addPath;
exports.changeDirectory = changeDirectory;