-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
99 lines (78 loc) · 1.95 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
const low = require('lowdb')
const FileSync = require('lowdb/adapters/FileSync')
const adapter = new FileSync('databases/db.json')
const db = low(adapter)
db.defaults({ users: [] })
.write()
db.getCheckers = (userId) => {
var user = db.get('users')
.find({userId})
.value()
if(!user) return [];
return user.checkers;
}
db.addChecker = (ctx, checker) => {
userId = ctx.from.id;
userInfo = ctx.from;
var user = db.get('users')
.find({userId})
if(user.value()) { //TODO:check if already present
user.get('checkers')
.push(checker)
.write()
}
else {
var userObj = {
userId,
userInfo,
checkers: [
checker
]
}
db.get("users")
.push(userObj)
.write()
}
ctx.reply("created!")
}
db.removeChecker = (ctx, name) => {
userId = ctx.from.id;
if(!isNaN(name)) {
//if name is a number
index=parseInt(name)
ctx.reply("removing index: " + index);
try {
var currValue = db.get('users')
.find({userId})
.get('checkers')
.value()
var newValue = currValue.filter((el, idx) => {
return idx+1 === index ? false : true;
})
db.get('users')
.find({userId})
.assign({checkers: newValue})
.write()
ctx.reply("removed");
return
}
catch {
ctx.reply("Checker not found")
return
}
}
try{
ctx.reply("removing: " + name);
db.get('users')
.find({userId})
.get('checkers')
.remove({name: name})
.write()
ctx.reply("removed: " + name);
}
catch {
ctx.reply("Checker not found");
}
}
// TODO: update and delete
module.exports = db