-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDBM_Session.js
116 lines (108 loc) · 3.64 KB
/
DBM_Session.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
* @author Korentin Massif
* @version 1.0
*
* @file Gestion of the session table
*/
const mysql = require('mysql');
const fs = require('fs');
const Session = require("./Session")
const Logger = require("./Log")
const config = JSON.parse(fs.readFileSync('DB_Config.json', 'utf-8'));
class DSession
{
/** @private Link to the database*/ #host = config.database.host;
/** @private Username to connect to the database*/ #user = config.database.user;
/** @private Password to connect to the database*/ #password = config.database.password;
/** @private Name of the database you want to work in*/ #database_name = config.Session.status ? "Session" : null;
/** @private Connection object used to log in the database*/ #connection;
/** @private Logger object used to log messages*/ #logger = new Logger()
/**
* Set up the connection with the database.
* If the table does not exist, it will be created
*
* @constructor
* @this {DSession}
*/
constructor()
{
this.#connection = mysql.createConnection({
host: this.#host,
user: this.#user,
password: this.#password,
database: this.#database_name
});
if(!config.Session.status) // check if the table is created
{
this.connect();
this.#connection.query(config.Session.query, (error) => {
if (error)
{
this.#logger("SESSION", "ERROR", `Cannot create the database : ${error}`);
throw error;
}
this.#logger("SESSION", "SUCCES", "Table created");
});
this.close();
this.#database_name = "Session";
this.#connection = mysql.createConnection({
host: this.#host,
user: this.#user,
password: this.#password,
database: this.#database_name
});
config.Session.status = true;
fs.writeFileSync('DB_Config.json', JSON.stringify(config, null, 2));
};
}
/**
* Connect to the database
*/
connect()
{
this.#connection.connect((error) =>
{
if (error)
{
this.#logger("SESSION", "ERROR", `Cannot connect to the database : ${error}`);
return;
}
this.#logger("SESSION", "INFO", "Connected to the database");
});
}
/**
* Close the connection to the database
*/
close()
{
this.#connection.end((error) =>
{
if (error)
{
this.#logger("SESSION", "ERROR", `Cannot close the database : ${error}`);
return;
}
this.#logger("SESSION", "INFO", "Closed the database");
});
}
/**
* Add a session into the database
* @param {Session} session
*/
addUser(session)
{
query = "INSERT INTO Session (patient_id, user_id, session_type, session_date, session_time) VALUES (?, ?, ?, ?, ?)";
data = [session.getPatientId(), session.getUserId(), session.getSessionType(), session.getSessionDate(), session.getSessionTime()];
this.connect();
this.#connection.query(query, data, (error) => {
if (error)
{
this.#logger("SESSION", "ERROR", `Cannot insert in the database : ${error}`);
throw error;
}
this.#logger("SESSION", "SUCCES", "Session inserted");
});
this.close();
}
}
module.exports = DSession;