Commit f83621f8 authored by Romain OZDEN's avatar Romain OZDEN

Ajout de storage_H

parent d39d0941
const { create,
getUsers,
getUserById,
updateUser,
deleteUser,
getUserByEmail,
} = require('./user.service');
const { genSaltSync, hashSync, compareSync } = require('bcrypt');
const { sign } = require('jsonwebtoken');
module.exports = {
createUser: (req, res) => {
const body = req.body;
const salt = genSaltSync(10);
body.user.password = hashSync(body.user.password, salt);
console.log(body.user.password, 'ça a l\'air de marcher');
create(body, (err, results) => {
if (err) {
console.log(err);
return res.status(500).json({
success: 0,
message: 'Database connection error'
});
}
});
},
getUserById: (req, res) => {
const id = req.params.id;
getUserById(id, (err, results) => {
if (err) {
console.log(err);
return;
}
if (!results) {
return res.json({
success: 0,
message: 'Record not found'
});
}
return res.json({
success: 1,
data: results
});
});
},
getUsers: (req, res) => {
getUsers((err, results) => {
if (err) {
console.log(err);
return;
}
return res.json({
success: 1,
data: results
});
})
},
updateUser: (req, res) => {
const body = req.body;
const salt = genSaltSync(10);
body.password = hashSync(body.password, salt);
updateUser(body, (err, results) => {
if (err) {
console.log(err);
return;
}
if (!results) {
return res.json({
success: 0,
message: 'Failed to update user'
})
}
return res.json({
success: 1,
message: 'updated successfully'
});
});
},
deleteUser: (req, res) => {
const data = req.body;
deleteUser(data, (err, results) => {
if (err) {
console.log(err);
return;
}
if (!results) {
return res.json({
success: 0,
message: 'Record not found'
});
}
return res.json({
success: 1,
message: 'user deleted successfully'
});
});
},
login: (req,res) => {
const body = req.body;
getUserByEmail(body.email, (err, results) => {
if (err) {
console.log(err);
}
if (!results) {
return res.json({
success: 0,
data: 'Invalid email or password'
});
}
const result = compareSync(body.password, results.password);
if (result) {
results.password = undefined;
const jsontoken = sign({ result: results }, 'qwe1234', {
expiresIn : '1h'
});
return res.json({
success: 1,
message: 'login successfully',
token : jsontoken
});
} else {
return res.json({
success: 0,
data: 'Invalied email or password'
});
}
});
}
}
\ No newline at end of file
const { createUser,
getUsers,
getUserById,
updateUser,
deleteUser,
login,
} = require('./user.controller');
const router = require('express').Router();
const { checkToken } = require('../../auth/token_validation');
router.post('/add', createUser);
router.get('/', getUsers);
router.get('/:id', checkToken, getUserById);
router.patch('/', checkToken, updateUser);
router.delete('/', checkToken, deleteUser);
router.post('/login', login);
module.exports = router;
\ No newline at end of file
const sqlcon = undefined;
const allah = require('alasql');
allah(`
ATTACH FILESTORAGE DATABASE annales("${__dirname}/../../db/db.json");
USE annales;
`);
module.exports = {
create: (data, callBack) => {
allah.exec(
`insert into Utilisateur(username, password, firstname, lastname, email)
values(?,?,?,?,?)`,
[
data.user.username,
data.user.password,
data.user.firstname,
data.user.lastname,
data.user.email,
],
(res) => {
console.log(res);
}
);
},
getUsers: callBack => {
sqlcon.query(
`select id, username, password, firstname, lastname, email from utilisateur`,
[],
(error, results, fields) => {
if (error) {
return callBack(error);
}
return callBack(null, results);
}
);
},
getUserById : (id, callBack) => {
sqlcon.query(
`select id, nom, prenom, username, password, email from utilisateur where id = ?`,
[id],
(error, results, fields) => {
if (error) {
callBack(error);
}
return callBack(null, results[0]);
}
)
},
updateUser: (data, callBack) => {
sqlcon.query(
`update utilisateur set nom=?, prenom=?, username=?, password=?, email=? where id = ?`,
[
data.nom,
data.prenom,
data.username,
data.password,
data.email,
data.id,
],
(error, results, fields) => {
if (error) {
callBack(error);
}
return callBack(null, results);
}
);
},
deleteUser: (data, callBack) => {
sqlcon.query(
`delete from utilisateur where id = ?`,
[data.id],
(error, results, fields) => {
if (error) {
callBack(error);
}
return this.callBack(null, results[0]);
}
)
},
getUserByEmail: (email, callback) => {
sqlcon.query(
`select * from utilisateur where email = ?`,
[email],
(error, results, fields) => {
if (error) {
callBack(error);
}
return callBack(null, results[0]);
}
);
}
};
\ No newline at end of file
const { verify } = require('jsonwebtoken');
module.exports = {
checkToken: (req, res, next) => {
let token = req.get('authorization');
if (token) {
token = token.slice(7);
verify(token, 'qwe1234', (err, decoded) => {
if (err){
res.json({
success: 0,
message: 'Invalid token'
})
} else {
next();
}
})
} else {
res.json({
success: 0,
message: 'Access denied! Unauthorized user'
});
}
}
}
\ No newline at end of file
{"tables":{"Promo":{"columns":[{"columnid":"promo_id","dbtypeid":"NUMBER UNSIGNED","notnull":true,"identity":{"value":1,"step":1}},{"columnid":"annee","dbtypeid":"STRING"},{"columnid":"specialite","dbtypeid":"STRING"}]},"Role":{"columns":[{"columnid":"role_id","dbtypeid":"NUMBER UNSIGNED","notnull":true,"identity":{"value":1,"step":1}},{"columnid":"nom","dbtypeid":"STRING"}],"dirty":true},"Utilisateur":{"columns":[{"columnid":"user_id","dbtypeid":"NUMBER UNSIGNED","notnull":true,"identity":{"value":1,"step":1}},{"columnid":"identifiant","dbtypeid":"STRING","notnull":true},{"columnid":"mot_de_passe","dbtypeid":"STRING","notnull":true},{"columnid":"nom","dbtypeid":"STRING"},{"columnid":"prenom","dbtypeid":"STRING"},{"columnid":"email","dbtypeid":"STRING"},{"columnid":"role_id","dbtypeid":"NUMBER UNSIGNED","notnull":true}],"dirty":true},"Matiere":{"columns":[{"columnid":"matiere_id","dbtypeid":"NUMBER UNSIGNED","notnull":true,"identity":{"value":1,"step":1}},{"columnid":"nom","dbtypeid":"STRING"},{"columnid":"user_id","dbtypeid":"NUMBER UNSIGNED","notnull":true},{"columnid":"promo_id","dbtypeid":"NUMBER UNSIGNED","notnull":true}]},"Sujet":{"columns":[{"columnid":"sujet_id","dbtypeid":"NUMBER UNSIGNED","notnull":true,"identity":{"value":1,"step":1}},{"columnid":"nom","dbtypeid":"STRING"},{"columnid":"pdf_sujet","dbtypeid":"STRING"},{"columnid":"pdf_correction","dbtypeid":"STRING"},{"columnid":"verif","dbtypeid":"BOOLEAN"},{"columnid":"date_depot","dbtypeid":"DATE"},{"columnid":"matiere_id","dbtypeid":"NUMBER UNSIGNED","notnull":true}]},"Commentaire":{"columns":[{"columnid":"com_id","dbtypeid":"NUMBER UNSIGNED","notnull":true,"identity":{"value":1,"step":1}},{"columnid":"position","dbtypeid":"NUMBER UNSIGNED"},{"columnid":"message","dbtypeid":"STRING"},{"columnid":"typesujet","dbtypeid":"STRING"},{"columnid":"sujet_id","dbtypeid":"NUMBER UNSIGNED","notnull":true},{"columnid":"user_id","dbtypeid":"NUMBER UNSIGNED","notnull":true}]},"Notification":{"columns":[{"columnid":"notif_id","dbtypeid":"NUMBER UNSIGNED","notnull":true,"identity":{"value":1,"step":1}},{"columnid":"user_id","dbtypeid":"NUMBER UNSIGNED","notnull":true}]},"Suivre":{"columns":[{"columnid":"matiere_id","dbtypeid":"NUMBER UNSIGNED","notnull":true},{"columnid":"user_id","dbtypeid":"NUMBER UNSIGNED","notnull":true}]}},"Promo":[],"Role":[{"nom":"Etudiant"},{"nom":"Admin"}],"Utilisateur":[{"username":"ihu","password":"$2b$10$PNhFNzA3KuuV.2fKY1CcwuU/7iGgjZeuF1LfoFl.Kjfo9emcYY7aq","email":""},{"username":"ihu","password":"$2b$10$ZUUkLlBmX50KXjoAfKxXXOJZsu..rvnab6F187R1LNhLNqrFvwg2S","firstname":"","lastname":"","email":""},{"username":"romain","password":"$2b$10$BsEwI.UI0dxqQgaNCTP8/u9IGtpPipW1kTK3j/fVFNkcaqfvU9odm","firstname":"","lastname":"","email":""}],"Matiere":[],"Sujet":[],"Commentaire":[],"Notification":[],"Suivre":[]}
\ No newline at end of file
(function dbInit() {
const alasql = require('alasql');
alasql(`
CREATE FILESTORAGE DATABASE IF NOT EXISTS annales("${__dirname}/db.json");
ATTACH FILESTORAGE DATABASE annales("${__dirname}/db.json");
USE annales;
SOURCE "${__dirname}/init.sql";
`);
}());
\ No newline at end of file
CREATE TABLE IF NOT EXISTS Promo (
promo_id number UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
annee string,
specialite string
);
CREATE TABLE IF NOT EXISTS `Role` (
role_id number UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
nom string UNIQUE
);
CREATE TABLE IF NOT EXISTS Utilisateur (
`user_id` number UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
identifiant string NOT NULL UNIQUE,
mot_de_passe string NOT NULL,
nom string,
prenom string,
email string,
role_id number UNSIGNED NOT NULL,
CONSTRAINT `fk_user_role` FOREIGN KEY (role_id) REFERENCES Role (role_id)
);
CREATE TABLE IF NOT EXISTS Matiere (
matiere_id number UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
nom string,
`user_id` number UNSIGNED NOT NULL,
promo_id number UNSIGNED NOT NULL,
CONSTRAINT `fj_mat_user` FOREIGN KEY (`user_id`) REFERENCES Utilisateur (`user_id`),
CONSTRAINT `fk_mat_promo` FOREIGN KEY (promo_id) REFERENCES Promo (promo_id)
);
CREATE TABLE IF NOT EXISTS Sujet (
sujet_id number UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
nom string,
pdf_sujet string,
pdf_correction string,
verif boolean,
date_depot date,
matiere_id number UNSIGNED NOT NULL,
CONSTRAINT `fk_suj_mat` FOREIGN KEY (matiere_id) REFERENCES Matiere (matiere_id)
);
CREATE TABLE IF NOT EXISTS Commentaire (
com_id number UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
position number UNSIGNED,
`message` string,
typesujet string,
sujet_id number UNSIGNED NOT NULL,
`user_id` number UNSIGNED NOT NULL,
CONSTRAINT `fk_com_suj` FOREIGN KEY (sujet_id) REFERENCES Sujet (sujet_id),
CONSTRAINT `fk_com_user` FOREIGN KEY (`user_id`) REFERENCES Utilisateur (`user_id`)
);
CREATE TABLE IF NOT EXISTS `Notification` (
notif_id number UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`user_id` number UNSIGNED NOT NULL,
CONSTRAINT `fk_notif_user` FOREIGN KEY (`user_id`) REFERENCES Utilisateur (`user_id`)
);
CREATE TABLE IF NOT EXISTS `Suivre` (
matiere_id number UNSIGNED NOT NULL,
`user_id` number UNSIGNED NOT NULL,
PRIMARY KEY (matiere_id, `user_id`),
CONSTRAINT `fk_suj_mat` FOREIGN KEY (matiere_id) REFERENCES Matiere (matiere_id),
CONSTRAINT `fk_notif_user` FOREIGN KEY (`user_id`) REFERENCES Utilisateur (`user_id`)
);
INSERT INTO `Role`(nom) VALUES ('Etudiant'), ('Admin');
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment