-
Notifications
You must be signed in to change notification settings - Fork 9
/
l01-e02.js
128 lines (105 loc) · 3.63 KB
/
l01-e02.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
'use strict';
/*
DB STRUCTURE
CREATE TABLE tasks (
id INTEGER PRIMARY KEY,
description TEXT NOT NULL,
urgent BOOLEAN DEFAULT (0) NOT NULL,
private BOOLEAN DEFAULT (1) NOT NULL,
deadline DATETIME
);
DATETIME FORMAT IS AS ISO 8601: 2018-04-04T16:00:00.000Z
*/
const sqlite = require("sqlite3");
const dayjs = require("dayjs");
// i18n and locale
const localizedFormat = require('dayjs/plugin/localizedFormat');
dayjs.extend(localizedFormat); // use shortcuts 'LLL' for date and time format
/*
const locale_it = require('dayjs/locale/it');
dayjs.locale('it');
*/
function Task(id, description, isUrgent = false, isPrivate = true, deadline = '') {
this.id = id;
this.description = description;
this.urgent = isUrgent;
this.private = isPrivate;
// saved as dayjs object
this.deadline = deadline && dayjs(deadline);
// dayjs().toString() prints GMT
// LLL stands for MMMM D, YYYY h:mm A see https://day.js.org/docs/en/display/format
this.toString = () => {
return `Id: ${this.id}, ` +
`Description: ${this.description}, Urgent: ${this.urgent}, Private: ${this.private}, ` +
`Deadline: ${this._formatDeadline('LLL')}`;
}
this._formatDeadline = (format) => {
return this.deadline ? this.deadline.format(format) : '<not defined>';
}
}
function TaskList() {
const db = new sqlite.Database('tasks.db', (err) => { if (err) throw err; });
this.getAll = () => {
return new Promise((resolve, reject) => {
const sql = 'SELECT * FROM tasks' ;
db.all(sql, [], (err, rows) => {
if(err)
reject(err);
else {
const tasks = rows.map(record => new Task(record.id, record.description, record.important == 1, record.private == 1, record.deadline));
resolve(tasks);
}
});
});
};
this.getAfterDeadline = (deadline) => {
return new Promise((resolve, reject) => {
const sql = 'SELECT * FROM tasks WHERE deadline > ?';
db.all(sql, [deadline.format()], (err, rows) => {
if(err)
reject(err);
else {
const tasks = rows.map(record => new Task(record.id, record.description, record.important == 1, record.private == 1, record.deadline));
resolve(tasks);
}
});
});
};
this.getWithWord = (word) => {
return new Promise((resolve, reject) => {
const sql = "SELECT * FROM tasks WHERE description LIKE ?";
db.all(sql, ["%" + word + "%"], (err, rows) => {
if(err)
reject(err);
else {
const tasks = rows.map(record => new Task(record.id, record.description, record.important == 1, record.private == 1, record.deadline));
resolve(tasks);
}
});
});
};
}
async function main(data) {
try {
const taskList = new TaskList();
// get all the tasks
console.log("****** All the tasks in the database: ******");
const tasks = await taskList.getAll();
tasks.forEach( (task) => console.log(task.toString()) );
//get tasks after a given deadline
const deadline = dayjs('2021-03-13T09:00:00.000Z');
console.log("****** Tasks after " + deadline.format() + ": ******");
const futureTasks = await taskList.getAfterDeadline(deadline);
futureTasks.forEach( (task) => console.log(task.toString()) );
//get tasks with a given word in the description
const word = "phone";
console.log("****** Tasks containing '" + word + "' in the description: ******");
const filteredTasks = await taskList.getWithWord(word);
filteredTasks.forEach( (task) => console.log(task.toString()) );
debugger;
} catch (error) {
console.error(error);
return;
}
}
main()