-
Notifications
You must be signed in to change notification settings - Fork 246
/
database.go
50 lines (42 loc) · 1.46 KB
/
database.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
package localnotifications
import (
"database/sql"
)
type Database struct {
db *sql.DB
network uint64
}
type NotificationPreference struct {
Enabled bool `json:"enabled"`
Service string `json:"service"`
Event string `json:"event,omitempty"`
Identifier string `json:"identifier,omitempty"`
}
func NewDB(db *sql.DB, network uint64) *Database {
return &Database{db: db, network: network}
}
func (db *Database) GetPreferences() (rst []NotificationPreference, err error) {
rows, err := db.db.Query("SELECT service, event, identifier, enabled FROM local_notifications_preferences")
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
pref := NotificationPreference{}
err = rows.Scan(&pref.Service, &pref.Event, &pref.Identifier, &pref.Enabled)
if err != nil {
return nil, err
}
rst = append(rst, pref)
}
return rst, nil
}
func (db *Database) GetWalletPreference() (rst NotificationPreference, err error) {
pref := db.db.QueryRow("SELECT service, event, identifier, enabled FROM local_notifications_preferences WHERE service = 'wallet' AND event = 'transaction' AND identifier = 'all'")
err = pref.Scan(&rst.Service, &rst.Event, &rst.Identifier, &rst.Enabled)
return
}
func (db *Database) ChangeWalletPreference(preference bool) error {
_, err := db.db.Exec("INSERT OR REPLACE INTO local_notifications_preferences (service, event, identifier, enabled) VALUES ('wallet', 'transaction', 'all', ?)", preference)
return err
}