-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
135 lines (120 loc) · 2.86 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
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
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"github.com/HouzuoGuo/tiedot/db"
log "gopkg.in/inconshreveable/log15.v2"
)
var conn *db.Col
// Command represents a Twitch chat command.
type Command struct {
Name string `json:"name"`
Response string `json:"response"`
}
func initDatabase() error {
cwd, err := os.Getwd()
if err != nil {
return err
}
databaseDirectory := filepath.Join(cwd, "database")
log.Info(fmt.Sprintf("Opening database at: %s", databaseDirectory))
commandDatabase, err := db.OpenDB(databaseDirectory)
if err != nil {
return err
}
collection := "Commands"
if !collectionExists(commandDatabase, collection) {
log.Info(fmt.Sprintf("Creating Collection: %s", collection))
if err := commandDatabase.Create(collection); err != nil {
return err
}
}
conn = commandDatabase.Use(collection)
return nil
}
func collectionExists(db *db.DB, collectionName string) bool {
for _, name := range db.AllCols() {
if name == collectionName {
return true
}
}
return false
}
// CreateCommand inserts a command into the NoSQL database.
func CreateCommand(cmd map[string]interface{}) bool {
_, err := conn.Insert(cmd)
if err != nil {
log.Error(fmt.Sprintf("Could not insert command: %s", cmd["name"]))
return false
}
return true
}
// CommandExists checks if the command with the given name
// exists in the database.
func CommandExists(name string) (result bool) {
result = false
conn.ForEachDoc(func(id int, doc []byte) (moveOn bool) {
cmd := new(Command)
json.Unmarshal(doc, cmd)
if cmd.Name == name {
result = true
return false
}
return true
})
return result
}
// GetCommand reads the command with the given name from the database.
func GetCommand(name string) (result *Command) {
conn.ForEachDoc(func(id int, doc []byte) (moveOn bool) {
cmd := new(Command)
json.Unmarshal(doc, cmd)
if cmd.Name == name {
result = cmd
return false
}
return true
})
return result
}
// ListCommands returns a slice containing the names of all commands
// that exist in the database.
func ListCommands() (list []string) {
conn.ForEachDoc(func(id int, doc []byte) (moveOn bool) {
cmd := new(Command)
json.Unmarshal(doc, cmd)
list = append(list, cmd.Name)
return true
})
return list
}
// getCommandIndex finds the index of a command with the given name.
func getCommandIndex(name string) (index int) {
success := false
conn.ForEachDoc(func(id int, doc []byte) (moveOn bool) {
cmd := new(Command)
json.Unmarshal(doc, cmd)
if cmd.Name == name {
index = id
success = true
return false
}
return true
})
if !success {
return -1
}
return index
}
// DeleteCommand deletes the command with the given name.
// It returns false if the command does not exist.
func DeleteCommand(name string) bool {
index := getCommandIndex(name)
if index == -1 {
return false
}
conn.Delete(index)
return true
}