This repository has been archived by the owner on Nov 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
133 lines (120 loc) · 3.58 KB
/
db.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
const sqlite3 = require('sqlite3').verbose();
const log4js = require('log4js');
function openDb () {
const db = new sqlite3.Database('trains.sqlite');
return db;
}
function initDb () {
const db = openDb();
db.run('CREATE TABLE trains (user_id TEXT NOT NULL UNIQUE, departure TEXT NOT_NULL, arrive TEXT NOT_NULL, time TEXT NOT_NULL, counter INT NOT_NULL DEFAULT 0)');
}
function addUser (userId) {
const db = openDb();
db.run('INSERT OR IGNORE INTO trains (user_id) VALUES ($user_id)', {
$user_id: userId
});
}
function addDeparture (userId, message) {
const db = openDb();
db.run('UPDATE trains SET departure=$departure WHERE user_id=$user_id', {
$user_id: userId,
$departure: message
});
}
function addArrive (userId, message) {
const db = openDb();
db.run('UPDATE trains SET arrive=$arrive WHERE user_id=$user_id', {
$user_id: userId,
$arrive: message
});
}
function addTime (userId, message) {
const db = openDb();
db.run('UPDATE trains SET time=$time WHERE user_id=$user_id', {
$user_id: userId,
$time: message
});
}
function addCounter (userId) {
const db = openDb();
db.run('UPDATE trains SET counter=counter + 1 WHERE user_id=$user_id', {
$user_id: userId
});
}
function getStationsUser (userId) {
const db = openDb();
return new Promise((resolve) => {
db.get('SELECT departure, arrive, time FROM trains where user_id=?', [userId], function (err, stations) {
if (err) {
const logger = log4js.getLogger('error');
logger.error('getStationsUser', err);
throw new Error('error');
} else {
return resolve(stations);
}
});
});
}
function deleteStationsUser (userId) {
const db = openDb();
return new Promise((resolve) => {
db.get('DELETE departure, arrive, time FROM trains where user_id=?', [userId], function (err, stations) {
if (err) {
const logger = log4js.getLogger('error');
logger.error('deleteStationsUser', err);
throw new Error('error');
}
resolve(stations);
});
});
}
function checkStationUser (stations) {
const db = openDb();
return new Promise((resolve) => {
db.all(`SELECT * FROM stations WHERE nome_staz LIKE '${stations}%'`, function (err, station) {
if (err) {
const logger = log4js.getLogger('error');
logger.error('checkStationUser', err);
throw new Error('error');
}
return resolve(station);
});
});
}
function getListStations () {
const db = openDb();
return new Promise((resolve) => {
db.all(`SELECT nome_staz FROM stations`, function (err, station) {
if (err) {
const logger = log4js.getLogger('error');
logger.error('getListStations', err);
throw new Error('error');
}
return resolve(station);
});
});
}
function getAllUsers () {
const db = openDb();
return new Promise((resolve) => {
db.all(`SELECT user_id FROM trains`, function (err, users) {
if (err) {
const logger = log4js.getLogger('error');
logger.error('getAllUsers', err);
throw new Error('error');
}
return resolve(users);
});
});
}
module.exports.initDb = initDb;
module.exports.addUser = addUser;
module.exports.addDeparture = addDeparture;
module.exports.addArrive = addArrive;
module.exports.addTime = addTime;
module.exports.addCounter = addCounter;
module.exports.getStationsUser = getStationsUser;
module.exports.checkStationUser = checkStationUser;
module.exports.deleteStationsUser = deleteStationsUser;
module.exports.getListStations = getListStations;
module.exports.getAllUsers = getAllUsers;