-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.js
173 lines (156 loc) · 5.2 KB
/
database.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database('./db.sqlite');
function createDatabase(fileCreateDatabaseScript) {
// read the file
var fs = require('fs');
fs.readFile(fileCreateDatabaseScript, 'utf8', function (err, data) {
if (err) {
return console.log(err);
}
// console.log(data);
db.exec(data, function (err) {
if (err) {
return console.log(err.message);
}
console.log("Database created");
});
});
}
function deleteDatabase(fileDatabase) {
var fs = require('fs');
if (fs.existsSync(fileDatabase)) {
fs.unlinkSync(fileDatabase);
console.log("Database deleted");
} else {
console.log("Database not found");
}
}
// ###############################
// # Utilizador
// ###############################
function createUtilizador(nome, email, password, onCreated = function (rowID) { }, onNotCreated = function () { }) {
db.run(`INSERT INTO Utilizador(nome, email, password) VALUES(?, ?, ?)`, [nome, email, password], function (err) {
if (err) {
return console.error(err.message);
}
if (this.changes == 0) {
onNotCreated();
} else {
onCreated(this.lastID);
}
});
}
function authenticateUtilizador(email, password, onAuthenticated = function (authenticated) { }, onNotAuthenticated = function () { }) {
db.get(`SELECT * FROM Utilizador WHERE email = ? AND password = ?`, [email, password], function (err, row) {
if (err) {
return console.error(err.message);
}
if (row == undefined) {
onNotAuthenticated();
} else {
onAuthenticated(row);
}
});
}
function deleteUtilizador(id, onDeleted = function () { }, onNotDeleted = function () { }) {
db.run(`DELETE FROM Utilizador WHERE id = ?`, [id], function (err) {
if (err) {
return console.error(err.message);
}
if (this.changes == 0) {
onNotDeleted();
} else {
onDeleted();
}
});
}
function updateUtilizador(id, nome, email, password, onUpdated = function () { }, onNotUpdated = function () { }) {
db.run(`UPDATE Utilizador SET nome = ?, email = ?, password = ? WHERE id = ?`, [nome, email, password, id], function (err) {
if (err) {
return console.error(err.message);
}
if (this.changes == 0) {
onNotUpdated();
} else {
onUpdated();
}
});
}
// ###############################
// # Tarefa
// ###############################
function createTarefa(idUtilizador, titulo, descricao, dataCriacao, dataLimite, onCreated = function (rowID) { }, onNotCreated = function () { }) {
db.run(`INSERT INTO Tarefa(idUtilizador, titulo, descricao, dataCriacao, dataLimite) VALUES(?, ?, ?, ?, ?)`, [idUtilizador, titulo, descricao, dataCriacao, dataLimite], function (err) {
if (err) {
return console.error(err.message);
}
if (this.changes == 0) {
onNotCreated();
} else {
onCreated(this.lastID);
}
});
}
function deleteTarefa(id, onDeleted = function () { }, onNotDeleted = function () { }) {
db.run(`DELETE FROM Tarefa WHERE id = ?`, [id], function (err) {
if (err) {
return console.error(err.message);
}
if (this.changes == 0) {
onNotDeleted();
} else {
onDeleted();
}
});
}
function updateTarefa(id, titulo, descricao, dataLimite, onUpdated = function () { }, onNotUpdated = function () { }) {
db.run(`UPDATE Tarefa SET titulo = ?, descricao = ?, dataLimite = ? WHERE id = ?`, [titulo, descricao, dataLimite, id], function (err) {
if (err) {
return console.error(err.message);
}
if (this.changes == 0) {
onNotUpdated();
} else {
onUpdated();
}
});
}
function getTarefa(id, onRetrieved = function (tarefa) { }, onNotRetrieved = function () { }) {
db.get(`SELECT * FROM Tarefa WHERE id = ?`, [id], function (err, row) {
if (err) {
return console.error(err.message);
}
if (row == undefined) {
onNotRetrieved();
} else {
onRetrieved(row);
}
});
}
function getTarefas(idUtilizador, onRetrieved = function (tarefas) { }, onNotRetrieved = function () { }) {
db.all(`SELECT * FROM Tarefa WHERE idUtilizador = ?`, [idUtilizador], function (err, rows) {
if (err) {
return console.error(err.message);
}
if (rows == undefined) {
onNotRetrieved();
} else {
onRetrieved(rows);
}
});
}
// ###############################
module.exports = {
deleteDatabase: deleteDatabase,
createDatabase: createDatabase,
createUtilizador: createUtilizador,
authenticateUtilizador: authenticateUtilizador,
deleteUtilizador: deleteUtilizador,
updateUtilizador: updateUtilizador,
createTarefa: createTarefa,
deleteTarefa: deleteTarefa,
updateTarefa: updateTarefa,
getTarefa: getTarefa,
getTarefas: getTarefas,
db: db
}