-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubscriber.js
More file actions
83 lines (75 loc) · 1.8 KB
/
Copy pathsubscriber.js
File metadata and controls
83 lines (75 loc) · 1.8 KB
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
const sqlite3 = require("sqlite3").verbose();
class Subscriber {
constructor(dbPath) {
this.db = new sqlite3.Database(dbPath, (error) => {
if (error) {
console.error(error);
} else {
console.log("Connected to the subscribers database.");
}
});
this.db.run(
"CREATE TABLE IF NOT EXISTS subscribers (id INTEGER PRIMARY KEY,is_send BOOLEAN DEFAULT 0)"
);
}
async getSubscribers() {
return new Promise((resolve, reject) => {
this.db.all("SELECT id,is_send FROM subscribers", (error, rows) => {
if (error) {
reject(error);
} else {
resolve(rows);
}
});
});
}
async addSubscriber(id) {
return new Promise((resolve, reject) => {
this.db.run("INSERT INTO subscribers (id) VALUES (?)", id, (error) => {
if (error) {
reject(error);
} else {
resolve();
}
});
});
}
async removeSubscriber(id) {
return new Promise((resolve, reject) => {
this.db.run("DELETE FROM subscribers WHERE id=?", id, (error) => {
if (error) {
reject(error);
} else {
resolve();
}
});
});
}
async isSubscribed(id) {
return new Promise((resolve, reject) => {
this.db.get("SELECT id FROM subscribers WHERE id=?", id, (error, row) => {
if (error) {
reject(error);
} else {
resolve(row);
}
});
});
}
async updateIsSend(id) {
return new Promise((resolve, reject) => {
this.db.run(
"UPDATE subscribers SET is_send = 1 WHERE id = ?",
id,
(error) => {
if (error) {
reject(error);
} else {
resolve();
}
}
);
});
}
}
module.exports = Subscriber;