-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
206 lines (143 loc) Β· 3.09 KB
/
store.go
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package main
import (
"strings"
"time"
r "gopkg.in/dancannon/gorethink.v2"
)
func ListUser() ([]*User, error) {
res, err := r.Table("user").Run(rc)
defer res.Close()
if err != nil {
return nil, err
}
users := make([]*User, 0)
if err := res.All(&users); err != nil {
return nil, err
}
return users, nil
}
func AddUser(u *User) error {
res, err := r.Table("user").Insert(u).RunWrite(rc)
if err != nil {
return err
}
u.ID = res.GeneratedKeys[0]
return nil
}
func RemoveUser(ID string) error {
_, err := r.Table("user").Get(ID).Delete().RunWrite(rc)
return err
}
func ListWebhook() ([]*Webhook, error) {
res, err := r.Table("webhook").Run(rc)
defer res.Close()
if err != nil {
return nil, err
}
webhooks := make([]*Webhook, 0)
if err := res.All(&webhooks); err != nil {
return nil, err
}
return webhooks, nil
}
func AddWebhook(w *Webhook) error {
res, err := r.Table("webhook").Insert(w).RunWrite(rc)
if err != nil {
return err
}
w.ID = res.GeneratedKeys[0]
return nil
}
func RemoveWebhook(ID string) error {
_, err := r.Table("webhook").Get(ID).Delete().RunWrite(rc)
return err
}
func GetWebhookByName(name string) (*Webhook, error) {
res, err := r.Table("webhook").Filter(map[string]string{
"Name": name,
}).Limit(1).Run(rc)
defer res.Close()
if err != nil {
return nil, err
}
var w Webhook
if err := res.One(&w); err != nil {
return nil, err
}
return &w, nil
}
func UpdateTimeSlot(ts *TimeSlot) error {
res, err := r.Table("time_slot").Filter(map[string]string{
"UID": ts.UID,
}).Limit(1).Run(rc)
defer res.Close()
if err != nil {
return err
}
if res.IsNil() {
w, _ := r.Table("time_slot").Insert(ts).RunWrite(rc)
ts.ID = w.GeneratedKeys[0]
return nil
}
var tmpTs TimeSlot
if err := res.One(&tmpTs); err != nil {
return err
}
r.Table("time_slot").Get(tmpTs.ID).Update(ts).RunWrite(rc)
ts.ID = tmpTs.ID
return nil
}
func GetUserByName(name string) (*User, error) {
res, err := r.Table("user").Filter(func(user r.Term) r.Term {
return user.Field("Name").Downcase().Eq(strings.ToLower(name))
}).Limit(1).Run(rc)
defer res.Close()
if err != nil {
return nil, err
}
if res.IsNil() {
return nil, nil
}
var u User
if err := res.One(&u); err != nil {
return nil, err
}
return &u, nil
}
func GetCurrentTimeSlot() ([]*TimeSlot, error) {
now := time.Now()
res, err := r.Table("time_slot").Filter(func(ts r.Term) r.Term {
return ts.Field("StartDate").Le(now).And(ts.Field("EndDate").Gt(now))
}).Run(rc)
defer res.Close()
if err != nil {
return nil, err
}
var slots []*TimeSlot
if err := res.All(&slots); err != nil {
return nil, err
}
return slots, nil
}
func GetAlwaysUser() ([]*User, error) {
res, err := r.Table("user").Filter(map[string]bool{
"Always": true,
}).Run(rc)
defer res.Close()
if err != nil {
return nil, err
}
var users []*User
if err := res.All(&users); err != nil {
return nil, err
}
return users, nil
}
func AddEvent(e *Event) error {
res, err := r.Table("event").Insert(e).RunWrite(rc)
if err != nil {
return err
}
e.ID = res.GeneratedKeys[0]
return nil
}