-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgallery.go
102 lines (80 loc) · 2.05 KB
/
gallery.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
package main
import (
"errors"
"sync"
)
// GalleryDB struct
type GalleryDB struct {
db *DBHandler
querylocker sync.RWMutex
}
type GalleryConfig struct {
ChannelID string `storm:"id"` // The name of our config option
Whitelist []string
Enabled bool
}
// AddNotificationToDB function
func (h *GalleryDB) AddConfigToDB(config GalleryConfig) (err error) {
h.querylocker.Lock()
defer h.querylocker.Unlock()
db := h.db.rawdb.From("GalleryConfig")
err = db.Save(&config)
return err
}
// RemoveNotificationFromDB function
func (h *GalleryDB) RemoveConfigFromDB(config GalleryConfig) (err error) {
h.querylocker.Lock()
defer h.querylocker.Unlock()
db := h.db.rawdb.From("GalleryConfig")
err = db.DeleteStruct(&config)
return err
}
// GetNotificationFromDB function
func (h *GalleryDB) GetConfigFromDB(channelid string) (entry GalleryConfig, err error) {
configs, err := h.GetAllConfigs()
if err != nil {
return entry, err
}
for _, i := range configs {
if i.ChannelID == channelid {
return i, nil
}
}
return entry, errors.New("No record found")
}
// GetAllNotifications function
func (h *GalleryDB) GetAllConfigs() (configlist []GalleryConfig, err error) {
h.querylocker.Lock()
defer h.querylocker.Unlock()
db := h.db.rawdb.From("GalleryConfig")
err = db.All(&configlist)
if err != nil {
return configlist, err
}
return configlist, nil
}
// UpdateConfig - Don't use this, it doesn't work as intended
func (h *GalleryDB) UpdateConfig(config GalleryConfig) (err error) {
h.querylocker.Lock()
defer h.querylocker.Unlock()
db := h.db.rawdb.From("GalleryConfig")
err = db.Update(&config)
if err != nil {
return err
}
return nil
}
func (h *GalleryDB) CheckEnabled(channelid string) (enabled bool, err error) {
entry, err := h.GetConfigFromDB(channelid)
if err != nil {
return false, err
}
return entry.Enabled, nil
}
func (h *GalleryDB) GetWhitelist(channelid string) (whitelist []string, err error) {
entry, err := h.GetConfigFromDB(channelid)
if err != nil {
return []string{""}, err
}
return entry.Whitelist, nil
}